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.
| Author |
Topic |
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2006-04-08 : 07:54:18
|
| select * from @mytableorder by case when @orderby = 'fname' then firstname end, case when @orderby = 'lname' then lastname end, case when @orderby = 'mname' then middlename end ASC <---- this statement causes an errorhow can i add order by ascending or descending without using dynamic sql queryusing the above statement.TCC |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2006-04-08 : 08:08:40
|
| i also tried this one.. but still it wont workcreate procedure DisplayResult @orderby varchar(20), @order varchar(4)asselect * from @mytableorder bycase when @orderby = 'fname' then firstname end,case when @orderby = 'lname' then lastname end,case when @orderby = 'mname' then middlename endcase when @order='asc' then ASC else DESC endgoexec dbo.DisplayResult ('fname','desc')TCC |
 |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2006-04-08 : 08:11:38
|
| What do you think guys. do i need to use dynamic sql query for this one?or there is other way around.TCC |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-08 : 08:11:43
|
select * from @mytableorder bycase when @orderby = 'fname' then firstname end,case when @orderby = 'lname' then lastname end,case when @orderby = 'mname' then middlename endASC KH |
 |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2006-04-08 : 08:15:48
|
| khtan i encountered error with the stament you wrote.. it happens whenselect * from @mytableorder bycase when @orderby = 'fname' then firstname when @orderby = 'lname' then lastname when @orderby = 'mname' then middlename when @orderby = 'account_no' then account_no endASCaccount_no is of type integer this causes an error if i use it within the same case statement.TCC |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-08 : 08:23:12
|
convert it to varcharselect * from @mytableorder bycase when @orderby = 'fname' then firstnamewhen @orderby = 'lname' then lastnamewhen @orderby = 'mname' then middlenamewhen @orderby = 'account_no' then convert(varchar(10), account_no) endASC KH |
 |
|
|
tishri
Yak Posting Veteran
95 Posts |
Posted - 2006-04-08 : 21:28:57
|
| thanks khtanTCC |
 |
|
|
jallen
Starting Member
1 Post |
Posted - 2006-04-23 : 00:57:10
|
| Try combining logic of field and sort order.JoeyThe case is a powerful statement......select name, id, sdatefrom ( --create some data to use select name='abc', id=3, sdate = getdate()-1 union all select 'xyz', 1, getdate()-2 union all select 'mno', 2, getdate()-3 ) Xorder by --if ordering by name asc case when 1=0 and 2=2 then name else --this nullify the asc at the end, no pun intended null end asc, --if ordering by name desc case when 3=3 and 4=4 then name else null end desc, --through in another field case when 1=1 then id else null end |
 |
|
|
|
|
|
|
|