Link to home
Start Free TrialLog in
Avatar of tommym121
tommym121Flag for Canada

asked on

SQL - Must declare the scalar variable "@ReturnVal".

This is the calling code
Use Test;

DECLARE @str nvarchar(max)
Exec sp_GetColummValueByIdx 'TmpUpdateTable', 'IID', 1, @str OUTPUT
Print '@str => ' + @str

This is the stored procedure
USE [Test]
GO
/****** Object:  StoredProcedure [dbo].[sp_GetColummValueByIdx]    Script Date: 05/24/2012 16:01:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_GetColummValueByIdx]
      -- Add the parameters for the stored procedure here
      @TableName varchar(max),      
      @ColumnName varchar(max),
      @idx int,
      @ReturnVal nvarchar(max) OUTPUT
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for procedure here
      --DECLARE @fmtString varchar(max)
      DECLARE @SQLQuery nvarchar(max)
      Set @SQLQuery =
            N'Select @ReturnVal=' +dbo.FormatStringForColumn(@TableName, @ColumnName)+ ' FROM ' + @TableName + ' WHERE idx='+CAST(@idx as varchar(max))
      
      Print '@SQLQuery -' + @SQLQuery
      EXEC (@SQLQuery)
        Print '@ReturnVal =>' + @ReturnVal
END

I got  the following result,  it complains (Must declare the scalar variable "@ReturnVal".)
Can anyone tell me why?


@SQLQuery -Select @ReturnVal= Cast(ISNULL(LTRIM(RTRIM('N'''+Replace([IID],'''','''''')+'''')),'NULL') as nvarchar(max)) FROM TmpUpdateTable WHERE idx=1
 
Msg 137, Level 15, State 1, Line 1
Must declare the scalar variable "@ReturnVal".
ASKER CERTIFIED SOLUTION
Avatar of dwkor
dwkor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tommym121

ASKER

Thanks.