Most time we are used a sort option with differen columns is a Dynamic Query. In this example will give a solution for removing the Dynamic Query.
DECLARE @SortBy AS VARCHAR(20)
SET @SortBy = 'EmployeeID'
SELECT * FROM TableName
ORDER BY
CASE WHEN @SortBy = 'EmployeeId' THEN EmpID
WHEN @SortBy = 'DomainID' THEN DomainID
ELSE
Location
END
ASC
The Case Result Value must be a column name of the given table.
04:26 |
Category:
SQL SERVER
|
0
comments
Comments (0)