存储过程的结果将另一个存储过程另存为参数

时间:2017-11-28 15:56:15

标签: sql-server stored-procedures

我想将一个存储过程的结果保存到另一个存储过程中, 但它总是节省0.

我的程序是:

Create Proc AddToBill
    @BillID INT,
    @CustomerID INT,
as
begin
    exec @BillID = BillID_Counter;

    INSERT INTO [dbo].[FullBillDetails] ([BillID], [CustomerID])
    VALUES (@BillID, @CustomerID INT)
end

此过程BillID_Counter仅返回一个整数。

Their is similar question but it has to many parameter and difficult to understand my Question is related to single parameter.

1 个答案:

答案 0 :(得分:1)

我们需要查看BillID_Counter sp的代码,但我可以打赌sp正在执行SELECT,您正试图获取该变量。实际上发生的是,你给@BillID sp的返回码(0表示“成功执行”。

您可以尝试这样做:

CREATE PROC AddToBill @BillID INT, @CustomerID INT,
AS
BEGIN

CREATE TABLE #BillID(BillID INT);

INSERT INTO #BillID
EXEC BillID_Counter;

INSERT INTO [dbo].[FullBillDetails]([BillID],[CustomerID])
SELECT BillID, @CustomerID
FROM #BillID;

END