简体   繁体   中英

Executing a statement within a stored procedure, out parameter always returns 0

I need to insert some values into a table and to do this I created a stored procedure. 4 values are passed. And two values can be inserted straight into the table, for two other values an ID needs to be found.

I have three stored procedures. When I execute the main stored procedure, I can see that the two called stored procedures are executed and come up with the correct value. However this value is not passed into the parameter.

Both parameters @uid and @did retrun 0 (zero) into the table.

What am I doing wrong??

Kind regards,

Clemens Linders

SP MES_D_GetUserID, Pass a name and you gat an ID as string SP MES_D_GetDOrderID, Pass a name and you get an ID as integer

The main stored procedure:

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

Change them to be

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

And

exec MES_D_GetDOrderID @DRINKORDER, @OUTDID OUTPUT;

Your @OUTDID will have the return value. Same with the other SP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM