0

我有一个这样的存储过程:

alter procedure [dbo].[fetchkey]
@locid int =null
as
begin
 select t.TBarcode,t.Status
from Transaction_tbl t 
left join EmployeeMaster_tbl e on t.Ecode=e.Ecode where Locid=5
end

我的输出

TBarcode             Status
-------------------- -----------
57173621345          0
57173865238          1
57181456325          2
57182756600          3
58125323124          4
-----------------------------------

我有另一个存储过程来根据状态获取密钥位置:

ALTER procedure [dbo].[Keylocation]
@Carid nvarchar(50)
as
begin
SET NOCOUNT ON;
SELECT 
        t.Status,
        k.HBarcode,
        te.UniqueName,
     COALESCE(e.Ename, e1.Ename) AS EName
FROM    Transaction_tbl t
        left JOIN UserMaster_tbl u
            ON u.uid = t.PAICID
             left join EmployeeMaster_tbl e on e.ECode=u.Ecode
            AND t.Status = 0
        LEFT JOIN EmployeeMaster_tbl e1
            ON e1.ECode = t.ECode
            AND t.Status = 1 or e1.Ecode=t.DelEcode and t.Status=4
            left Join KHanger_tbl k
            on t.transactID=k.transactid
            and t.Status in(2,3)
            left JOIN Terminals_tbl te ON k.tid = te.tid

WHERE   t.TBarcode = @Carid
end

我的输出是这样的:

Status      HBarcode         UniqueName                EName                         
3           001              Key Room-1                NULL 

这是状态 3 的结果。在我的第一个存储过程中,我想根据这个结果再获得一列。我的预期输出:

TBarcode             Status    key location
-------------------- -----------
57173621345          0         with Employee(Ename Value)
57173865238          1         with Paic(Ename value)
57181456325          2         UniqueName value( HBarcode value)                       
57182756600          3          UniqueName value ( HBarcode value) 
58125323124          4          with driver(Ename Valu)

有什么办法吗?我是存储过程中的新手。有什么方法可以创建函数并将此结果调用到我的存储过程中,如果有人知道请帮助我

4

1 回答 1

0

看一下表值函数,这可用于将一个函数的输出作为另一个存储过程的输入。这是解释它的最简单的链接之一,请参见此处的表值函数

请参阅下文了解您的情况。我不确定应该进行什么联接,但是如果您查看并了解它的编写方式,您肯定会明白这一点。

CREATE FUNCTION FetchKEY(@locid int)    
 RETURNS @keydetails TABLE  (TbarCode int NOT NULL,    Status int NOT NULL )  AS 
  BEGIN   
     INSERT INTO @person (TbarCode,Status)  select t.TBarcode,t.Status from
     Transaction_tbl t  left join EmployeeMaster_tbl e on t.Ecode=e.Ecode
     where Locid=@locid     
  RETURN; 
  END;
Go

所以你的 Store 过程是这样的

 ALTER procedure [dbo].[Keylocation] @Carid nvarchar(50) as begin 
 SET NOCOUNT ON; 
 SELECT 
          t.Status,k.HBarcode,
          te.UniqueName,
       COALESCE(e.Ename, e1.Ename) AS EName FROM    Transaction_tbl t
          left JOIN UserMaster_tbl u
              ON u.uid = t.PAICID
               left join EmployeeMaster_tbl e on e.ECode=u.Ecode
              AND t.Status = 0
          LEFT JOIN EmployeeMaster_tbl e1
              ON e1.ECode = t.ECode
              AND t.Status = 1 or e1.Ecode=t.DelEcode and t.Status=4
              left Join KHanger_tbl k
              on t.transactID=k.transactid
              and t.Status in(2,3)
              left JOIN Terminals_tbl te ON k.tid = te.tid  
         **Inner Join FetchKEY F on F.TbarCode = k.HBarcode**      
  WHERE   t.TBarcode = @Carid 
end
于 2013-07-24T10:14:16.523 回答