很久没有写存储过程了,因为存储过程违背OOP精神,话说带了参数的存储过程,如果业务需求改变,改存储过程还真的挺麻烦的。所以现在比较倾向于负责的业务逻辑在业务层去处理,业务层专门做业务层的事情。
通过一层巧妙地包装,动态执行SQL语句就解决了
ALTER procedure [dbo].[PROC_NextSteps] ( @Count 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=(‘[email protected]+‘)‘ 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 @[email protected]+dbo.F_SplitOfIndex(@IDS,‘|‘,@index)+‘|‘ end set @[email protected]+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
时间: 2024-12-06 22:59:33