I have some code in a stored procedure that looks like this:
SELECT @SQL = 'GRANT SELECT, UPDATE, INSERT, DELETE ON' + @TABLENAME + ' TO ' @USER + ' GO'
@TABLENAME has previously been created and is named as 'PCT_SALES_DISTREF_' + @USER. @USER is provided as an input parameter to the stored procedure. When I run the procedure I get the error message on the attached file.
SQL is looking at the GRANT statement as a stored procedure. The problem is in the Dynamic SQL, because if I just hardcode that line as
GRANT SELECT, UPDATE, INSERT, DELETE ON PCT_SALES_DISTREF_test TO test, it runs without error. Can you see what the problem may be in my Dynamic SQL code?