Solved

ansi sql equivalent to t-sql "top"

Posted on 2012-03-13
29
965 Views
Last Modified: 2012-07-24
How would you go about writing the ansi equivalent to t-sql's select top n * from tableName?
0
Comment
Question by:_Wade_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
  • 6
  • +1
29 Comments
 
LVL 15

Expert Comment

by:Anuj
ID: 37715662
LIMIT 10
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37715663
which level of ansi sql?

in sql2003 you get window functions like row_number()
sql2008 has fetch first
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 37715671
LIMIT 10 is mysql flavor

I would say,

Select top 10 fieldname from your table
0
Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

 
LVL 74

Expert Comment

by:sdstuber
ID: 37715676
anujnb,

 I don't think the LIMIT clause is part of any ANSI standard as yet.
 If it is, can you specify which one?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37715683
sammySeltzer,

the asker was looking for an ANSI equivalent to sqlserver TOP
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 37715685
LIMIT is also postgres db dialect
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 37715692
ok, I see sdstuber
0
 
LVL 15

Expert Comment

by:Anuj
ID: 37715707
Yes, that's right LIMIT is of mysql and it will not work for SQL Server, as @sdstuber suggested you have to use window functions like ROW_NUMBER(), RANK() etc. but these are supported only on SQL Server 2005 and above, for older versions you have to use TOP clause.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37715726
LIMIT is not part of ANSI/ISO SQL   depending on the version (up to 2008)

If the asker is looking for sql92  standards then there isn't a way except to procedurally iterate N times through a result set.

If the asker is looking for a later sql standard, then there might be options, which ones, again, depends on the version of ansi defined.

Then, of course, the real trick is whether that standard has been implemented in the target database

I'm sure we could all name umpteen different ways to do it and umpteen databases where each of those methods would fail.  However, none of that is particularly relevant to the question which is what are the ANSI methods.
0
 

Author Comment

by:_Wade_
ID: 37715777
I'm querying a DB2 database from .NET with oledb. Offhand I don't know what version of ANSI SQL it conforms to.

I've tried some of the suggestions though and none work so far. I might be using the wrong syntax.

select * from bsydtad.docdvlf8 where now_number() = 4 ...didn't work

How do I use "LIMIT"?
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 250 total points
ID: 37715797
DB2 supports FETCH FIRST


select  * from tableName
fetch first 10 rows only
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 37715799
LIMIT will not work for DB2.

It is more like

select * from bsydtad.docdvlf8 where someid = someid order by someid
fetch 10 rows only;

something like that.
0
 
LVL 15

Accepted Solution

by:
Anuj earned 250 total points
ID: 37715800
For DB@ its Fetch First

  SELECT *
  FROM Table
  ORDER BY EmpID
  FETCH FIRST 10 ROWS ONLY
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37715810
db2 does support row_number too,

using it would look something like this...


select * from (
select d.*, row_number() over(order by something)  as rn
from bsydtad.docdvlf8 as d) as x
 where rn = 4
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 37715812
i missed the first in error, sorry.

typing fast
0
 

Author Comment

by:_Wade_
ID: 37715814
ahh...

SELECT column_name(s)
FROM table_name
LIMIT number

Yeah that's MySql
0
 

Author Comment

by:_Wade_
ID: 37715829
select * from (
select d.*, row_number() over(order by something)  as rn
from bsydtad.docdvlf8 as d) as x
 where rn = 4

..in this one is the "something" in over(order by something) supposed to be a unique field?>
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37715847
it doesn't have to be unique, but if it is, it'll ensure your ordering is repeatable.


for example

X Y
2 a
3 a
5 a
7 a
11 a
13 a


if you order by Y, then your results could be anything.  If you order by X then your results will be consistent and meaningful

the ordering is required so the function can pick a row to be #1, #2, #3 etc
0
 

Author Comment

by:_Wade_
ID: 37716406
When I try that I'm getting the following error back. Apparently the row_number() is not supported in the version of DB2 we have. Is there another way?

ERROR: [IBM][iSeries Access ODBC Driver][DB2 UDB]SQL0255 - Function
not supported for query.
Error Code: -255

Query = select
* from (
select d.*, row_number() over(order by doc_id)
 as rn
from bsydtad.docdvlf8 as d) as x
 where rn = 4
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37716420
use FETCH FIRST as shown above

http:#a37715797
0
 

Author Comment

by:_Wade_
ID: 37716696
Thanks, sorry didn't try that one at first.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37716819
wouldn't a split be in order?

I posted the same thing earlier.  

Also, the question asked was about ANSI SQL not db2
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 37718231
Actually, everyone posted similar solutions ; go figure!
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 37718385
I agree, the db2 bit can be split, I happened to posted first, but they all came in about the same time.  Picking only the last one isn't appropriate.

However, while one of those was the accepted answer, none of db2 references really answer the question that was asked.  Those are really just supplemental info.

So, my point was, if the decision is to ignore the original question about ANSI SQL then I think a split is in order rather than picking the last post that mentioned the db2 feature.

Or, reevaluate the whole thread and split differently among the posts that actually discussed the original question.
0
 

Author Comment

by:_Wade_
ID: 37722656
I agree sdstuber. I've been under the weather these past few days and have some projects due and I awarded the points very hastily without reviewing the whole post again first. I'll have a look again and split the points. You gave a lot of useful feedback.
0
 

Author Comment

by:_Wade_
ID: 37722667
_alias99,

Can the thread be reopened please for point reallocation?
0
 

Author Closing Comment

by:_Wade_
ID: 38219717
Sorry this took so long...
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question