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 2005 Forums
 Transact-SQL (2005)
 order by without where clause

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.
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-04-14 : 14:44:23
yes
Go to Top of Page

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]
Go to Top of Page

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 int
SET @prim = 1

SELECT * FROM table1 ORDER BY
CASE
WHEN @SortBy = 1 THEN DeptName
WHEN @SortBy = 2 THEN SaleAmt
END
Go to Top of Page

zhshqzyc
Posting Yak Master

240 Posts

Posted - 2009-04-14 : 15:36:39
I have another condition


when @OrderBy = 0 then ' ASC'
when @OrderBy = 1 then ' DESC'

How to combine them together?
Go to Top of Page

zhshqzyc
Posting Yak Master

240 Posts

Posted - 2009-04-14 : 16:29:16
I mean

SELECT * FROM table1 ORDER BY
CASE
WHEN @SortBy = 1 and @OrderBy =1 THEN DeptName asc
WHEN @SortBy = 1 and @OrderBy =2 THEN SaleAmt desc
END


Is it correct?
Go to Top of Page

Kokkula
Starting Member

41 Posts

Posted - 2009-04-15 : 08:43:15
Hello,

Try this

SELECT *
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 DESC

Hope helpful...


Thanks,
Pavan
Go to Top of Page
   

- Advertisement -