I can’t get to the bottom of why my output variable in the code below is returning null. I’ve stepped through it in debug, and copy/ ran the sql statement, and it does return the expected values. But when I run it, just null. Any help would be great..
ALTER PROCEDURE [dbo].[spCountOfMostRecentByTableName] @table VARCHAR(120),
@count BIGINT OUTPUT
AS
BEGIN
DECLARE @parameters NVARCHAR(100)
SET @parameters = '@count BIGINT OUTPUT'
DECLARE @date DATE = dbo.MostRecentTradeDateForDataDownload()
DECLARE @sql NVARCHAR(MAX) = N'
SELECT COUNT(ID)
FROM ' + @table +
' WHERE DateAdded = ''' + CAST(@date AS CHAR) + '''';
EXEC sp_executesql @sql, @parameters, @count = @count OUTPUT
SELECT @count
END
You aren’t setting the @count variable anywhere.
Change
SELECT COUNT(ID)
to
SELECT @count = COUNT(ID)