I have a query as below, which executes as per requirement.
DECLARE @count varchar(20)
SET @count = (SELECT COUNT(*) FROM emp)
PRINT 'Employee Count: '+@count
SELECT @count
But, if I want to do the same with the use if dynamic SQL i am not getting my desired result. My dynamic SQL code is as below:
DECLARE @count varchar(10)
DECLARE @sqlQuery varchar(500)
SET @sqlQuery = 'SET '+@count +'= (SELECT COUNT(*) FROM emp)'
EXEC (@sqlQuery)
PRINT 'Employee Count: '+@count
SELECT @count
This code gives me a NULL as output.
What should be done? Where have I gone wrong?