尝试在 SQL WHERE IN 语句中使用变量
Trying to use a variable in a SQL WHERE IN statement
我正在尝试使用 @List Table 来提供 Where IN 语句。我一直收到错误消息“必须声明标量变量“@List”。
我知道我可以简单地在 WHERE IN 部分使用 select 语句,但我需要从概念上找出这是否可能用于更大的 SQL 语句,我可以'不分享全部。
DECLARE @EarliestDate date Set @EarliestDate = '2020-01-01'
DECLARE @LatestDate date Set @LatestDate = '2020-01-05'
DECLARE @List TABLE(DATAPOINT varchar(20))
INSERT INTO @List SELECT number1 + CONVERT(varchar(5),number2)
FROM
tbl1
INNER JOIN msts tb2 on tbl1.j = tbl2.j and tbl1.s = tbl2.s
INNER JOIN tbl3 on tbl3.r = tbl1.j and tbl3.re = tbl1.s
AND tbl2.date > @EarliestDate
AND tbl2.date < @LatestDate
SELECT number3
, number4
FROM table1 ci
WHERE 1=1
AND ci.number1 + CONVERT(varchar(5),ci.number2) IN (**@List**)
要正确使用 in()
,您必须指定一个查询,而不仅仅是将您声明并插入数据的 table 变量放入。
示例:
DECLARE @EarliestDate date Set @EarliestDate = '2020-01-01'
DECLARE @LatestDate date Set @LatestDate = '2020-01-05'
DECLARE @List TABLE(DATAPOINT varchar(20))
INSERT INTO @List SELECT number1 + CONVERT(varchar(5),number2)
FROM
tbl1
INNER JOIN msts tb2 on tbl1.j = tbl2.j and tbl1.s = tbl2.s
INNER JOIN tbl3 on tbl3.r = tbl1.j and tbl3.re = tbl1.s
AND tbl2.date > @EarliestDate
AND tbl2.date < @LatestDate
SELECT number3
, number4
FROM table1 ci
WHERE 1=1
AND ci.number1 + CONVERT(varchar(5),ci.number2) IN (select DATAPOINT from @List)
为了完整起见,您还可以使用 CTE 解决此问题——有时速度更快。我在这里也使用了连接,因为有时也可以让编译器优化。
DECLARE @EarliestDate date Set @EarliestDate = '2020-01-01';
DECLARE @LatestDate date Set @LatestDate = '2020-01-05';
WITH list AS
(
SELECT number1 + CONVERT(varchar(5),number2) as DATAPOINT
FROM tbl1
JOIN msts tb2 on tbl1.j = tbl2.j and tbl1.s = tbl2.s
JOIN tbl3 on tbl3.r = tbl1.j and tbl3.re = tbl1.s
AND tbl2.date > @EarliestDate
AND tbl2.date < @LatestDate
)
SELECT number3, number4
FROM table1 ci
JOIN list on ci.number1 + CONVERT(varchar(5),ci.number2) = DATAPOINT ;
我正在尝试使用 @List Table 来提供 Where IN 语句。我一直收到错误消息“必须声明标量变量“@List”。
我知道我可以简单地在 WHERE IN 部分使用 select 语句,但我需要从概念上找出这是否可能用于更大的 SQL 语句,我可以'不分享全部。
DECLARE @EarliestDate date Set @EarliestDate = '2020-01-01'
DECLARE @LatestDate date Set @LatestDate = '2020-01-05'
DECLARE @List TABLE(DATAPOINT varchar(20))
INSERT INTO @List SELECT number1 + CONVERT(varchar(5),number2)
FROM
tbl1
INNER JOIN msts tb2 on tbl1.j = tbl2.j and tbl1.s = tbl2.s
INNER JOIN tbl3 on tbl3.r = tbl1.j and tbl3.re = tbl1.s
AND tbl2.date > @EarliestDate
AND tbl2.date < @LatestDate
SELECT number3
, number4
FROM table1 ci
WHERE 1=1
AND ci.number1 + CONVERT(varchar(5),ci.number2) IN (**@List**)
要正确使用 in()
,您必须指定一个查询,而不仅仅是将您声明并插入数据的 table 变量放入。
示例:
DECLARE @EarliestDate date Set @EarliestDate = '2020-01-01'
DECLARE @LatestDate date Set @LatestDate = '2020-01-05'
DECLARE @List TABLE(DATAPOINT varchar(20))
INSERT INTO @List SELECT number1 + CONVERT(varchar(5),number2)
FROM
tbl1
INNER JOIN msts tb2 on tbl1.j = tbl2.j and tbl1.s = tbl2.s
INNER JOIN tbl3 on tbl3.r = tbl1.j and tbl3.re = tbl1.s
AND tbl2.date > @EarliestDate
AND tbl2.date < @LatestDate
SELECT number3
, number4
FROM table1 ci
WHERE 1=1
AND ci.number1 + CONVERT(varchar(5),ci.number2) IN (select DATAPOINT from @List)
为了完整起见,您还可以使用 CTE 解决此问题——有时速度更快。我在这里也使用了连接,因为有时也可以让编译器优化。
DECLARE @EarliestDate date Set @EarliestDate = '2020-01-01';
DECLARE @LatestDate date Set @LatestDate = '2020-01-05';
WITH list AS
(
SELECT number1 + CONVERT(varchar(5),number2) as DATAPOINT
FROM tbl1
JOIN msts tb2 on tbl1.j = tbl2.j and tbl1.s = tbl2.s
JOIN tbl3 on tbl3.r = tbl1.j and tbl3.re = tbl1.s
AND tbl2.date > @EarliestDate
AND tbl2.date < @LatestDate
)
SELECT number3, number4
FROM table1 ci
JOIN list on ci.number1 + CONVERT(varchar(5),ci.number2) = DATAPOINT ;