0

我有一个有趣的问题。我将编写一个标量值函数。其中一个参数不在表格列中。我必须在 where 子句中使用这个参数 (@ImpUrgPri)。至于那个参数,会有一个条件,表中存在一个列。你能帮我解决这个问题吗?

DECLARE @ImpUrgPri NVARCHAR(3)
SET @ImpUrgPri = 'URG'
DECLARE @Enum INT
SET @Enum = 0

select COUNT(*) 
from zODYA_vwCallReq_AllData 
where ActiveFlag = 1
AND 
(CASE WHEN (@ImpUrgPri = 'PRI') THEN Priority_Enum == @Enum
        WHEN (@ImpUrgPri = 'URG') THEN Urgency_Enum == @Enum 
        WHEN (@ImpUrgPri = 'IMP') THEN Impact_Enum == @Enum END)

group by 
(CASE WHEN (@ImpUrgPri = 'PRI') THEN Priority_Enum 
        WHEN (@ImpUrgPri = 'URG') THEN Urgency_Enum 
        WHEN (@ImpUrgPri = 'IMP') THEN Impact_Enum END)
4

3 回答 3

2

您的选择可能是:

select COUNT(*) 
from zODYA_vwCallReq_AllData 
where ActiveFlag = 1
AND @Enum =  (CASE WHEN (@ImpUrgPri = 'PRI') THEN Priority_Enum
             WHEN (@ImpUrgPri = 'URG') THEN Urgency_Enum
             WHEN (@ImpUrgPri = 'IMP') THEN Impact_Enum END)

group by 
(CASE WHEN (@ImpUrgPri = 'PRI') THEN Priority_Enum 
        WHEN (@ImpUrgPri = 'URG') THEN Urgency_Enum 
        WHEN (@ImpUrgPri = 'IMP') THEN Impact_Enum END)
于 2012-11-19T13:06:38.640 回答
0

我认为最好使用IF并拥有三个不同SELECT的 s,每个“何时”案例一个。

您还可以动态创建 SQL(在存储过程中),这会使存储过程稍微短一些。这里只有三个选项,我会坚持使用 3 个 SELECT。

于 2012-11-19T13:02:27.980 回答
0
DECLARE @ImpUrgPri NVARCHAR(3)
SET @ImpUrgPri = 'URG'
DECLARE @Enum INT
SET @Enum = 0

select COUNT(*) 
from zODYA_vwCallReq_AllData 
where ActiveFlag = 1
AND 
(CASE @ImpUrgPri
    WHEN 'PRI' THEN Priority_Enum
    WHEN 'URG' THEN Urgency_Enum
    WHEN 'IMP' THEN Impact_Enum
END) = @Enum
group by 
(CASE @ImpUrgPri
    WHEN 'PRI' THEN Priority_Enum
    WHEN 'URG' THEN Urgency_Enum
    WHEN 'IMP' THEN Impact_Enum
END)
于 2012-11-19T13:08:09.733 回答