For dynamic orderby clauses in Stored procedure
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)
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)
Comments
Post a Comment