为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
投稿

猜你喜欢

  • Pandas中根据条件替换列中的值的四种方式

    2022-07-25 00:27:38
  • asp如何写入超长的字符串?

    2010-06-09 18:53:00
  • Python正则表达re模块之findall()函数详解

    2021-04-20 05:00:16
  • GitHub Eclipse配置使用教程详解

    2023-11-04 10:23:02
  • SQLServer 2008助你轻松编写T-SQL存储过程

    2010-12-06 13:38:00
  • JQuery获取表单值

    2009-11-19 13:17:00
  • mysql如何查询两个日期之间最大的连续登录天数

    2024-01-14 09:26:22
  • 利用django model save方法对未更改的字段依然进行了保存

    2021-12-21 21:24:18
  • GoLang函数栈的使用详细讲解

    2024-03-16 21:06:21
  • python3 xpath和requests应用详解

    2022-10-13 18:24:36
  • Vue.js实现微信过渡动画左右切换效果

    2023-07-02 17:03:10
  • python读取xml文件方法解析

    2021-04-25 03:53:45
  • python 通过SMSActivateAPI 获取验证码的步骤

    2022-04-23 15:16:43
  • go开发中引用静态库.a文件的方法

    2024-04-25 13:16:26
  • MySQL InnoDB和MyISAM数据引擎的差别分析

    2024-01-25 20:04:43
  • 不管你的Python报什么错,用这个模块就能正常运行

    2023-08-29 14:42:43
  • CTF中的PHP特性函数解析之中篇

    2023-06-11 12:56:20
  • PHP用PDO如何封装简单易用的DB类详解

    2023-11-23 16:05:39
  • python traceback捕获并打印异常的方法

    2022-11-06 10:35:30
  • python实现简单socket程序在两台电脑之间传输消息的方法

    2021-04-25 21:14:26
  • asp之家 网络编程 m.aspxhome.com