I am trying to create a stored proc with a dynamic sql statement. What I am trying to accomplish is passing the count(*) to a integer variable. Below is my sql. The @iPul is returning NULL. How should I change this so I can get the value in @iPul?
Code:
SET @SQLFromWhere = ' FROM P LEFT Join S on P.HosID = S.site'
+ ' WHERE P.HosID = ' + @Site
+ ' AND P.Emailed = 1'
+ ' AND datepart(mm, P.EmailSent) <= ' + convert(varchar, @Month)
+ ' AND datepart(yyyy, P.EmailSent) = ' + convert(varchar, @Year)
+ ' AND P.ChartReviewCompleted = 1'
SET @command = 'SELECT ' + @sPul + ' = count(*)'
+ @SQLFromWhere --" + @iPul + " =
+ ' AND PulOx = 1'
EXEC (@command)
SET @iPul = convert(int, @sPul)