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 |
|
zhshqzyc
Posting Yak Master
240 Posts |
Posted - 2009-04-14 : 14:41:59
|
Hello,Can we have a query select * from table1 order by something There is no where clause.Thanks |
|
|
whitefang
Enterprise-Level Plonker Who's Not Wrong
272 Posts |
Posted - 2009-04-14 : 14:43:50
|
| Yes. |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-04-14 : 14:44:23
|
| yes |
 |
|
|
zhshqzyc
Posting Yak Master
240 Posts |
Posted - 2009-04-14 : 14:59:20
|
| [code]select @prim = case @SortBy when '1' then ' order by DeptName' when '2' then ' order by SaleAmt' else '' end[/code]So how to write script?Suppose @SortBy is passed as 1 already.[code]select * from table1@prim[/code] |
 |
|
|
whitefang
Enterprise-Level Plonker Who's Not Wrong
272 Posts |
Posted - 2009-04-14 : 15:04:29
|
quote: Originally posted by zhshqzyc
select @prim = case @SortBy when '1' then ' order by DeptName' when '2' then ' order by SaleAmt' else '' end So how to write script?Suppose @SortBy is passed as 1 already.select * from table1@prim
DECLARE @prim intSET @prim = 1SELECT * FROM table1 ORDER BYCASE WHEN @SortBy = 1 THEN DeptNameWHEN @SortBy = 2 THEN SaleAmtEND |
 |
|
|
zhshqzyc
Posting Yak Master
240 Posts |
Posted - 2009-04-14 : 15:36:39
|
I have another conditionwhen @OrderBy = 0 then ' ASC' when @OrderBy = 1 then ' DESC' How to combine them together? |
 |
|
|
zhshqzyc
Posting Yak Master
240 Posts |
Posted - 2009-04-14 : 16:29:16
|
I meanSELECT * FROM table1 ORDER BYCASE WHEN @SortBy = 1 and @OrderBy =1 THEN DeptName ascWHEN @SortBy = 1 and @OrderBy =2 THEN SaleAmt descEND Is it correct? |
 |
|
|
Kokkula
Starting Member
41 Posts |
Posted - 2009-04-15 : 08:43:15
|
| Hello,Try thisSELECT * FROM table1 ORDER BY CASE WHEN @SortBy = 1 and @OrderBy = 1 THEN DeptName END ASC ,CASE WHEN @SortBy = 1 and @OrderBy =2 THEN SaleAmt END DESCHope helpful...Thanks,Pavan |
 |
|
|
|
|
|