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)
 Reverse ResultSet query in EXEC

Author  Topic 

baburk
Posting Yak Master

108 Posts

Posted - 2008-12-17 : 06:23:26
Hi,

This is my query.

SET @Query = 'SELECT City1.Station FROM City1 WHERE City1.Sequence' + CAST ( @SequenceName AS VARCHAR ) +' BETWEEN ' + @SeqStart + ' AND ' + @SeqEnd

EXEC (@Query)

How can I able to reverse the Station.

Not is asc or desc.

I want to reverse the resultset

Thanks in advance.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-12-17 : 06:27:28
SET @Query = 'SELECT Station FROM dbo.City1 WHERE Sequence' + @SequenceName +' BETWEEN ' + @SeqStart + ' AND ' + @SeqEnd + ' ORDER BY Station DESC'



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-17 : 06:28:20
[code]
SET @Query = 'SELECT REVERSE(City1.Station) FROM City1 WHERE City1.Sequence' + CAST ( @SequenceName AS VARCHAR ) +' BETWEEN ' + @SeqStart + ' AND ' + @SeqEnd

EXEC (@Query)
[/code]
Go to Top of Page

darkdusky
Aged Yak Warrior

591 Posts

Posted - 2008-12-17 : 06:29:08
Can you re-write your question? I'm not sure what you mean.
Go to Top of Page

baburk
Posting Yak Master

108 Posts

Posted - 2008-12-17 : 06:45:30
quote:
Originally posted by darkdusky

Can you re-write your question? I'm not sure what you mean.



If my result set is

Station
-------
Salem
Erode
Coimbatore
Egmore

I want it as
Station
-------
Egmore
Coimbatore
Erode
Salem

Reversing the total result set.

ie. UPSIDE DOWN MY RESULT SET

Thanks for the reply.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-17 : 06:46:27
do you have primary key field in your table? may be an identity column?
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-17 : 07:02:17
select identity (int,1,1) as id , name into #temptable from urtable

select * from #temptable order by id desc
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-17 : 07:08:53
quote:
Originally posted by bklr

select identity (int,1,1) as id , name into #temptable from urtable

select * from #temptable order by id desc


you can never guarantee the order in which records get populated in temp table using select... into and hence id wont necessarily be generated in same order in which the records appear in resultset.
see this

http://support.microsoft.com/kb/273586
Go to Top of Page

baburk
Posting Yak Master

108 Posts

Posted - 2008-12-17 : 07:11:34
quote:
Originally posted by bklr

select identity (int,1,1) as id , name into #temptable from urtable

select * from #temptable order by id desc



I have already have 3 temp tables. I don't want to have another temp table. How can I.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-17 : 07:14:27
quote:
Originally posted by baburk

quote:
Originally posted by bklr

select identity (int,1,1) as id , name into #temptable from urtable

select * from #temptable order by id desc



I have already have 3 temp tables. I don't want to have another temp table. How can I.



then whats your primary key/id column?
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-17 : 07:17:22
quote:
Originally posted by visakh16

quote:
Originally posted by baburk

quote:
Originally posted by bklr

select identity (int,1,1) as id , name into #temptable from urtable

select * from #temptable order by id desc



I have already have 3 temp tables. I don't want to have another temp table. How can I.



then whats your primary key/id column?



if u have any primary key or identity column then order by primarykey or identity column
r u have to insert an identity column in ur temp tables then order by that column
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-17 : 07:20:46
quote:
Originally posted by bklr


if u have any primary key or identity column then order by primarykey or identity column
r u have to insert an identity column in ur temp tables then order by that column


didnt understand why you quoted me. or were you suggesting to me?
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-17 : 07:29:00
quote:
Originally posted by visakh16

quote:
Originally posted by bklr


if u have any primary key or identity column then order by primarykey or identity column
r u have to insert an identity column in ur temp tables then order by that column


didnt understand why you quoted me. or were you suggesting to me?



iam not quote to u visakh
i am just explain to baburk
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-17 : 07:31:48
k
Go to Top of Page
   

- Advertisement -