1

我在 MS SQL 上遇到了一个问题:
我正在使用一个 CASE,在我看来有很多语句,如下所示:

select
e.[Nom d'annuaire],
e.[Prénom annuaire],
f.[Nom d'annuaire en UTF-8] AS login,
e.Usagers as [Numéro interne],
d.[Numéro externe],
s.[Préfixe No abr direct],
v.fonction,

  CASE
           WHEN v.lib_delegation = 'Service informatique' THEN 'Euralille'
           WHEN v.delegation = 'test_national' THEN 'Maillot'
           WHEN v.delegation = 'test_tpe' THEN 'Maillot'
           WHEN v.delegation = 'test_paris-idf' THEN 'Maillot'
           WHEN v.delegation = 'test_grands-comptes' THEN 'Maillot'
           WHEN v.delegation = 'cotestrm' THEN 'Marcq en baroeul'
           WHEN v.delegation = 'test_sud-ouest' THEN 'Bruges'
           WHEN v.delegation = 'test_normandie' THEN 'Caen'
           WHEN v.delegation = 'test_bourgogne' THEN 'Dijon'
           WHEN v.delegation = 'test_ouest' THEN 'Ouest'
           WHEN v.lib_delegation = 'Centre Limousin' THEN 'Tours'
           WHEN v.delegation = 'test_est' THEN 'Nancy'
           WHEN v.delegation = 'test_mediterranee' THEN 'Aix'
           WHEN v.delegation = 'test_rhone-alpes' THEN 'Lyon'
           ELSE v.delegation
        END AS Délégation,
        v.lib_delegation

from ale e 

INNER JOIN ale f
on e.Usagers = f.Usagers AND f.[No alias] = 1 and e.[No Alias] = 0

INNER JOIN Delegation d
on f.Usagers = d.[No annuaire]

INNER JOIN V_users v
on v.LOGIN = f.[Nom d'annuaire en UTF-8]

LEFT OUTER JOIN svi s
on e.Usagers = s.[No d'appel] 

order by e.Usagers

如您所见,我的 CASE 语句不是嵌套的,因为我使用的是WHEN... THEN ...语法,然后我只是简单地放置ELSE ... END语句。

但是,当我尝试使用例如 filter 过滤我的输出时:

WHERE [Délégation] = 'Euralille'

我只收到 2 个错误:
Msg 8180, Level 16, State 1, Line 1
Msg 125, Level 15, State 4, Line 1

错误编号说明:

  • 消息 8180 = 无法准备语句。
  • Msg 125 = Case 表达式只能嵌套到 %d 级。

(抱歉法语翻译不佳)
我一直在 MS SQL 网站和 StackOverflow 上寻找答案,但问题始终是 CASE 语句是嵌套的。但对我来说,我不是这种情况......

在此先感谢,我是这个世界的初学者。

4

1 回答 1

0

你是这样过滤的吗?请注意,我将您的所有查询放入子查询中,然后应用 where 子句:

select * from 
(select
e.[Nom d'annuaire],
e.[Prénom annuaire],
f.[Nom d'annuaire en UTF-8] AS login,
e.Usagers as [Numéro interne],
d.[Numéro externe],
s.[Préfixe No abr direct],
v.fonction,

  CASE
           WHEN v.lib_delegation = 'Service informatique' THEN 'Euralille'
           WHEN v.delegation = 'test_national' THEN 'Maillot'
           WHEN v.delegation = 'test_tpe' THEN 'Maillot'
           WHEN v.delegation = 'test_paris-idf' THEN 'Maillot'
           WHEN v.delegation = 'test_grands-comptes' THEN 'Maillot'
           WHEN v.delegation = 'cotestrm' THEN 'Marcq en baroeul'
           WHEN v.delegation = 'test_sud-ouest' THEN 'Bruges'
           WHEN v.delegation = 'test_normandie' THEN 'Caen'
           WHEN v.delegation = 'test_bourgogne' THEN 'Dijon'
           WHEN v.delegation = 'test_ouest' THEN 'Ouest'
           WHEN v.lib_delegation = 'Centre Limousin' THEN 'Tours'
           WHEN v.delegation = 'test_est' THEN 'Nancy'
           WHEN v.delegation = 'test_mediterranee' THEN 'Aix'
           WHEN v.delegation = 'test_rhone-alpes' THEN 'Lyon'
           ELSE v.delegation
        END AS Délégation,
        v.lib_delegation

from ale e 

INNER JOIN ale f
on e.Usagers = f.Usagers AND f.[No alias] = 1 and e.[No Alias] = 0

INNER JOIN Delegation d
on f.Usagers = d.[No annuaire]

INNER JOIN V_users v
on v.LOGIN = f.[Nom d'annuaire en UTF-8]

LEFT OUTER JOIN svi s
on e.Usagers = s.[No d'appel] 

order by e.Usagers) as subTotalQuery
WHERE [Délégation] = 'Euralille'
于 2013-05-02T10:57:52.380 回答