Wednesday, March 21, 2012

Current Row Number

Hi,
Is there a way to get the current Row number.
If get this as a result of select statement,
12 as
56 yu
89 gh
I need like this
1 12 as
2 56 yu
3 89 gh
I saw some eg.s using temporary tables. But Is there a straight forward way
to get this.
Thanks
KiranYou can use Group By, Order By clauses to get the row number. or you can
also make sure of subquery to get the row number..check examples here
http://www.aspfaq.com/show.asp?id=2427
instead of temp tables, you can make use of table variables in the example
provided in the above link.
Av.
http://dotnetjunkies.com/WebLog/avnrao
http://www28.brinkster.com/avdotnet
"Kiran" <Kiran@.nospam.net> wrote in message
news:#0zmneS$EHA.2112@.TK2MSFTNGP14.phx.gbl...
> Hi,
> Is there a way to get the current Row number.
> If get this as a result of select statement,
> 12 as
> 56 yu
> 89 gh
> I need like this
> 1 12 as
> 2 56 yu
> 3 89 gh
> I saw some eg.s using temporary tables. But Is there a straight forward
way
> to get this.
> Thanks
> Kiran
>|||Kiran wrote:
> Hi,
> Is there a way to get the current Row number.
> If get this as a result of select statement,
> 12 as
> 56 yu
> 89 gh
> I need like this
> 1 12 as
> 2 56 yu
> 3 89 gh
> I saw some eg.s using temporary tables. But Is there a straight
> forward way to get this.
> Thanks
> Kiran
This uses a temp table as well, but maybe it's something different than
what you saw. Can you tell us how you are going to to know the order of
the rows during the select? Do you have an ORDER BY for the statement
that is driving the final order of the rows?
create table #OrderTest(col1 int not null, col2 varchar(3) not null)
insert into #OrderTest (col1, col2) values (10, 'AA')
insert into #OrderTest (col1, col2) values (12, 'AA')
insert into #OrderTest (col1, col2) values (13, 'AA')
insert into #OrderTest (col1, col2) values (16, 'AA')
Select IDENTITY(int, 1, 1) as "OrderCol", col1, col2
INTO #OrderTest2
From #OrderTest
Select * from #OrderTest2
Drop Table #OrderTest
Drop Table #OrderTest2
David Gugick
Imceda Software
www.imceda.com|||Hi Kiran,
Wait for SQL 2005
Regards,
Daniel
"Kiran" <Kiran@.nospam.net> wrote in message
news:#0zmneS$EHA.2112@.TK2MSFTNGP14.phx.gbl...
> Hi,
> Is there a way to get the current Row number.
> If get this as a result of select statement,
> 12 as
> 56 yu
> 89 gh
> I need like this
> 1 12 as
> 2 56 yu
> 3 89 gh
> I saw some eg.s using temporary tables. But Is there a straight forward
way
> to get this.
> Thanks
> Kiran
>

No comments:

Post a Comment