You say you DECLAREd @compl_failed, where did you do that? The script you posted doesn't have a DECLARE statement. You are ASSIGNING SUM(FAILED) to the variable @compl_failed, but the procedure doesn't know yet what @compl_failed is. It needs to be DECLAREd as an integer.
Unless I'm mistaken, it seems to me that you can't use the 'EXEC' function for dynamic SQL with variables. SQL treats the EXEC statement as it's own private batch so it can't see any variable declarations outside the specific dynamic SQL statement. I think you might be able to use 'sp_executesql' to execute dynamic SQL with variables but I'm no expert on dynamic SQL... hope it helps.
Note: I used different names for the variables in order to differentiate but you can use the same name for the sp_executesql parameter as is used in the SP. Terry L. Broadbent - DBA
SQL Server Page:
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.