Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use sp_executesql or EXEC to run a stored procedure?

I have a stored procedure that needs to call a 2nd SP multiple times. The only thing that changes are the parameters to the 2nd SP. Something like this:

 SELECT @P1=5, @P2=5
 EXEC MyProc @P1, @P2

 SELECT @P1=0, @P2=1
 EXEC MyProc @P1, @P2

Now if it was dynamic SQL I was running I know sp_executesql would be better than EXEC but since what I'm calling multiple times in actually a SP should I still use sp_executesql or is EXEC like shown above just as good?

Thanks for any help.

like image 380
user169867 Avatar asked Nov 05 '22 08:11

user169867


1 Answers

Use EXEC like you have above which is the form EXEC storedprocname

sp_executesql is better than EXEC (@sqlstring) generally where you have dynamic SQL, not a stored proc. So because you're calling a stored proc, the syntax you have is the best way

like image 85
gbn Avatar answered Nov 09 '22 17:11

gbn