用于从多个表中获取数据的存储过程

时间:2013-06-19 19:31:47

标签: stored-procedures

我有这样的存储过程

Select k.HBarcode, m.make,t.plateno ,v.vtype ,l.locname,mdl.model,c.Colname 
from   transaction_tbl t,
       KHanger_tbl k,
       make_tbl m,
       vtype_tbl v,
       Location_tbl l,
       Model_tbl mdl,
       Color_tbl C  
 where t.tbarcode=@carid and 
       t.mkid=m.mkid and 
       v.vtid=t.vtid and 
       t.locid=l.locid and 
       mdl.mdlid=t.mdlid and 
       t.colid=c.colid and 
       t.transactID=k.transactID

执行此操作时获取输出

HBarcode     make     plateno    vtype   locname          model        Colname
34           BMW      44554      Normal  Fashion Avenue   520          Red

我还有两个表,从事务表中我可以获得transactid(上面的例子: t.transactID ),然后我可以得到相应的 来自“KHanger_tbl”的 tid ,然后我希望从“终端”表格中显示相应 tid 的uniquename

1-KHanger_tbl

transactid                              HBarcode                tid
--------------------------------------- ----------------------------------
19                                      34                      7
22                                      002                     5
21                                      1                       7
23                                      200005                   6

2- Terminals_tbl

tid         UniqueName
----------- --------------------------------------------------
5           Key Room-1
6           Podium -1
7           Key Room - 2

预期输出

 UniqueName HBarcode make plateno vtype  locname               model  Colname
 -------------------------------------------------------------------------------
 KeyRoom-2  34       BMW  44554   Norma Fashion Avenue       520      Red

所以如何为此编写存储过程,如果有人知道,请帮助我

1 个答案:

答案 0 :(得分:0)

这样的事可能吗?

Select  term.UniqueName , 
   k.HBarcode, m.make,t.plateno ,v.vtype ,l.locname,mdl.model,c.Colname 
from transaction_tbl t,
     KHanger_tbl k,
     make_tbl m,
     vtype_tbl v,
     Location_tbl l,
     Model_tbl mdl,
     Color_tbl C  
     ,
     Terminals_tbl Term

where t.tbarcode=@carid and 
      t.mkid=m.mkid and 
      v.vtid=t.vtid and 
      t.locid=l.locid and 
      mdl.mdlid=t.mdlid and 
      t.colid=c.colid and 
      t.transactID=k.transactID 
      and
      term.tid = t.transactID