SQL Server "array parameters"

Source: Internet
Author: User
If you encounter such a problem during the write process, you need to process n data records in the library. Program Loop causes multiple accesses to the database
SQL Sever does not support array parameters. We tried two methods. Here we take the note as follows (the processing of a single piece of data is omitted, and # temp is used to display the result)
1. Use replace Create Table # temp
(
SS varchar ( 200 ) Not Null
)
Declare @ STR varchar ( 200 )
Declare @ result varchar ( 1000 )
Set @ Str = ' Aaa, BB, C, D, E, ffffff '
Set @ Result = ' Insert into # temp (SS) Select ''' + Replace (@ STR, ' , ' , ''' Union select ''' ) + ''''
Exec (@ result)
Select * From # temp

2. Use charindex and substring

Create Table # temp
(
SS varchar ( 200 ) Not Null
)
Declare @ STR varchar ( 200 )
Declare @ curr Int
Declare @ Prev Int
Set @ Str = ' Aaa, BB, C, D, E, ffffff '
Set @ Curr = 1
Set @ Prev = 1
While @ Prev < Len (@ Str)
Begin
Set @ Curr = Charindex ( ' , ' , @ STR, @ PREV)
If @ Curr > @ Prev
Insert # temp select substring (@ STR, @ Prev, @ curr - @ PREV)
Else
Begin
Insert # temp select substring (@ STR, @ Prev, Len (@ Str) - @ Prev + 1 )
Break
End
Set @ Prev = @ Curr + 1
End
Select * From # temp
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.