CREATE PROCEDURE getEmployees AS
CREATE PROCEDURE getEmployees ( @OrderByClause varchar(100) ) AS
CREATE PROCEDURE getEmployees ( @OrderByClause varchar(100) ) AS Create a variable @SQLStatement DECLARE @SQLStatement varchar(255) Enter the dynamic SQL statement into the variable @SQLStatement SELECT @SQLStatement = 'SELECT EmployeeID, FirstName, LastName, SSN, Salary ' + 'FROM Employees ORDER BY ' + @OrderByClause Execute the SQL statement EXEC(@SQLStatement)
SELECT FirstName, LastName, CASE WHEN Salary < 20000 THEN 'Not so much' WHEN Salary >= 20000 AND Salary < 100000 THEN 'Better' WHEN Salary >= 100000 AND Salary < 250000 THEN 'Big bucks!' ELSE 'Holly cow!' END As WealthInfo FROM Employees ORDER BY LastName, FirstName
CREATE PROCEDURE getEmployees ( @ColumnName varchar(100) ) ASSELECT EmployeeID, FirstName, LastName, SSN, Salary FROM Employees ORDER BY CASE WHEN @ColumnName='LastName' THEN LastName WHEN @ColumnName='Salary' THEN CONVERT(varchar(50), Salary) WHEN @ColumnName='SSN' THEN SSN END