我在下面有表结构,我想通过使用 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'.
提前感谢您的帮助。