我有一个使用IN运算符的Transact-SQL查询。像这样:
select * from myTable where myColumn in (1,2,3,4)
有没有一种方法来定义一个变量来保存整个列表“(1,2,3,4)”?我应该如何定义它?
declare @myList {data type}
set @myList = (1,2,3,4)
select * from myTable where myColumn in @myList
我有一个使用IN运算符的Transact-SQL查询。像这样:
select * from myTable where myColumn in (1,2,3,4)
有没有一种方法来定义一个变量来保存整个列表“(1,2,3,4)”?我应该如何定义它?
declare @myList {data type}
set @myList = (1,2,3,4)
select * from myTable where myColumn in @myList
Answers:
DECLARE @mylist TABLE (Id int)
INSERT INTO @mylist
SELECT id FROM (VALUES (1),(2),(3),(4),(5)) AS tbl(id)
SELECT * FROM Mytable WHERE theColumn IN (select id from @mylist)
[Err] 42000 - [SQL Server]Must declare the scalar variable "@mylist".
(VALUES (1),(2),(3),(4),(5))
直接使用吗?
有两种方法可以处理TSQL查询的动态csv列表:
1)使用内部选择
SELECT * FROM myTable WHERE myColumn in (SELECT id FROM myIdTable WHERE id > 10)
2)使用动态串联的TSQL
DECLARE @sql varchar(max)
declare @list varchar(256)
select @list = '1,2,3'
SELECT @sql = 'SELECT * FROM myTable WHERE myColumn in (' + @list + ')'
exec sp_executeSQL @sql
3)可能的第三个选择是表变量。如果您有SQl Server 2005,则可以使用表变量。如果在Sql Server 2008上,您甚至可以将整个表变量作为参数传递给存储过程,并在联接中或IN子句中用作子选择。
DECLARE @list TABLE (Id INT)
INSERT INTO @list(Id)
SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
SELECT
*
FROM
myTable
JOIN @list l ON myTable.myColumn = l.Id
SELECT
*
FROM
myTable
WHERE
myColumn IN (SELECT Id FROM @list)
使用如下函数:
CREATE function [dbo].[list_to_table] (@list varchar(4000))
returns @tab table (item varchar(100))
begin
if CHARINDEX(',',@list) = 0 or CHARINDEX(',',@list) is null
begin
insert into @tab (item) values (@list);
return;
end
declare @c_pos int;
declare @n_pos int;
declare @l_pos int;
set @c_pos = 0;
set @n_pos = CHARINDEX(',',@list,@c_pos);
while @n_pos > 0
begin
insert into @tab (item) values (SUBSTRING(@list,@c_pos+1,@n_pos - @c_pos-1));
set @c_pos = @n_pos;
set @l_pos = @n_pos;
set @n_pos = CHARINDEX(',',@list,@c_pos+1);
end;
insert into @tab (item) values (SUBSTRING(@list,@l_pos+1,4000));
return;
end;
您可以使用函数返回的表进行内部联接,而不是使用like:
select * from table_1 where id in ('a','b','c')
变成
select * from table_1 a inner join [dbo].[list_to_table] ('a,b,c') b on (a.id = b.item)
在未索引的1M记录表中,第二个版本花费了大约一半的时间...
干杯
@LukeH略有改进,无需重复“ INSERT INTO”:和@realPT的答案-无需进行SELECT:
DECLARE @MyList TABLE (Value INT)
INSERT INTO @MyList VALUES (1),(2),(3),(4)
SELECT * FROM MyTable
WHERE MyColumn IN (SELECT Value FROM @MyList)
我知道这已经很老了,但是TSQL => 2016,您可以使用STRING_SPLIT:
DECLARE @InList varchar(255) = 'This;Is;My;List';
WITH InList (Item) AS (
SELECT value FROM STRING_SPLIT(@InList, ';')
)
SELECT *
FROM [Table]
WHERE [Item] IN (SELECT Tag FROM InList)
从SQL2017开始,您可以使用STRING_SPLIT并执行以下操作:
declare @myList nvarchar(MAX)
set @myList = '1,2,3,4'
select * from myTable where myColumn in (select value from STRING_SPLIT(@myList,','))
正如以前没有人提到的那样,从Sql Server 2016开始,您还可以使用json数组和OPENJSON (Transact-SQL)
:
declare @filter nvarchar(max) = '[1,2]'
select *
from dbo.Test as t
where
exists (select * from openjson(@filter) as tt where tt.[value] = t.id)
您可以在中进行测试 sql fiddle demo
您还可以使用json轻松处理更复杂的情况-请参阅使用带有SQL变量的WHERE IN子句在SQL中搜索值和范围的列表?
这使用PATINDEX来将表中的ID与非数字定界整数列表进行匹配。
-- Given a string @myList containing character delimited integers
-- (supports any non digit delimiter)
DECLARE @myList VARCHAR(MAX) = '1,2,3,4,42'
SELECT * FROM [MyTable]
WHERE
-- When the Id is at the leftmost position
-- (nothing to its left and anything to its right after a non digit char)
PATINDEX(CAST([Id] AS VARCHAR)+'[^0-9]%', @myList)>0
OR
-- When the Id is at the rightmost position
-- (anything to its left before a non digit char and nothing to its right)
PATINDEX('%[^0-9]'+CAST([Id] AS VARCHAR), @myList)>0
OR
-- When the Id is between two delimiters
-- (anything to its left and right after two non digit chars)
PATINDEX('%[^0-9]'+CAST([Id] AS VARCHAR)+'[^0-9]%', @myList)>0
OR
-- When the Id is equal to the list
-- (if there is only one Id in the list)
CAST([Id] AS VARCHAR)=@myList
笔记:
DECLARE @StatusList varchar(MAX);
SET @StatusList='1,2,3,4';
DECLARE @Status SYS_INTEGERS;
INSERT INTO @Status
SELECT Value
FROM dbo.SYS_SPLITTOINTEGERS_FN(@StatusList, ',');
SELECT Value From @Status;
我认为您必须声明一个字符串,然后执行该SQL字符串。