将ORDER BY语句添加到SELECT语句

时间:2013-04-10 17:30:56

标签: sql-server

我有以下SELECT声明,我需要添加ORDER BY [UserName]

SELECT * 
FROM [UserInfo] 
WHERE ([AWSAccountID] = CASE WHEN @AWSAccountID = -1 THEN [AWSAccountID] 
ELSE @AWSAccountID END)

我在确定ORDER BY需要去哪里时会遇到一些麻烦,假设它可以添加。

1 个答案:

答案 0 :(得分:7)

除非我遗漏了某些内容,否则您只需将ORDER BY添加到查询的末尾:

SELECT * 
FROM [UserInfo] 
WHERE ([AWSAccountID] = CASE WHEN @AWSAccountID = -1 THEN [AWSAccountID] 
                           ELSE @AWSAccountID END)
ORDER BY [UserName]