Ano*_*eek 4 sql-server stored-procedures
我有一个简单的存储过程,有参数
CREATE Procedure GetSupplierForTesting
(@SupplierId INT)
AS
SELECT SuppLabel
FROM Supplier
WHERE Supplier.SupplierId = @SupplierId
Run Code Online (Sandbox Code Playgroud)
我可以使用exec命令在另一个存储过程中调用它
exec GetSupplierForTesting @SupplierId = 10
Run Code Online (Sandbox Code Playgroud)
我发现了一篇文章,解释了如何sp_executesql更快exec.我的问题是我不知道如何调用具有参数的存储过程sp_executesql.我试过这段代码
DECLARE @SupplierId INT = 10;
EXEC sp_executesql N'GetSupplierForTesting', N'@SupplierId INT', @SupplierId
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
过程或函数'GetSupplierForTesting'需要参数'@SupplierId',这是未提供的
您需要的语法是
DECLARE @SupplierId INT = 10;
EXEC sys.sp_executesql N'GetSupplierForTesting @SupplierId=@SupplierId',
N'@SupplierId INT',
@SupplierId=@SupplierId
Run Code Online (Sandbox Code Playgroud)
但是不要这样做.这完全没有意义.通过使用sp_executesql基本上包装相同的exec语句并在不同的范围内执行它,没有任何神奇的性能提升.
只是用
exec dbo.GetSupplierForTesting @SupplierId = 10
Run Code Online (Sandbox Code Playgroud)