In the SQL Server Stored Procedure, pass the in parameter a comma value, for example, & #39; 1 & #3

Source: Internet
Author: User
Declare @ areavarchar (120); set @ area01 +, + 02; selectschoolnamefromschool_infowherecodein (@ area), but the result cannot be obtained. I think it may be that the @ area assignment method is incorrect, but I have tried many ways. Try to make up a dynamic SQL statement and execute it using exec.

Declare @ area varchar (120); set @ area = '01 '+', '+ '02'; select schoolname from school_info where code in (@ area ), but cannot get the results. I think it may be that the @ area assignment method is incorrect, but I have tried many ways. Try to make up a dynamic SQL statement and execute it using exec.

Declare @ area varchar (120 );
Set @ area = '01' + ',' + '02 ';
Select schoolname from school_info where code in (@ area), but the results cannot be obtained.

I think it may be that the @ area assignment method is incorrect, but I have tried many ways.SolutionNone. Try to make up a dynamic SQL statement and execute it using exec. There is no problem and the result can be obtained, but I always feelThis wayIt's a little strange, not an ideal solution.Solution.

I finally found a solution on the Internet.SolutionAfter the test, there is no problem and it is very convenient to use. Create a function. After the function is created, it is listed under the function's table value function. The statement is as follows:

Create function [dbo]. [f_split] (@ c varchar (2000), @ split varchar (2 ))
Returns @ t table (col varchar (20 ))
As
Begin
While (charindex (@ split, @ c) <> 0)
Begin
Insert @ t (col) values (substring (@ c, 1, charindex (@ split, @ c)-1 ))
Set @ c = stuff (@ c, 1, charindex (@ split, @ c ),'')
End
Insert @ t (col) values (@ c)
Return
End

The statement is as follows:

Select schoolname from school_info where code in (select col from [dbo]. [f_split] (@ area ,','))

This wayRun again Storage ProcessTo pass in CommaValue Parameters.

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.