存储过程中动态执行SQL并获取返回结果
时间:2021-07-01 10:21:17
帮助过:6人阅读
int,
@IDS NVARCHAR(500),
@Rules NVARCHAR(MAX),
@IDSOut NVARCHAR(500) OUT
)
as
BEGIN
set @Rules=
UPPER(@Rules);
set @IDSOut=
‘‘
declare @index int
set @index=
1;
WHILE(@index<=
@Count)
BEGIN
DECLARE @SplitRule NVARCHAR(MAX),@Value int
select @SplitRule=dbo.F_SplitOfIndex(@Rules,
‘|‘,@index)
if(CHARINDEX(
‘SELECT‘,@SplitRule)=
1)
begin
declare @sql nvarchar(1000)
select @sql=N
‘select @Value=(‘+@SplitRule+
‘)‘
exec sp_executesql @sql, N‘@Value int out‘,@Value
out
end
ELSE IF(CHARINDEX(‘PROC_‘,@SplitRule)=
1)
BEGIN
exec @Value =
@SplitRule
END
ELSE
BEGIN
set @Value =
@SplitRule
END
if(@Value>=
1)
begin
select @IDSOut=@IDSOut+dbo.F_SplitOfIndex(@IDS,
‘|‘,@index)+
‘|‘
end
set @index=@index+
1
end
if(@IDSOut<>
‘‘)
begin
set @IDSOut=LEFT(@IDSOut,LEN(@IDSOut)-
1)
end
END
declare @res nvarchar(1000)
EXEC [dbo].[proc_nextsteps] 3,‘id1|id2|id3‘,‘SELECT COUNT(1) FROM dbo.A_CDKey|PROC_B|0‘,@res OUT
print @res
存储过程中动态执行SQL并获取返回结果
标签: