为SQL Server数据库传数组参数的变通办法

来源:asp之家 时间:2009-10-23 09:26:00 

最近一直在做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对此的改进方法:

应该用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

标签:SQLServer,数据库
0
投稿

猜你喜欢

  • SQL触发器在保持数据库完整性中的实际应用

    2009-10-23 14:34:00
  • mysql5.58的编译安装

    2011-01-29 16:26:00
  • 新兴XML处理方法VTD-XML介绍

    2008-09-04 14:42:00
  • Persits AspJpeg 1.8+ 轻松实现透明文字去锯齿水印

    2009-03-20 14:03:00
  • 交互设计实用指南系列(5) – 突出重点,一目了然

    2010-01-11 21:05:00
  • 省市级联菜单的可用性

    2009-02-24 16:32:00
  • 无图片CSS圆角的五个实例

    2008-08-02 12:18:00
  • rs.open sql,conn,1,1与rs.open sql,conn,1.3还有rs.open sql,conn,3,2区别

    2011-02-24 10:49:00
  • 深入理解ASP中FSO的神奇功能

    2007-09-18 12:22:00
  • 自动更新程序的设计框架

    2009-08-12 13:00:00
  • 通过XSL转换XML文件步骤

    2008-01-27 16:03:00
  • SQL Server和MySql中创建临时表

    2008-06-19 13:31:00
  • 解读HTML:大厦的基石

    2008-12-01 12:57:00
  • 有效防止ASP木马上传运行—小知识[网络安全技术]

    2011-03-06 11:15:00
  • 由浅入深漫谈margin属性

    2007-05-11 17:03:00
  • 讲解SQL Server数据库触发器的安全隐患

    2009-02-24 17:46:00
  • Sql Server 2005读取外部数据的方法

    2008-07-08 19:08:00
  • ASP之对象总结

    2008-02-27 13:18:00
  • Ubuntu下设置mysql自动备份

    2010-10-25 20:25:00
  • asp如何用FSO对象显示一个文本文件?

    2010-06-09 18:41:00
  • asp之家 网络编程 m.aspxhome.com