为什么会收到此错误
关键字
附近的语法不正确
当我执行以下代码时
SELECT *
FROM [dbo].[priority_table] p
WHERE EXISTS ( (SELECT 1
FROM [dbo].[item_table] i
WHERE i.priority_id = p.priority_id)
AND filter = @filter )
OR ( @filter IS NULL )
在过去的两个小时里,我的头部无法得到任何东西。当Where
变量为@filter
NULL
子句
答案 0 :(得分:3)
在Open parenthesis
EXISTS
SELECT *
FROM [dbo].[priority_table] p
WHERE ( EXISTS (SELECT 1
FROM [dbo].[item_table] i
WHERE i.priority_id = p.priority_id)
AND filter = @filter )
OR ( @filter IS NULL )
答案 1 :(得分:0)
如果在[dbo]。[item_table]
中过滤列SELECT *
FROM [dbo].[priority_table] p
WHERE EXISTS
(SELECT 1
FROM [dbo].[item_table] i
WHERE (i.priority_id = p.priority_id
AND @filter = filter)
OR @filter IS NULL);
如果过滤器位于[dbo]。[priority_table]
SELECT *
FROM [dbo].[priority_table] p
WHERE(EXISTS
(SELECT 1
FROM [dbo].[item_table] i
WHERE i.priority_id = p.priority_id)
AND @filter = filter)
OR @filter IS NULL;
答案 2 :(得分:0)
这是无效的
EXISTS ( ( SELECT 1
FROM [dbo].[item_table] i
WHERE i.priority_id = p.priority_id
)
AND filter = @filter
)
SELECT 1不等同于AND滤镜
答案 3 :(得分:0)
这不是ju-ju魔术。 where子句中存在的内容就像子查询(特别是半连接)一样,因此(为了强调而添加额外的行):
SELECT *
FROM [dbo].[priority_table] p
WHERE EXISTS
(
SELECT 1
FROM [dbo].[item_table] i
WHERE i.priority_id = p.priority_id
AND (i.filter = @filter
OR @filter IS NULL)
)
关键位是确保使用表别名,因此您可以正确引用子查询的外部和内部表。
答案 4 :(得分:0)
或者更清楚地替代MotoGP的答案是(对@filter = 2没有任何假设):
SELECT *
FROM [dbo].[priority_table] p
WHERE EXISTS
(
SELECT 1
FROM [dbo].[item_table] i
WHERE i.priority_id = p.priority_id
AND i.filter = @filter
)
OR ( @filter IS NULL )