给SQL Server传送数组参数的变通办法

作者:佚名 来源:qqcf.com 时间:2008-11-25 11:39: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

标签:
0
投稿

猜你喜欢

  • ASP中Utf-8与Gb2312编码转换乱码问题的解决方法 页面编码声明

    2012-11-30 20:45:55
  • Javascript 中对中文长度对行判断

    2009-07-05 18:39:00
  • ASP 三层架构 Convert类实现代码

    2011-03-16 11:01:00
  • FSO中的SubFolders 属性介绍

    2008-01-05 13:57:00
  • 巧用Dreamweaver制作复杂图像

    2010-09-02 12:34:00
  • response.setHeader()方法设置http文件头的值

    2010-03-11 22:43:00
  • 如何给eWebEditor编辑器加上运行代码框功能

    2007-09-25 07:02:00
  • IE6终极备忘单——策略

    2010-01-13 13:05:00
  • ASP怎样获得代码中第一张图片地址

    2008-10-29 09:40:00
  • 一个将半角"转换为中文"的asp函数

    2007-09-19 11:47:00
  • Christopher Schmitt 谈学习CSS的益处

    2008-07-13 14:15:00
  • sqlserver 文件数据库和关系数据库的比较

    2011-10-24 20:11:38
  • asp上传文件自动重命名方法

    2007-08-24 09:46:00
  • 看ASP程序源码的方法及工具

    2009-01-21 19:58:00
  • asp实现新评论自动发短信提示的代码

    2011-03-07 10:38:00
  • 什么是XML

    2008-09-05 17:21:00
  • 解析ASP与SQL server互操作的时间处理

    2008-05-17 11:57:00
  • Oracle PL/SQL入门案例实践

    2010-07-18 13:13:00
  • 如何实现论坛的树状记录表展开技术?

    2010-05-19 21:37:00
  • Oracle7.X 回滚表空间数据文件误删除处理方法

    2010-07-28 12:54:00
  • asp之家 网络编程 m.aspxhome.com