4

我在下面有表结构,我想通过使用 case 关键字按顺序使用我在 select 语句中使用的函数。但是我收到错误消息,说 FullName 是无效列。

CREATE TABLE STUDENTS
(
     ID INT IDENTITY(1, 1) ,
     FirstName VARCHAR(50) ,
     LastName VARCHAR(50)
)

向表 STUDENTS 中插入一些数据

INSERT  INTO STUDENTS
VALUES  ( 'kamal', 'sharma' )

INSERT  INTO STUDENTS
VALUES  ( 'john', 'jack' )

INSERT  INTO STUDENTS
VALUES  ( 'Rahul', 'Thomson' )

该声明有效

SELECT 
       ID
     , FirstName + ' ' + LastName AS FullName
FROM STUDENTS
ORDER BY FullName ASC

我需要这个,但它说 FullName 是无效的列。

DECLARE @VAL INT  
SET @VAL = 1

SELECT ID
    ,FirstName + ' ' + LastName AS FullName
FROM STUDENTS
ORDER BY CASE 
        WHEN @VAL = 1
            THEN FullName
        END ASC
    ,CASE 
        WHEN @VAL = 2
            THEN FullName
        END DESC

Msg 207, Level 16, State 1, Line 10
Invalid column name 'FullName'.
Msg 207, Level 16, State 1, Line 14
Invalid column name 'FullName'.

提前感谢您的帮助。

4

5 回答 5

5

中定义的列别名SELECT不能在相应的表达式中使用ORDER BY

要么重复基本定义。

SELECT ID
    ,FirstName + ' ' + LastName AS FullName
FROM STUDENTS
ORDER BY CASE 
        WHEN @VAL = 1
            THEN FirstName + ' ' + LastName
        END ASC
    ,CASE 
        WHEN @VAL = 2
            THEN FirstName + ' ' + LastName
        END DESC

或者在不同级别定义别名。例如派生表或 CTE。

WITH T
     AS (SELECT ID,
                FirstName + ' ' + LastName AS FullName
         FROM   STUDENTS)
SELECT *
FROM   T
ORDER  BY CASE
            WHEN @VAL = 1 THEN FullName
          END ASC,
          CASE
            WHEN @VAL = 2 THEN FullName
          END DESC 
于 2013-06-26T07:22:54.117 回答
2

将其更改为:

SELECT ID
    ,FirstName + ' ' + LastName AS FullName
FROM STUDENTS
ORDER BY CASE 
        WHEN @VAL = 1
            THEN FirstName + ' ' + LastName
        END ASC
    ,CASE
        WHEN @VAL = 2
            THEN FirstName + ' ' + LastName
        END DESC
于 2013-06-26T07:23:39.943 回答
2

试试这个——

SELECT 
       ID
     , FullName
FROM (
     SELECT 
            ID
          , FullName = FirstName + ' ' + LastName
     FROM dbo.STUDENTS
) t
ORDER BY 
      CASE WHEN @VAL = 1 THEN FullName END
    , CASE WHEN @VAL = 2 THEN FullName END DESC

另一种变体:

DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = '

SELECT 
       ID
     , FullName
FROM (
     SELECT 
            ID
          , FullName = FirstName + '' '' + LastName
     FROM dbo.STUDENTS
) t
ORDER BY ' + 
     CASE WHEN @VAL = 1 
          THEN 'FullName' 
          ELSE 'FullName DESC' 
     END

EXEC sys.sp_executesql @SQL
于 2013-06-26T07:26:26.200 回答
0

只需尝试以下脚本:

 SELECT ID
            ,FirstName + ' ' + LastName AS FullName
        FROM STUDENTS
        ORDER BY FirstName + ' ' + LastName ASC
于 2013-06-26T07:47:21.077 回答
0

将 order-by 部分包裹在实际查询周围:

SELECT ID, FullName
FROM (
    SELECT  ID, 
            FirstName + ' ' + LastName AS FullName, 
            CASE WHEN @VAL = 1 THEN FullName END AS OrderNumber1,
            CASE WHEN @VAL = 2 THEN FullName END AS OrderNumber2
    FROM STUDENTS 
) data
ORDER BY OrderNumber1 ASC, OrderNumber2 DESC
于 2013-06-26T07:29:24.787 回答