September 8, 2009 at 1:05 pm
Hi friends,
I do 'select rownum from table' in oracle to select rows ordered by rownum. What is the equivalent in sql server?
Thanks
September 8, 2009 at 1:19 pm
That functionality isn't really built in to SQL server but you can do this if you are using SQL 2005 or later
SELECT *, ROW_NUMBER() OVER (ORDER BY [column name]) as rowNum
FROM
order by ROW_NUMBER() OVER (ORDER BY [column name])
You would have to change the [column name] and
.
September 8, 2009 at 4:31 pm
newbieuser (9/8/2009)
I do 'select rownum from table' in oracle to select rows ordered by rownum.
:w00t: No, you don't.
In the Oracle world rownum is a pseudo-column, Oracle assigns -to rownum- a value from 1 up to all rows in a retrieved dataset. Not matter the order rownum will always look like 1, 2, 3, ... , etc.
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.September 8, 2009 at 4:41 pm
Just as a matter of technical interest:
If ROWNUM in Oracle returns rows ordered by the internal row identifier (RID) of the table, then SQL Server does have something similar, though it is undocumented, likely to change any moment, and will cause your shoes to catch fire if you use it for anything other than curiosity value:
*** 2008 only - see my next post for a 2005 version ***
CREATE TABLE dbo.A (id INT NOT NULL);
INSERT dbo.A VALUES (1)
INSERT dbo.A VALUES (2)
INSERT dbo.A VALUES (3)
SELECT id, %%PhysLoc%% from dbo.A ORDER BY %%PhysLoc%%;
The value returned can be converted to a true heap RID with some magic.
Never use this. Did I mention that already?
Paul
edit: added the ORDER BY to better match the original question, and to add Lowell's observation 😀
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
September 8, 2009 at 5:12 pm
Paul i tried your example on SQL2005 Standard, SP3, but i get a syntax error in a 9.0 compatibility database, as well as master. is that a 2008 feature? it is really percent-percent-PhysLoc-percent-percent, right? no paste error?
Msg 102, Level 15, State 3, Line 7
Incorrect syntax near 'PhysLoc'.
productversion productlevel edition
9.00.4035.00 SP3 Standard Edition
Lowell
September 8, 2009 at 5:29 pm
Hey Lowell,
Thanks for that! Yes it is 2008-specific, but the following will work in both (though not quite the same thing - it's close)
CREATE TABLE dbo.A (id INT NOT NULL);
INSERT dbo.A VALUES (1)
INSERT dbo.A VALUES (2)
INSERT dbo.A VALUES (3)
SELECT id, %%LockRes%% from dbo.A ORDER BY %%LockRes%%;
DROP TABLE dbo.A
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
September 8, 2009 at 5:42 pm
that is one of the coolest things I've seen in a long time; Thanks Paul, I learned something,and have a shiny new toy to play with now.
I added this to my snippets.
Lowell
September 8, 2009 at 5:57 pm
You're welcome - but make sure you fire-proof your shoes eh?
BTW you may notice that %%LockRes%% output bears a resemblance to entries in the resource_description column of sys.dm_tran_locks for RID and KEY lock types. You may also see it in places like Activity Monitor...
Have fun 🙂
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
September 8, 2009 at 6:02 pm
Paul White (9/8/2009)
If ROWNUM in Oracle returns rows ordered by the internal row identifier
...actually, it doesn't.
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.September 8, 2009 at 6:44 pm
PaulB (9/8/2009)
Paul White (9/8/2009)
If ROWNUM in Oracle returns rows ordered by the internal row identifier...actually, it doesn't.
Not that I really care, but it turns out I was thinking of Orcale's ROWID.
ROWNUM seems to be a sort of dynamic IDENTITY(1,1) or ROW_NUMBER() OVER (ORDER BY ).
Thanks for posting such a super-helpful and detailed reply though - well done sir.
Paul
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
September 8, 2009 at 8:29 pm
Paul White (9/8/2009)
Thanks for posting such a super-helpful and detailed reply though - well done sir.
Just to make it clear, rownum does not exist in real world, it's a pseudo-column. Think of it as the sequence number of rows returned by a query no matter how rows are ordered.
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.September 8, 2009 at 8:40 pm
PaulB (9/8/2009)
Just to make it clear, rownum does not exist in real world, it's a pseudo-column. Think of it as the sequence number of rows returned by a query no matter how rows are ordered.
Thanks.
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
September 8, 2009 at 8:47 pm
So, with that in mind, the following seems to emulate the ROWNUM pseudo-column:
-- Assumes AdventureWorks sample database is available
WITH ResultSet AS
(
SELECT TOP (9223372036854775807) *
FROM [AdventureWorks].[HumanResources].[Employee]
ORDER BY
[BirthDate] ASC
)
SELECT ROWNUM = ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),
*
FROM ResultSet;
GO
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
September 8, 2009 at 11:48 pm
PaulB (9/8/2009)
newbieuser (9/8/2009)
I do 'select rownum from table' in oracle to select rows ordered by rownum.:w00t: No, you don't.
In the Oracle world rownum is a pseudo-column, Oracle assigns -to rownum- a value from 1 up to all rows in a retrieved dataset. Not matter the order rownum will always look like 1, 2, 3, ... , etc.
Maybe not like that, but I've used it in relation to derived tables and it does work as advertised. Of course, it doesn't help that the OP excluded the highly recommended ORDER BY that should go with it.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 9, 2009 at 7:05 am
Jeff Moden (9/8/2009)Maybe not like that, but I've used it in relation to derived tables
You are correct. Smart people are able to do all kinds of magic like using rownum to "remember" the order in which rows got delivered by a previous query.
In this specific case OP was relying on rownum to get "ordered" rows from a table which would never happen.
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.Viewing 15 posts - 1 through 15 (of 32 total)
You must be logged in to reply to this topic. Login to reply