给SQL Server存储过程,传送数组参数的变通办法

 最近一直在做Dnn模块的开发,过程中碰到这么一个问题,需要同时插入N条数据,不想在程序里控制,但是SQL Sever又不支持数组参数.所以只能用变通的办法了.利用SQL Server强大的字符串处理传把数组格式化为类似"1,2,3,4,5,6"
 然后在存储过程中用SubString配合CharIndex把分割开来

详细的存储过程

给SQL Server存储过程,传送数组参数的变通办法CREATE PROCEDURE dbo.ProductListUpdateSpecialList
给SQL Server存储过程,传送数组参数的变通办法    
@ProductId_Array varChar(800),
给SQL Server存储过程,传送数组参数的变通办法    
@ModuleId int
给SQL Server存储过程,传送数组参数的变通办法
AS
给SQL Server存储过程,传送数组参数的变通办法    
DECLARE @PointerPrev int
给SQL Server存储过程,传送数组参数的变通办法    
DECLARE @PointerCurr int
给SQL Server存储过程,传送数组参数的变通办法    
DECLARE @TId int
给SQL Server存储过程,传送数组参数的变通办法    
Set @PointerPrev=1
给SQL Server存储过程,传送数组参数的变通办法    
set @PointerCurr=1
给SQL Server存储过程,传送数组参数的变通办法    
给SQL Server存储过程,传送数组参数的变通办法    
begin transaction
给SQL Server存储过程,传送数组参数的变通办法    
Set NoCount ON
给SQL Server存储过程,传送数组参数的变通办法    
delete  from ProductListSpecial where ModuleId=@ModuleId
给SQL Server存储过程,传送数组参数的变通办法    
给SQL Server存储过程,传送数组参数的变通办法    
Set @PointerCurr=CharIndex(',',@ProductId_Array,@PointerPrev+1)
给SQL Server存储过程,传送数组参数的变通办法    
set @TId=cast(SUBSTRING(@ProductId_Array,@PointerPrev,@PointerCurr-@PointerPrevas int)
给SQL Server存储过程,传送数组参数的变通办法    
Insert into ProductListSpecial (ModuleId,ProductId) Values(@ModuleId,@TId)
给SQL Server存储过程,传送数组参数的变通办法    
SET @PointerPrev = @PointerCurr
给SQL Server存储过程,传送数组参数的变通办法    
while (@PointerPrev+1 < LEN(@ProductId_Array))
给SQL Server存储过程,传送数组参数的变通办法    
Begin
给SQL Server存储过程,传送数组参数的变通办法        
Set @PointerCurr=CharIndex(',',@ProductId_Array,@PointerPrev+1)
给SQL Server存储过程,传送数组参数的变通办法        
if(@PointerCurr>0)
给SQL Server存储过程,传送数组参数的变通办法        
Begin
给SQL Server存储过程,传送数组参数的变通办法            
set @TId=cast(SUBSTRING(@ProductId_Array,@PointerPrev+1,@PointerCurr-@PointerPrev-1as int)
给SQL Server存储过程,传送数组参数的变通办法            
Insert into ProductListSpecial (ModuleId,ProductId) Values(@ModuleId,@TId)
给SQL Server存储过程,传送数组参数的变通办法            
SET @PointerPrev = @PointerCurr
给SQL Server存储过程,传送数组参数的变通办法        
End
给SQL Server存储过程,传送数组参数的变通办法        
else
给SQL Server存储过程,传送数组参数的变通办法            
Break
给SQL Server存储过程,传送数组参数的变通办法    
End
给SQL Server存储过程,传送数组参数的变通办法    
给SQL Server存储过程,传送数组参数的变通办法    
set @TId=cast(SUBSTRING(@ProductId_Array,@PointerPrev+1,LEN(@ProductId_Array)-@PointerPrevas int)
给SQL Server存储过程,传送数组参数的变通办法    
Insert into ProductListSpecial (ModuleId,ProductId) Values(@ModuleId,@TId)
给SQL Server存储过程,传送数组参数的变通办法    
Set NoCount OFF
给SQL Server存储过程,传送数组参数的变通办法    
if @@error=0
给SQL Server存储过程,传送数组参数的变通办法    
begin
给SQL Server存储过程,传送数组参数的变通办法        
commit transaction
给SQL Server存储过程,传送数组参数的变通办法    
end
给SQL Server存储过程,传送数组参数的变通办法    
else
给SQL Server存储过程,传送数组参数的变通办法    
begin
给SQL Server存储过程,传送数组参数的变通办法        
rollback transaction
给SQL Server存储过程,传送数组参数的变通办法    
end
给SQL Server存储过程,传送数组参数的变通办法
GO
给SQL Server存储过程,传送数组参数的变通办法





    本文转自无心之柳.NET博客园博客,原文链接http://www.cnblogs.com/9527/archive/2005/09/04/230000.html:,如需转载请自行联系原作者

上一篇:Mac book pro m1使用homebrew安装jmeter后查找jmeter.properties所在目录


下一篇:【1元试用邀测】脱胎于阿里双11实时大屏业务,5年披荆斩棘,阿里云实时计算为数据价值而战