I am struggling to make this logic work in my stored procedure. My logic is simple. I have two stored procedures called in another stored procedure (see code below).
The idea is to return a date variable or the count of the records based on the input provided and populate values in the main stored procedure variables
The procedures run fine but I come across the following problems:
dbo.GetLastDatedoesn't return a date valuedbo.usr_GetRecCountalways returns 0, even when there are records in the table
Main stored procedure:
....
Exec dbo.usr_GetLastDate @TableName, @LastTransDate
EXEC dbo.usr_GetRecCount @TableName, @RecCount
...
dbo.usr_GetLastDate:
ALTER PROCEDURE [dbo].[usr_GetLastDate]
-- Add the parameters for the stored procedure here
@TableName SYSNAME,
@OUTDATE date Output
AS
BEGIN
SET NOCOUNT ON;
DECLARE @SQLCommand NVARCHAR(MAX) =
N'SELECT @OUTDATE=MAX(TRANSDATE) FROM ' + QUOTENAME(@TableName);
EXECUTE [dbo].[sp_executesql]
@sqlCommand, N'@OUTDATE DATE OUTPUT', @OUTDATE=@OUTDATE OUTPUT;
END
`dbo.usr_GetRecCount`:
ALTER PROCEDURE [dbo].[usr_GetRecCount]
-- Add the parameters for the stored procedure here
@ViewName SYSNAME,
@OUTINT int output
AS
BEGIN
SET NOCOUNT ON;
DECLARE @SQLCommand NVARCHAR(MAX) =
N'SELECT @OUTINT=COUNT(SYMBOL) FROM ' + QUOTENAME(@ViewName);
EXECUTE [dbo].[sp_executesql]
@sqlCommand, N'@OUTINT INT OUTPUT', @OUTINT=@OUTINT OUTPUT;
END
FROMin your SQL statements - otherwise your statement will beSELECT COUNT(Symbol) FROMMytableand that might just not work ....OUTPUTwhen the stored procedure is executed.