获取一个存储过程值到另一个存储过程的结果

时间:2013-07-24 09:03:52

标签: sql sql-server

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

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
-----------------------------------

我有另一个存储过程来根据状态获取keylocation:

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)

有没有办法做到这一点?我是存储过程中的新手。有没有办法创建函数并将此结果调用到我的存储过程中,如果有人知道请帮帮我

1 个答案:

答案 0 :(得分:0)

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

请参阅下面的案例。我不确定应该加入什么,但是如果你看起来并理解它是如何写的,你肯定会得到这个想法。

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

所以你的商店程序就像这样

 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