Hi I'm running a stored procedure that executes other stored procedure based on what's needed. My issue is that sometimes some of the stored procedures fail come back with an error and stop the whole code.[code="sql"]DECLARE @sp varchar(80)DECLARE @cnt INT = 1DECLARE @RunID INTSET @RunID = (SELECT MAX(RunID)+1 as RunID FROM [RISE_DATA].[dbo].[tb_CompaniesToRun])------------------------- EXECUTE ALL THE STORED PROCEDURES THAT NEED TO RUN -------------------WHILE @cnt < (SELECT DISTINCT MAX(Target_ID)+1 as SPs FROM [RISE_DATA].[dbo].[tb_CompaniesToRun] WHERE RunID = @RunID)BEGIN SET @sp = '[RISE].[dbo].' SET @sp = (SELECT DISTINCT @sp + [Target_StoredProcName] as SPs FROM [RISE_DATA].[dbo].[tb_CompaniesToRun] WHERE Target_ID = @cnt AND RunID = @RunID) EXEC (@sp) SET @cnt = @cnt + 1END[/code]I googled this and found that putting a GO in between executions can solve this issue, but when I change my code with the Go it didn't work at all.So I tried testing some ideas but all failed.This code does exactly what I need[code="sql"]EXEC [RISE_DATA].[dbo].[sp1] GOEXEC [RISE_DATA].[dbo].[sp2] GOEXEC [RISE_DATA].[dbo].[sp3] GOEXEC [RISE_DATA].[dbo].[sp4] GOEXEC [RISE_DATA].[dbo].[sp5] GO[/code]But if I try to replicate that in code it comes back with an error[code="sql"]DECLARE @a nvarchar(888)SET @a = N'EXEC [RISE_DATA].[dbo].[sp1] ' + CHAR(10) + CHAR(13) + 'GO' + CHAR(10) + CHAR(13) + 'EXEC [RISE_DATA].[dbo].[sp2] ' + CHAR(10) + CHAR(13) + 'GO' + CHAR(10) + CHAR(13) + 'EXEC [RISE_DATA].[dbo].[sp3] ' + CHAR(10) + CHAR(13) + 'GO' + CHAR(10) + CHAR(13) + 'EXEC [RISE_DATA].[dbo].[sp4] ' + CHAR(10) + CHAR(13) + 'GO' + CHAR(10) + CHAR(13) + 'EXEC [RISE_DATA].[dbo].[sp5] ' + CHAR(10) + CHAR(13) + 'GO' + CHAR(10) + CHAR(13) --PRINT @aEXEC sp_executesql @a[/code]the error is:Msg 8146, Level 16, State 1, Procedure sp1, Line 2Procedure sp1 has no parameters and arguments were supplied.Msg 8146, Level 16, State 1, Procedure sp2, Line 2Procedure sp2 has no parameters and arguments were supplied.Msg 8146, Level 16, State 1, Procedure sp3, Line 2Procedure sp3 has no parameters and arguments were supplied.Msg 8146, Level 16, State 1, Procedure sp4, Line 2Procedure sp4 has no parameters and arguments were supplied.Msg 8146, Level 16, State 1, Procedure sp5, Line 2Procedure sp5 has no parameters and arguments were supplied.I think that it thinks about the 'GO' as a parameter. So my question is how can I avoid that?Or how can I make my code ignore the failed procedures and move on to the next one?Thanks for any suggestions
↧