34,593
社区成员
发帖
与我相关
我的任务
分享
if exists (select 1 from sysobjects where name='fn_list2tb)
drop function dbo.fn_list2tb
go
create function dbo.fn_list2tb (@lst varchar(512))
returns @tb table (id int) as
begin
declare @pos int
set @lst = replace(replace(@lst,' ',''),',,',',')
set @pos = charindex(',',@lst)
while @pos>0 and @lst>'' begin
if @pos>1
insert into @tb values (left(@lst,@pos-1))
set @lst = stuff(@lst,1,@pos,'')
set @pos = charindex(',',@lst)
end
if @lst>''
insert into @tb values (@lst)
return
end
go
select a.*
from Item a
join dbo.fn_lst2tb(@lst) b on a.itemId=b.id
declare @st nvarchar(2000)
select @st='35,36'
select * from Item where charindex(','+ltrim(ItemId)+',',','+@st+',')>0
select @st='''35''+'',36'''
exec('select * from Item where ItemId in '+@st)
declare @st nvarchar(2000)
select @st='35,36'
select * from Item where patindex('%,'+rtrim(ItemId)+',%',','+@st+',')>0
exec('select * from Item where ItemId in ('+@t+')')
select @st='''35''+'',36'''
exec('select * from Item where ItemId in '+@st)