Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

order by clause using input parameter

Status
Not open for further replies.

sarahw

MIS
Sep 29, 2000
25
GB
I have a stored procedure which performs a query based on a number of input parameters. I need the resultset to be ordered by a user defined input parameter. When I pass a parameter called sort (which is the column name) and refer to it in the order by clause, the syntax checker throws up an error message.

Does anyone know of a way to do this?

Many thanks
 
Dear sarahw;
Here is solution of your problem. Check it , use pubs database.

*****************************************************
CREATE PROCEDURE sample
@Sort varchar(20)

AS

Declare @SQL nvarchar(500)

Set @SQL = 'SELECT * FROM AUTHORS ORDER BY ' + @Sort

Exec (@SQL)
*****************************************************

Regards,
essa2000
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top