如果SQL根本不以这种方式工作,我不会感到惊讶,但是:
如果在查询中运行两个SELECT语句,则会得到一个拆分的“结果”窗格。我想知道是否可以将变量添加到列表中,然后使结果窗格拆分的数量与该列表的长度匹配。
如果我要混合语言:
id_list = [26275, 54374, 84567]
for i in id_list:
SELECT * FROM table WHERE id = i
我只是试图轻松地比较查询结果,同时保留不同的组,并且变量数量不断变化。由于循环似乎从来都不是SQL的答案,因此我很乐意插入空白行或水平线等内容。尽管不确定是否可行...
在T-SQL中没有“列表”(作为单独的数据结构)的概念。这是您想要的吗?
SELECT *
FROM table
WHERE id IN (26275, 54374, 84567);
declare @i int = 0;
declare @Id int;
declare @Ids table (Id int);
insert @Ids select Id from (values (26275), (54374), (84567)) t(Id);
declare @Count int = (select count(*) from @Ids);
while @i < @Count
begin
select @Id = Id, @i = @i + 1
from @Ids order by Id
offset @i rows fetch next 1 rows only;
select * from dbo.MyTable where Id = @Id;
end