下面的文章主要介绍的是实现SQL Server传送数组参数变通实际操作办法,最近一直在做有关Dnn模块的开发这一项目,在其实际操作的过程中有碰到这么的一个问题,即需要同时插入N条数据,不想在程序里控制,但是SQL Sever又不支持数组参数。

“专业、务实、高效、创新、把客户的事当成自己的事”是我们每一个人一直以来坚持追求的企业文化。 创新互联建站是您可以信赖的网站建设服务商、专业的互联网服务提供商! 专注于网站建设、成都做网站、软件开发、设计服务业务。我们始终坚持以客户需求为导向,结合用户体验与视觉传达,提供有针对性的项目解决方案,提供专业性的建议,创新互联建站将不断地超越自我,追逐市场,引领市场!
所以只能用变通的办法了.利用SQL Server强大的字符串处理传把数组格式化为类似"1,2,3,4,5,6"。然后在存储过程中用SubString配合CharIndex把分割开来.
详细的存储过程
- CREATE PROCEDURE dbo.ProductListUpdateSpecialList
 - @ProductId_Array varChar(800),
 - @ModuleId int
 - AS
 - DECLARE @PointerPrev int
 - DECLARE @PointerCurr int
 - DECLARE @TId int
 - Set @PointerPrev=1
 - set @PointerCurr=1
 - begin transaction
 - Set NoCount ON
 - delete from ProductListSpecial where ModuleId=@ModuleId
 - Set @PointerCurr=CharIndex(',',@ProductId_Array,@PointerPrev+1)
 - set @TId=cast(SUBSTRING(@ProductId_Array,@PointerPrev,@PointerCurr-@PointerPrev) as int)
 - Insert into ProductListSpecial (ModuleId,ProductId) Values(@ModuleId,@TId)
 - SET @PointerPrev = @PointerCurr
 - while (@PointerPrev+1 < LEN(@ProductId_Array))
 - Begin
 - Set @PointerCurr=CharIndex(',',@ProductId_Array,@PointerPrev+1)
 - if(@PointerCurr> 0)
 - Begin
 - set @TId=cast(SUBSTRING(@ProductId_Array,@PointerPrev+1,@PointerCurr-@PointerPrev-1) as int)
 - Insert into ProductListSpecial (ModuleId,ProductId) Values(@ModuleId,@TId)
 - SET @PointerPrev = @PointerCurr
 - End
 - else
 - Break
 - End
 - set @TId=cast(SUBSTRING(@ProductId_Array,@PointerPrev+1,LEN(@ProductId_Array)-@PointerPrev) as int)
 - Insert into ProductListSpecial (ModuleId,ProductId) Values(@ModuleId,@TId)
 - Set NoCount OFF
 - if error=0
 - begin
 - commit transaction
 - end
 - else
 - begin
 - rollback transaction
 - end
 - GO
 
网友Bizlogic对此的改进方法:
给SQL Server传送数组参数的变通办法中应该用SQL2000 OpenXML更简单,效率更高,代码更可读:
- CREATE Procedure [dbo].[ProductListUpdateSpecialList]
 - (
 - @ProductId_Array NVARCHAR(2000),
 - @ModuleId INT
 - )
 - AS
 - delete from ProductListSpecial where ModuleId=@ModuleId
 - -- If empty, return
 - IF (@ProductId_Array IS NULL OR LEN(LTRIM(RTRIM(@ProductId_Array))) = 0)
 - RETURN
 - DECLARE @idoc int
 - EXEC sp_xml_preparedocument @idoc OUTPUT, @ProductId_Array
 - Insert into ProductListSpecial (ModuleId,ProductId)
 - Select
 - @ModuleId,C.[ProductId]
 - FROM
 - OPENXML(@idoc, '/Products/Product', 3)
 - with (ProductId int ) as C
 - where
 - C.[ProductId] is not null
 - EXEC sp_xml_removedocument @idoc
 
上述的相关内容就是对SQL Server传送数组参数的变通办法的描述,希望会给你带来一些帮助在此方面。
【编辑推荐】
                新闻名称:实现SQL Server传送数组参数变通的方案描述
                
                分享网址:http://www.csdahua.cn/qtweb/news7/215357.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网