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.