How to transmit array parameters to SQL Server

Source: Internet
Author: User
I have been developing the dnn module recently. I encountered such a problem that I need to insert n data records at the same time. Program But SQL Sever does not support array parameters. Therefore, you can only use a work und. use SQL Server's powerful string processing to format the array as "1, 2, 3, 4, 5, 6 ". In the stored procedure, use substring and charindex to separate them.

Detailed stored procedures

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 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 should be simpler and more efficient,CodeMore readable:

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.