I am selecting the COUNT of something in a SQL Server 2016 database using a dynamic SQL query and want that value placed into a variable. Below is the SQL statement which I was sure was correct, but the value I'm getting is NULL (I have tested the query and it returns a value). Any tips?
DECLARE @Output INT
SELECT @SqlCommand = 'SELECT COUNT(ServerName) FROM ' + @TableReference + ' WITH (NOLOCK) WHERE ServerName = ''' + @PackageEndPoint + ''''
EXEC sp_executesql @SqlCommand, N'@Output INT OUTPUT', @Output = @Output OUTPUT
SELECT @StagingRecordCount = @Output