我应该使用sp_executesql还是EXEC来运行存储过程?

时间:2020-11-26 16:30:35

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:

我有一个存储过程需要多次调用第二个SP。唯一改变的是第二个SP的参数。像这样的东西:

 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?

现在如果它是动态SQL我运行我知道sp_executesql会比EXEC更好但是因为我实际上在SP中多次调用我应该仍然使用sp_executesql或者如上所示的EXEC一样好吗?

Thanks for any help.

谢谢你的帮助。

1 个解决方案

#1


2  

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

像上面的EXEC storedprocname一样使用EXEC

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

sp_executesql通常比EXEC(@sqlstring)更好,因为你有动态SQL,而不是存储过程。因此,因为您正在调用存储过程,所以您拥有的语法是最好的方法

#1


2  

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

像上面的EXEC storedprocname一样使用EXEC

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

sp_executesql通常比EXEC(@sqlstring)更好,因为你有动态SQL,而不是存储过程。因此,因为您正在调用存储过程,所以您拥有的语法是最好的方法