在存储过程中执行语句,out参数始终返回0

时间:2016-03-03 08:32:29

标签: sql sql-server tsql stored-procedures

我需要在表中插入一些值,为此我创建了一个存储过程。传递4个值。并且可以将两个值直接插入表中,对于另外两个值,需要找到ID。

我有三个存储过程。当我执行主存储过程时,我可以看到执行了两个被调用的存储过程并得到了正确的值。但是,此值不会传递给参数。

两个参数@uid和@did将0(零)返回到表中。

我做错了什么?

亲切的问候,

Clemens Linders

SP MES_D_GetUserID,传递名称并将ID作为字符串gat SP MES_D_GetDOrderID,传递一个名称,你得到一个ID为整数

主存储过程:

USE [AddOn_DEV_HE]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[MES_D_Consumed]
@WERKS nvarchar(4), @USERNAME nvarchar(50), @MACHID int, @DRINKORDER nvarchar(50)
WITH EXEC AS CALLER
AS
BEGIN
    SET NOCOUNT ON;
    Declare @uid AS varchar(10)
    Declare @did AS int
    Declare @OUTUID AS varchar(10)
    Declare @OUTDID AS int

    exec @uid = MES_D_GetUserID @USERNAME, @OUTUID  OUTPUT;
    exec @did = MES_D_GetDOrderID @DRINKORDER, @OUTDID OUTPUT;

    INSERT INTO Demo_D_Consumed (Werks, UserID, MachID, DrinkID, TimeDate) VALUES (@WERKS, @uid, @MACHID, @did, GETDATE());

END

and these are the two other stored procedures :
USE [AddOn_DEV_HE]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[MES_D_GetDOrderID]
@DRINK nvarchar(50), @OUTDID int OUTPUT
WITH EXEC AS CALLER
AS
BEGIN
    SET NOCOUNT ON;
    SELECT RecordNr FROM DEMO_D_ORDERS WHERE Drink = @DRINK
END



USE [AddOn_DEV_HE]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[MES_D_GetUserID]
@USERNAME nvarchar(50), @OUTUID nvarchar(50) OUTPUT
WITH EXEC AS CALLER
AS
BEGIN
    SET NOCOUNT ON;
    SELECT UserLan FROM sysUsernames WHERE UserName = @USERNAME

END

1 个答案:

答案 0 :(得分:0)

将它们更改为

ALTER PROCEDURE [dbo].[MES_D_GetDOrderID]
@DRINK nvarchar(50), @OUTDID int OUTPUT
WITH EXEC AS CALLER
AS
BEGIN
    SET NOCOUNT ON;
    SELECT @OUTDID = RecordNr FROM DEMO_D_ORDERS WHERE Drink = @DRINK
END

exec MES_D_GetDOrderID @DRINKORDER, @OUTDID OUTPUT;

您的@OUTDID将具有返回值。与其他SP相同。