Unix Technical Forum

SQL for fetching first n records

This is a discussion on SQL for fetching first n records within the Sybase forums, part of the Database Server Software category; --> Hi I want a query which will return first n records from a table in Sybase. I do not ...


Go Back   Unix Technical Forum > Database Server Software > Sybase

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 04:04 PM
Abhi
 
Posts: n/a
Default SQL for fetching first n records

Hi
I want a query which will return first n records from a table in Sybase.
I do not want to use the SET Rowcount option. Can anybody suggest me the
solution for the same.


Thanks and Regards
-Abhi
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 04:04 PM
Michael Peppler
 
Posts: n/a
Default Re: SQL for fetching first n records

On Tue, 04 Nov 2003 08:59:14 -0800, Abhi wrote:

> Hi
> I want a query which will return first n records from a table in Sybase.
> I do not want to use the SET Rowcount option.


Why?

> Can anybody suggest me the solution for the same.


If you control the client you could fetch the appropriate number of rows
and then cancel the request, but that's not going to be very efficient.

I could think of a few other possible ideas, but they're all pretty ugly.

Michael
--
Michael Peppler Data Migrations, Inc.
mpeppler@peppler.org http://www.mbay.net/~mpeppler
Sybase T-SQL/OpenClient/OpenServer/C/Perl developer available for short or
long term contract positions - http://www.mbay.net/~mpeppler/resume.html

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 04:04 PM
Larry Coon
 
Posts: n/a
Default Re: SQL for fetching first n records

Abhi wrote:

> I want a query which will return first n records from a table in Sybase.


Based on any specific criteria, or do you just want to
limit it to ANY n rows?


Larry Coon
University of California
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 04:04 PM
Carl Kayser
 
Posts: n/a
Default Re: SQL for fetching first n records

I completely agree with the comments made by Michael and Larry. That said,
you might be able to write code to do what you want based upon the SQL
below. It returns the "first" N records by name from sysobjects. Note, in
general, some possible problems.

(1) If there are "duplicate keys" then (N-1), (N+1), etc. rows might be
returned instead of N.
(2) If "first" depends upon multiple columns (and especially if the column
datatypes differ) then the query can be quite ugly. Actually, I can see a
generalization if all of the columns are [var]char (use concatenation).
Otherwise I don't.
(3) If the table is large and there is no useful index performance could be
terrible.

select (o.name)

from sysobjects o

having N > (select count (o2.name)

from sysobjects o2

where o2.name < o.name)

order by o.name



"Abhi" <getabhijit@indiatimes.com> wrote in message
news:67db1745.0311040859.28f0b3d1@posting.google.c om...
> Hi
> I want a query which will return first n records from a table in

Sybase.
> I do not want to use the SET Rowcount option. Can anybody suggest me the
> solution for the same.
>
>
> Thanks and Regards
> -Abhi



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-08-2008, 04:04 PM
Rob Verschoor
 
Posts: n/a
Default Re: SQL for fetching first n records

"Abhi" <getabhijit@indiatimes.com> wrote in message
news:67db1745.0311040859.28f0b3d1@posting.google.c om...
> Hi
> I want a query which will return first n records from a table

in Sybase.
> I do not want to use the SET Rowcount option. Can anybody suggest me

the
> solution for the same.
>
>
> Thanks and Regards
> -Abhi


I also think you should simply use 'set rowcount'. But if you don't
want to do that (tell us why...) this is an alternative:

select row_number = identity(9), * into #t from your_table order by
<your-ordering-criterium>

select * from #t where row_number <= N -- N = a number defined by
you


Note the the whole concept of 'the first N rows' is rather shaky
without defining the what ordering you have in mind. Hence, you should
provide an 'order by' to define this.

Also note that this is not advisable when the table in question is
large.

HTH,

Rob
-------------------------------------------------------------
Rob Verschoor

Certified Sybase Professional DBA for ASE 12.5/12.0/11.5/11.0
and Replication Server 12.5

Author of "Tips, Tricks & Recipes for Sybase ASE" and
"The Complete Sybase ASE Quick Reference Guide"
Online orders accepted at http://www.sypron.nl/shop

mailto:rob@DO.NOT.SPAM.sypron.nl.REMOVE.THIS.DECOY
http://www.sypron.nl
Sypron B.V., P.O.Box 10695, 2501HR Den Haag, The Netherlands
-------------------------------------------------------------

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:52 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com