62,268
社区成员
发帖
与我相关
我的任务
分享--测试环境
declare @s varchar(100)
set @s='PO0901017;PO0901001'
declare @t table (z001 varchar(100))
insert into @t select 'PO0901001;PO0901002;PO0901018'
insert into @t select 'a;b;c'
--测试语句
declare @t1 table (z001 varchar(100))
declare @s1 varchar(1000)
set @s1=right(replace(';'+@s,';',''' as S union select '''),len(replace(';'+@s,';',''' as S union select '''))-12)+''''
insert into @t1 exec(@s1)
select distinct a.* from @t a,@t1 b where charindex(b.z001,a.z001)>0
go
/*
z001
-----------------------------
PO0901001;PO0901002;PO0901018
*/
--創建測試數據
create table k_za01 (z001 nvarchar(500))
insert into k_za01
select 'PO0901001;PO0901002;PO0901018'
union select 'PO0901001;PO0901023'
union select 'PO0901004;PO0901023'
union select 'PO0901023'
union select 'PO0901018;PO0901022'
union select 'PO0901022'
--創建存儲過程
create proc test
@str nvarchar(500)
as
begin
if(right(@str,1)<>';')
begin
set @str=@str+';'
end
while(len(@str)>0)
begin
declare @rr as table(tt nvarchar(20))
insert into @rr select left(@str,charindex(';',@str)-1)
set @str=right(@str,len(@str)-charindex(';',@str))
end
select * from k_za01 a where (select count(1) from @rr where a.z001 like '%'+tt+'%' )>0
end
--執行存儲過程
exec test 'PO0901023;PO0901022'
/*
z001
------------------------------------
PO0901001;PO0901023
PO0901004;PO0901023
PO0901018;PO0901022
PO0901022
PO0901023
(5 個資料列受到影響)
*/