This article shares information about sorting resultset of T-SQL query using ORDER BY clause with alias name of the column mentioned in the SELECT statement .
The reason that ORDER BY clause works with alias name of the column mentioned in the SELECT statement where as WHERE , GROUP BY & HAVING clause cannot is ORDER BY comes after SELECT statement in the logical query processing phase .
Example :
DECLARE @Tmp TABLE (Id INT) INSERT @Tmp SELECT 3 INSERT @Tmp SELECT 2 INSERT @Tmp SELECT 1 --Without ORDER BY SELECT * FROM @Tmp --With ORDER BY AliasName SELECT Id AliasName FROM @Tmp ORDER BY AliasName
The reason that ORDER BY clause works with alias name of the column mentioned in the SELECT statement where as WHERE , GROUP BY & HAVING clause cannot is ORDER BY comes after SELECT statement in the logical query processing phase .
No comments:
Post a Comment