0

我有这样的存储过程

ALTER Procedure [dbo].[spCarCallFetch] @Carid nvarchar(20)
as
begin
  Select 
      k.tid, 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
end

执行此操作时

exec spCarCallFetch'51891044554'

输出:

tid         HBarcode   make plateno   vtype         locname        model       Colname--------------------------------------------
6           564       BMW   44554    Normal       Fashion Avenue  520          Red

这里 tid 取自 KeyHanger 表,但对应的 tid 名称保存在 Terminal 表中,实际上我想获取对应的 tid 名称。

我的表如下所示:

KHanger_tbl

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

Terminals_tbl

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

我想取对应 tid 的唯一名称。我该如何为此编写存储过程?如果有人知道,请帮助我

4

1 回答 1

1

也许这样的东西是你想要的?

正如评论中建议的那样,应该使用显式连接,然后查询应该如下所示:

ALTER PROCEDURE [dbo].[spCarCallFetch] @Carid NVARCHAR(20)
AS

BEGIN

  SELECT
    k.tid, k.HBarcode, m.make,
    t.plateno, v.vtype, l.locname,
    mdl.model, c.Colname, te.UniqueName

  FROM transaction_tbl t

  INNER JOIN KHanger_tbl   AS k   ON t.transactID = k.transactID
  INNER JOIN make_tbl      AS m   ON t.mkid = m.mkid 
  INNER JOIN vtype_tbl     AS v   ON v.vtid = t.vtid 
  INNER JOIN Location_tbl  AS l   ON t.locid = l.locid 
  INNER JOIN Model_tbl     AS mdl ON mdl.mdlid = t.mdlid 
  INNER JOIN Color_tbl     AS c   ON t.colid = c.colid 
  INNER JOIN Terminals_tbl AS te  ON k.tid = te.tid

  WHERE t.tbarcode = @carid 

END
于 2013-06-19T20:09:08.827 回答