1.创建一个函数:将用逗号分隔的字符串变成一个表的一列,这样就可以运用在select 语句的in中查询

  

create Function StrToTable(@str varchar(1000)) 
Returns @tableName Table 
( 
str2table varchar(50) 
) 
As 
--该函数用于把一个用逗号分隔的多个数据字符串变成一个表的一列,例如字符串'1,2,3,4,5' 将编程一个表,这个表 
Begin 
set @str = @str+',' 
Declare @insertStr varchar(50) --截取后的第一个字符串 
Declare @newstr varchar(1000) --截取第一个字符串后剩余的字符串 
set @insertStr = left(@str,charindex(',',@str)-1) 
set @newstr = stuff(@str,1,charindex(',',@str),'') 
Insert @tableName Values(@insertStr) 
while(len(@newstr)>0) 
begin 
set @insertStr = left(@newstr,charindex(',',@newstr)-1) 
Insert @tableName Values(@insertStr) 
set @newstr = stuff(@newstr,1,charindex(',',@newstr),'') 
end 
Return 
End

--使用方法
--select * from StrToTable('1,3,4')

 

相关文章:

  • 2022-01-17
  • 2021-06-25
  • 2021-12-20
  • 2022-12-23
  • 2021-06-02
  • 2021-12-01
  • 2022-01-05
  • 2021-06-28
猜你喜欢
  • 2022-03-03
  • 2022-12-23
  • 2021-08-14
  • 2021-06-28
  • 2022-02-01
  • 2021-07-24
  • 2021-10-25
相关资源
相似解决方案