emd
emd

Reputation: 1223

Stored procedure and function returning unexpected results

Using stored procedure output for a parameter of a function

DECLARE @SeqNo int; 
DECLARE @CharNumber varchar(10);
EXEC  dbo.sp_GetNextCounter 'ITEMTYPE', @SeqNo OUTPUT;
EXEC @CharNumber=dbo.rf_f_CIntToChar @SeqNo, 6;
SELECT @CharNumber;

The stored procedure sp_GetNextCounter has an integer output called SeqNo.

The stored procedure:

@param0 varchar(12),
@SeqNo INT OUTPUT
AS
BEGIN
IF (@param0 IS NOT NULL)
BEGIN
    set nocount on
    DECLARE @reqseqno INT
    SELECT @reqseqno = CounterValue FROM Counters WHERE CounterName = @param0
    UPDATE Counters SET CounterValue = @reqseqno + 1 WHERE CounterName = @param0
    SELECT @reqseqno AS 'SeqNo'
END
END
GO

The function dbo.rf_f_CIntToChar takes two parameters (integer to convert, number of characters to return) returns a varchar version of the integer with leading zeros.

The function:

(@intVal int, @intLen int)
RETURNS varchar(10)
WITH EXEC AS CALLER
AS
BEGIN
IF @intlen > 20 SET @intlen = 20
IF @intlen < LEN(@intVal) RETURN RIGHT(CONVERT(varchar(10), @intVal), @intlen)
RETURN REPLICATE('0', @intLen - LEN(@intVal)) + CONVERT(varchar(10), @intVal)
END
GO

Both the stored procedure and the function operate as they should when used separately.

Why does the above query return SeqNo rather than CharNumber?

Upvotes: 2

Views: 325

Answers (1)

Martin Smith
Martin Smith

Reputation: 453243

Your stored procedure never assigns a value to the output parameter @SeqNo

Upvotes: 4

Related Questions