Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 4th, 2003, 03:18 PM
lito's Avatar
lito lito is offline
MACTEP /\OMACTEP
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150 lito User rank is Private First Class (20 - 50 Reputation Level)lito User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 46 m 51 sec
Reputation Power: 7
Database functions question

I was looking for similar functions between MS SQL 2000 and MySQL 3.X...

In mySQL you can select specific number of items with a "LIMIT" clause and also specify Starting record number and how many to display EX: Select * FROM myTable WHERE ... LIMIT 1, 50

So that would pull 50 records beginning with record number 1.

Is there anything similar to this in MS SQL? Or how would I be able to achieve this result

Thanks,
Lito
__________________
Sometimes just a few hours of trial and error debugging can save minutes of reading the manual.

Reply With Quote
  #2  
Old December 4th, 2003, 05:13 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,689 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 16 h 12 m 15 sec
Reputation Power: 985
select TOP 50 * from mytable
ORDER BY something

unfortunately, there's no offset capability, so to get rows 51 - 100 is a lot harder

rudy
http://r937.com/

Reply With Quote
  #3  
Old December 4th, 2003, 06:16 PM
lito's Avatar
lito lito is offline
MACTEP /\OMACTEP
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150 lito User rank is Private First Class (20 - 50 Reputation Level)lito User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 46 m 51 sec
Reputation Power: 7
thanks rudy,
but offset capability is exactly what I need! This is a web based app...

the trouble is my database contains records for multiple users and those records are only available to the owner and they are scattered throuout the database in no particular order also the owner needs to be able page through his records in order.

Reply With Quote
  #5  
Old December 4th, 2003, 07:29 PM
lito's Avatar
lito lito is offline
MACTEP /\OMACTEP
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Seattle, WA
Posts: 150 lito User rank is Private First Class (20 - 50 Reputation Level)lito User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 46 m 51 sec
Reputation Power: 7
Thanks r937,
I have see some of these pages but not all, I was just hoping that there is something equivalent to LIMIT function in MySQL, I guess not then.

In case anyone else is looking for a solution this seems to work pretty good...
PHP Code:
 SELECT TOP 1 ID FROM myTable WHERE userID 101 AND ID
NOT IN 
(SELECT TOP 3 ID FROM myTable WHERE userID 101 ORDER BY ID)
ORDER BY ID 

It's short and easy compared to some others out there

First select statement tells how many records per page
in my case it's one record per page, if you choose multiple records per page for example 10 records on each page... then you must also increment second select by 10

ID is a unique identifier
userID in my case is used to identify the data that belongs to specific user (no one but user 101 is allowed to see it)

in Second select it tells MS SQL to to find the top record that is not in the first 3 records with smallest ID (that's where ORDER BY comes in handy)

so then you would get 4th record with smallest ID

for example...
if user 101 has following ID's in database (12,550,725,1200,15000) then
the query above would return record with ID field 1200

and if you want to go a page back
you just subtract from ID in the second select statement the number of records per page.

~Lito

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Database functions question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway