It seems very simple solution, but I can't figure it out. Please help.
I have to call a stored proc with OUTPUT param from another stored proc. I think one of the issues is dynamic SQL, but I don't know how else to write it since @SQLWhere will change dynamically within C# code.
This is the proc being called from another proc:
ALTER PROCEDURE [dbo].[USP_RetrieveTotalRecord]
@SQLWhere AS NVARCHAR(1000),
@TotalRecordsFound as varchar(16) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @SQL AS NVARCHAR(Max)
SET @SQL='Select @TotalRecordsFound = Count(Table_ID)
From TableName Where ' + @SQLWhere
EXEC(@SQL)
return
END
Here is how I am calling it from another proc:
Declare @TotalRec AS NVARCHAR(16);
Declare @SQLWhere AS NVARCHAR(1000);
SET @SQLWhere='Date Between ''12/13/2016'' AND ''12/14/2016'''
EXECUTE USP_RetrieveTotalRecord @SQLWhere, @TotalRec output;
Here is the error I am trying to resolve:
Msg 137, Level 15, State 1, Line 30
Must declare the scalar variable "@TotalRecordsFound".