| Author |
Topic |
|
mori0043
Starting Member
18 Posts |
Posted - 2007-07-25 : 15:44:22
|
| Hello, I want to query a database with this:SELECT *FROM dbo.vw_TabsWHERE PortalId = NULLORDER BY TabOrder, TabNameUNIONSELECT *FROM dbo.vw_TabsWHERE PortalId = 0ORDER BY TabOrder, TabNameUnfortunately I can't order by inside of the select statements with a union between. Is there any way around this? Thanks in advance... |
|
|
cognos79
Posting Yak Master
241 Posts |
Posted - 2007-07-25 : 16:01:49
|
| No you cant. you can only use order by at the end. If you use order by at the end it should serve your propose. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-25 : 16:08:04
|
Use this workaroundSELECT *FROM dbo.vw_TabsWHERE PortalId = NULL OR PortalID = 0ORDER BY CASE WHEN PortalID IS NULL THEN 0 ELSE 1 END, TabOrder, TabName E 12°55'05.76"N 56°04'39.42" |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-07-25 : 16:12:39
|
| If you were using more than one object, I do it like this:SELECT Column1, Column2FROM(SELECT 1 AS OrderBy, Column1, Column2FROM Table1WHERE ...UNION ALLSELECT 2, Column1, Column2FROM Table2WHERE ...) tORDER BY OrderBy, Column1, Column2Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-07-25 : 16:13:42
|
:)quote: Originally posted by Peso Use this workaroundSELECT *FROM dbo.vw_TabsWHERE PortalId = IS NULL OR PortalID = 0ORDER BY CASE WHEN PortalID IS NULL THEN 0 ELSE 1 END, TabOrder, TabName
|
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-25 : 16:14:24
|
Thanks!I guess I have to change my SET ANSI_NULL setting now?  E 12°55'05.76"N 56°04'39.42" |
 |
|
|
mori0043
Starting Member
18 Posts |
Posted - 2007-07-25 : 17:09:36
|
| Unfotunately tkizer's idea won't work. The sort will be off and this is why I need to use the union. It is essentially two queries that I want to tie together. I will try Peso's workaround and let you know how it goes...THanks! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-25 : 17:12:43
|
Tara's approach will work. Look closer. E 12°55'05.76"N 56°04'39.42" |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-07-25 : 17:13:07
|
| How will the sort be off in my solution, which really shouldn't be used anyhow since you are only referencing one object? But regardless, how is it wrong?Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-07-25 : 17:14:32
|
| Or, you can get the results of each query (with the order by) into a table variable and do a SELECT from it at the end.Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
mori0043
Starting Member
18 Posts |
Posted - 2007-07-25 : 20:39:36
|
| Sorry... I read it too fast. The solution works. Thanks Tara! |
 |
|
|
|