sp_executesql的用法
sp_executesql介绍和使用
示例
create proc [dbo].[usp_Test]
@StartTime datetime
,@EndTime datetime
,@TestOutput int output
as
begin
--定义动态SQL和参数
declare @sql nvarchar(max), @pms nvarchar(max)
set @sql = N'
select
*
from
' + '[dbo].[tb_Test]' + '
where
UpdatedDateTime >= @StartTime
and UpdatedDateTime <= @EndTime
set @TestOutput = 123;
';
set @pms = N'@StartTime datetime, @EndTime datetime, @TestOutput int output';
exec sp_executesql @sql, @pms, @StartTime = @StartTime, @EndTime = @EndTime, @TestOutput = @TestOutput output
select @TestOutput --123
end