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
 SQL Server Development (2000)
 dynamic "ORDER BY "

Author  Topic 

iamsmahd
Yak Posting Veteran

55 Posts

Posted - 2006-06-14 : 07:09:38
hi all,
I wanna to have a select query which its order by part dinamically changes based on the option that user selects from a combo box of form.....I have written this query but as it seems it is not correct::
declare @n varchar(70)
set @n = 'PersonNumber'
select * from dbo.Persons order by @n

what is your solutions? tanks in advance

sonia

vallis
Starting Member

12 Posts

Posted - 2006-06-14 : 07:12:59
declare @n varchar(70)
set @n = 'PersonNumber'
EXEC('select TOP 50 * from dbo.Persons order by ' + @n)
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-06-14 : 07:20:21
http://www.sqlteam.com/item.asp?ItemID=2209

Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-06-14 : 08:54:45
quote:
Originally posted by vallis

declare @n varchar(70)
set @n = 'PersonNumber'
EXEC('select TOP 50 * from dbo.Persons order by ' + @n)



Dynamic SQL is not needed. Refer the link posted by Ryan

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

iamsmahd
Yak Posting Veteran

55 Posts

Posted - 2006-06-14 : 10:03:26
THANK YOU VERY MUCH

sonia
Go to Top of Page
   

- Advertisement -