I am trying to run the following stored procedure.
CREATE PROCEDURE NWR.GRADER
@YEAR AS NVARCHAR(4)
AS
BEGIN
DECLARE @sql1 as nvarchar(500) =
N'select a.*, b.pts as W_GRD_PTS
into nwr.atp_matches_' + @YEAR + N'WGP
from nwr.atp_matches_' + @YEAR + N' a
left join NWR.RNK_VAL as b on a.winner_rank >= low
and a.winner_rank<= high
alter table nwr.atp_matches_' + @YEAR + N'WGP
add L_GRD_PTS smallint null
UPDATE nwr.atp_matches_' + @YEAR + N'WGP
SET L_GRD_PTS = C.pts
FROM NWR.RNK_VAL C
WHERE loser_rank >= LOW AND loser_rank <= HIGH;'
--print (@sql1);
EXEC sys.sp_execute @sql1;
end;
exec nwr.GRADER @year='2016';
However I'm getting the following error
Msg 214, Level 16, State 2, Procedure sp_execute, Line 1
Procedure expects parameter '@handle' of type 'int'.
But when I switch to the Print rather than EXEC I get the code exactly as I expected as below:
select a.*, b.pts as W_GRD_PTS
into nwr.atp_matches_2016WGP
from nwr.atp_matches_2016 a
left join NWR.RNK_VAL as b on a.winner_rank >= low and a.winner_rank <= high
alter table nwr.atp_matches_2016WGP
add L_GRD_PTS smallint null
UPDATE nwr.atp_matches_2016WGP
SET L_GRD_PTS=C.pts
FROM NWR.RNK_VAL C
WHERE loser_rank>= LOW AND loser_rank<= HIGH;
Can anyone explain what I'm doing wrong?
L_GRD_PTSwill be added to your table...... actually, this will only work once, and then fail.... don't do this! Keep you DDL separate from your other code!