请问大家在sql 2005中怎么实现多表的查询的存储过程?

liuzhengkang 2009-03-19 05:35:36

数据库有三张表,如下
stk_shtUnBal(未平仓表):
字段有:shtNum(单号)、stkName(产品名称)、dtimeCreate(创建时间)、priceCreate(建仓价)、create_User_id(创建的用户ID)。

stk_shtBal(平仓表):
字段有:shtNum(单号)、stkName(产品名称)、dtimeCreate(创建时间)、priceCreate(建仓价)、create_User_id(创建的用户ID)、bal_matchCreate(平仓时间)、bal_matchPrice(平仓价)、bal_WinLost(盈亏)。

stk_shtCancel(取消表):
字段有:shtNum(单号)、stkName(产品名称)、dtimeCreate(创建时间)、priceCreate(建仓价)、create_User_id(创建的用户ID)、cancelTime(取消的时间)、msg(取消的原因)。

用户操作:
用户下了单之后会在stk_shtUnBal(未平仓表)中产生一条记录单号为0001,
当用户平仓之后,在stk_shtUnBal表中删除0001单并把单插入到stk_shtBal;
当用户取消之后,在stk_shtUnBal表中删除0001单并把单插入到stk_shtCancel;

现在的问题是:
我要根据单号查询单的信息,
返回的信息字段有:shtNum(单号)、stkName(产品名称)、dtimeCreate(创建时间)、priceCreate(建仓价)、create_User_id(创建的用户ID)、bal_matchCreate(平仓时间)、bal_matchPrice(平仓价)、bal_WinLost(盈亏)、cancelTime(取消的时间)、msg(取消的原因)、stkStauts(状态:未平仓或平仓或取消)。


即输入单号0001,要得到这张单的信息,请问各位大哥,存储过程要怎么从这3张表中返回单的信息?

...全文
196 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
-布谷鸟- 2009-03-20
  • 打赏
  • 举报
回复
设一个平仓标志,一个取消标志,就不用将数据删来移去的。简单多了。
-布谷鸟- 2009-03-20
  • 打赏
  • 举报
回复
不觉得这种设计有点问题吗?
不合理的设计必然造成操作复杂。
liuzhengkang 2009-03-20
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 cuckoo1 的回复:]
设一个平仓标志,一个取消标志,就不用将数据删来移去的。简单多了。
[/Quote]



呵呵,先顶一下1、2楼的!小弟先谢谢大家了!
cuckoo1 说的好像很对哦,不过这三张表是分别有线程在频繁扫描的,再加上用户的操作,可能就是高并发的了,所以就分了3个表,不知道这样是不是合理?
qizhengsheng 2009-03-20
  • 打赏
  • 举报
回复
顶钻石
zz005 2009-03-20
  • 打赏
  • 举报
回复
学习
叶子 2009-03-20
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 liuzhengkang 的回复:]
呵呵,先顶一下1、2楼的!小弟先谢谢大家了!
cuckoo1 说的好像很对哦,不过这三张表是分别有线程在频繁扫描的,再加上用户的操作,可能就是高并发的了,所以就分了3个表,不知道这样是不是合理?
[/Quote]
个人意见:整体来讲,设计成这样,表之间的操作太多,太麻烦了。
lovezx1028 2009-03-20
  • 打赏
  • 举报
回复
niu xx
ai_li7758521 2009-03-19
  • 打赏
  • 举报
回复
union all
wenblue7 2009-03-19
  • 打赏
  • 举报
回复
up
楼上的
kye_jufei 2009-03-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 libin_ftsafe 的回复:]
SQL codeselect
t.*
from
(select
shtNum,
stkName,
dtimeCreate,
priceCreate,
create_User_id,
null as bal_matchCreate,
null as bal_matchPrice,
null as bal_WinLost,
null as cancelTime,
null as msg,
'未平仓' as stkStauts
from
stk_shtUnBal
union all
se…
[/Quote]
先頂鑽石級回覆,再強烈建議使用union all進行兩表關聯查詢
dpzc_love 2009-03-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 libin_ftsafe 的回复:]
SQL codeselect
t.*
from
(select
shtNum,
stkName,
dtimeCreate,
priceCreate,
create_User_id,
null as bal_matchCreate,
null as bal_matchPrice,
null as bal_WinLost,
null as cancelTime,
null as msg,
'未平仓' as stkStauts
from
stk_shtUnBal
union all
se…
[/Quote]

强烈支持!
sdhdy 2009-03-19
  • 打赏
  • 举报
回复
楼上的。
-狙击手- 2009-03-19
  • 打赏
  • 举报
回复
直接union all平仓表和取消表
-狙击手- 2009-03-19
  • 打赏
  • 举报
回复
直接union all平仓表和取消表
ks_reny 2009-03-19
  • 打赏
  • 举报
回复

create proc myproc
@shtnum varchar
as
begin
select a.*,b.bal_matchCreate,b.bal_matchPrice,b.bal_WinLost,c.cancelTime,c.msg,
( case when b.shtNum is not null then '平倉'
when c.shtNum is not null then '取消'
else '未平倉' end) as stkStauts
from stk_shtUnBal a,stk_shtBal b,stk_shtCancel c
where a.shtNum*=b.shtNum and a.shtNum*=c.shtNum
and a.shtNum=@shtNum
end
子陌红尘 2009-03-19
  • 打赏
  • 举报
回复
select
t.*
from
(select
shtNum,
stkName,
dtimeCreate,
priceCreate,
create_User_id,
null as bal_matchCreate,
null as bal_matchPrice,
null as bal_WinLost,
null as cancelTime,
null as msg,
'未平仓' as stkStauts
from
stk_shtUnBal
union all
select
shtNum,
stkName,
dtimeCreate,
priceCreate,
create_User_id,
bal_matchCreate,
bal_matchPrice,
null as bal_WinLost,
null as cancelTime,
null as msg,
'平仓' as stkStauts
from
stk_shtBal
union all
select
shtNum,
stkName,
dtimeCreate,
priceCreate,
create_User_id,
null as bal_matchCreate,
null as bal_matchPrice,
null as bal_WinLost,
cancelTime,
msg,
'取消' as stkStauts
from
stk_shtCancel) t
where
t.shtNum='0001'

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧