Converted to a comma delimited string table involved in the query conditions IN

Return value '1,2,3,4,5,6,7', is a string, then use the IN query sql that this is a complete string, you need to be converted into the contents of the partition table

Function is defined as follows:

 

Function sysfStrToTable Create (@str VARCHAR (1000)) 
Returns the Table @tablename 
( 
str2table VARCHAR (50) 
) 
of As 
- This function is used to a plurality of data strings delimited by commas into a column of a table, for example, the string '1,2,3,4,5' becomes a table 
the Begin 
SET @str STR + = @ ',' 
the Declare @insertStr VARCHAR (50) - a first string taken 
Declare @newstr varchar (1000) - the interception of a string of the remaining string 
SET @insertStr = left (STR @, CHARINDEX ( ',', @ STR) -1) 
SET @newstr = Stuff (STR @,. 1, CHARINDEX ( ',' , @ STR), '') 
the Insert @tablename Values (@insertStr) 
the while (len (@newstr)> 0) 
the begin 
SET @insertStr = left (for newstr @, CHARINDEX ( ',', @ for newstr) -1) 
the Insert @ Values tableName (@insertStr) 
SET @newstr = Stuff (for newstr @,. 1,charindex(',',@newstr),'') 
end 
Return 
End

  

 transfer:

declare @str as varchar(100)
set @str='1,2,3,4,6'

select * from table1
where 
ID IN (SELECT * FROM  dbo.sysfStrToTable(@str) )

  

 

Guess you like

Origin www.cnblogs.com/crrc/p/11280490.html