I am using SQL Server 2014 and I am querying a linked server.
My T-SQL query stands as follows:
USE MyDatabase SELECT [EMP_CODE], [NAME], [ADDRESS] FROM [xxx.xxx.xx].[HR].[dbo].[EMPLOYEE] WHERE [EMP_CODE] IN ( 'A0025', 'A0045', 'B0008', ... ... 'A0096' ) ORDER BY CASE [EMP_CODE] WHEN 'A0025' THEN 1 WHEN 'A0045' THEN 2 WHEN 'B0008' THEN 3 ... ... WHEN 'A0096' THEN 68 END
My issue is that I am getting the following error message when running the above T_SQL query:
"Case expressions may only be nested to level 10."
How can I get around this error?
Advertisement
Answer
I don’t know where the nested case
expressions are coming from. But an alternative formulation would use JOIN
:
SELECT . . . FROM [xxx.xxx.xx].[HR].[dbo].[EMPLOYEE] e LEFT JOIN (VALUES ('A0025', 1), ('A0045', 2), ('B0008', 3), . . .) ) v(emp_code, ord) ON e.emp_code = v.emp_code ORDER BY COALESCE(v.ord, 1000000)