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 |
|
baburk
Posting Yak Master
108 Posts |
Posted - 2008-11-17 : 01:08:38
|
| Hi,Incorrect syntax near the keyword 'ASC'.Where to put the ASC and DESC.DECLARE @OrderBY CHAR(4)BEGIN SET @OrderBY = 'DESC' SELECT ROW_NUMBER() OVER(PARTITION BY Items.Item ORDER BY CASE WHEN @OrderBY = 'ASC' THEN Items.Item ASC WHEN @OrderBY = 'DESC' THEN Items.Item DESC END) AS RowNumber, Items.DateAdded , Items.Item , Items.Tags FROM dbo.ItemsEND Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-17 : 01:23:10
|
you cant change the ordering using case statement like this. i think you can do a work around like thisDECLARE @OrderBY CHAR(4)BEGINSET @OrderBY = 'DESC'SELECT ROW_NUMBER() OVER(PARTITION BY Items.Item ORDER BYCASEWHEN @OrderBY = 'ASC' THEN Items.Item ELSE 1END ASC,CASEWHEN @OrderBY = 'DESC' THEN Items.Item DESCELSE 1END) AS RowNumber,Items.DateAdded ,Items.Item ,Items.TagsFROM dbo.ItemsEND I assume Item is of type integer. if not set instead of 1 just string default value like '' |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-17 : 01:49:02
|
| orDECLARE @OrderBY CHAR(4)BEGINSET @OrderBY = 'DESC'EXEC('SELECT ROW_NUMBER() OVER(PARTITION BY Items.Item ORDER BYItems.Item '+@OrderBY +') AS RowNumber,Items.DateAdded ,Items.Item ,Items.TagsFROM dbo.ItemsEND')MadhivananFailing to plan is Planning to fail |
 |
|
|
baburk
Posting Yak Master
108 Posts |
Posted - 2008-11-17 : 02:38:47
|
| Hi, Thanks for both of you.I tried like this and it works.DECLARE @OrderBY CHAR(4)BEGIN SET @OrderBY = 'DESC' SELECT ROW_NUMBER() OVER(PARTITION BY Items.Item ORDER BY CASE WHEN @OrderBY = 'ASC' THEN Items.Item END ASC, CASE WHEN @OrderBY = 'DESC' THEN Items.Item END DESC) AS RowNumber, Items.DateAdded , Items.Item , Items.Tags FROM dbo.Items ORDER BY CASE WHEN @OrderBY = 'ASC' THEN Items.Item END ASC, CASE WHEN @OrderBY = 'DESC' THEN Items.Item END DESCEND |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-17 : 02:45:16
|
You're welcome |
 |
|
|
|
|
|
|
|