How to pass an array to the SQL Server Stored Procedure

Source: Internet
Author: User

Recently writtenProgramIt is required to pass a string array or set to the stored procedure and then use not in for condition judgment.

After careful research, I found a solution to this problem on csdn. I will share it with you.

The general method is to transmit a long string to the stored procedure in the form of a long string. Because sqlserver does not have the splite Function

Therefore, you must define a splite function for processing.

 

Customize a function
Create Function f_splitstr (@ sourcesql varchar (8000), @ strseprate varchar (10 ))
Returns @ temp table (F1. varchar (100 ))
As
Begin
Declare @ I int
Set @ sourcesql = rtrim (ltrim (@ sourcesql ))
Set @ I = charindex (@ strseprate, @ sourcesql)
While @ I> = 1
Begin
Insert @ temp values (left (@ sourcesql, @ i-1 ))
Set @ sourcesql = substring (@ sourcesql, @ I + 1, Len (@ sourcesql)-@ I)
Set @ I = charindex (@ strseprate, @ sourcesql)
End
If @ sourcesql <>''
Insert @ temp values (@ sourcesql)
Return
End
-Execution
Select * From DBO. f_splitstr ('1, 2, 3, 4 ',',')

 

Note: '1, 2, 3, 4 'is the string you pass

You can also use select cunt (*) from DBO. f_splitstr ('1, 2, 3, 4 ',',')

Returns the length of the string array.

If you want to delete this function, use

-- Delete a function
Drop function fsplit

**************************************** *********************************

Another method is to take full advantage of the powerful string function of SQL Server.

However, because the exec command is used, it can only be implemented in the stored procedure.

Create procedure f_splitstr (@ sourcesql varchar (8000), @ strseprate varchar (10 ))
As

Create Table # temp (F1. varchar (100 ))
Declare @ tmpsql varchar (8000)

Select @ tmpsql = 'insert into # temp values ('+ Replace (@ sourcesql, @ strseprate ,')
Insert into # temp values (') + ')'

Exec (@ tmpsql)

Select * from # temp

Drop table # temp
Go

F_splitstr4 '1, 2, 3, 4 ',','

The results are the same.

 

 

 

 

1

2

3

4

5

6

7

8

8

9

9

 

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.