使用拆分函数

时间:2015-10-16 13:27:14

标签: sql-server-2008

我尝试执行以下操作。但它给出错误“Subquery返回的值超过1。”

declare @filter varchar(100)='Moving,Idle,Stopped,Working'  
declare @filter2 varchar(100)='Init,suspend,Complete'    

create table #SPLITITEMS(value varchar(100),value1 varchar(100))
insert into #SPLITITEMS (value,value1)values((select val from dbo.split(@filter,',')),(select val from dbo.split(@filter2,',')))

以便我可以执行

select * from table1 where column1 in (select value from #SPLITITEMS)
select * from table2 where column1 in (select value1 from #SPLITITEMS) 

以获得更好的表现。

我该如何完成这项任务?

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助:

declare @Q1 nvarchar(max)
declare @filter varchar(100)='Moving,Idle,Stopped,Working' 
declare @filter2 varchar(100)='Init,suspend,Complete'    

Set @filter = '''' + replace(@filter, ',', ''',''') + ''''
Set @filter2 = '''' + replace(@filter2, ',', ''',''') + ''''

Set @Q1 = 'select * from table1 where column1 in (' + @filter + ')'

Exec (@Q1)