大家好我一直在尝试做这个SQL,但它显示了这条消息。我不知道为什么因为条件有一个IN来搜索子查询,但是卡在它上面。
查询
SELECT PRO.ID_PRODUCTO,
PRO.ID_CATEGORIA,
PRO.DESCRIPCION AS DESCRIPCION
FROM T_PRODUCTO PRO WHERE PRO.ID_CATEGORIA in (
CASE WHEN @ID_CATEGORIA = '0'
THEN PRO.ID_CATEGORIA
ELSE
(Select [Data]
from dbo.fnSplit('1,3,4,5',',')
)
消息
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
答案 0 :(得分:2)
SELECT PRO.ID_PRODUCTO,
PRO.ID_CATEGORIA,
PRO.DESCRIPCION AS DESCRIPCION
FROM T_PRODUCTO PRO
WHERE @ID_CATEGORIA = '0'
OR PRO.ID_CATEGORIA IN (SELECT [Data]
FROM dbo.fnSplit('1,3,4,5',','))
答案 1 :(得分:0)
在CASE
语句中使用时,子查询将被计算为表达式。
您可以以不同的方式应用案例陈述(因为它基于您的@ID_CATEGORIA
变量):
SELECT PRO.ID_PRODUCTO,
PRO.ID_CATEGORIA,
PRO.DESCRIPCION AS DESCRIPCION
FROM T_PRODUCTO PRO WHERE
(
@ID_CATEGORIA <> '0'
AND PRO.ID_CATEGORIA IN (
SELECT [Data] FROM dbo.fnSplit('1,3,4,5',',')
)
)
OR
(
@ID_CATEGORIA = '0'
AND PRO.ID_CATEGORIA = PRO.ID_CATEGORIA
)