Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 order by @orderby

Author  Topic 

ygeorge
Yak Posting Veteran

68 Posts

Posted - 2003-08-12 : 12:12:46
Hi,

I'm doing a stored procedure which will sort the resultset with a input column name. It will look like this -

select * from MY_TABLE order by @ColName

Is there a way to make it without using dynamic SQL?

Thanks,
George

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-08-12 : 12:47:32
Here is an example:


DECLARE @SortOrder tinyint
SET @SortOrder = 2

SELECT CompanyName,
ContactName,
ContactTitle
FROM Customers
ORDER BY CASE WHEN @SortOrder = 1 THEN CompanyName
WHEN @SortOrder = 2 THEN ContactName
ELSE ContactTitle
END


Tara
Go to Top of Page

ygeorge
Yak Posting Veteran

68 Posts

Posted - 2003-08-12 : 12:51:05
Thanks Tara.
Go to Top of Page
   

- Advertisement -