If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > last value of database

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-14-04, 04:24
mrbear mrbear is offline
Registered User
 
Join Date: Mar 2004
Posts: 21
last value of database

i want to get the last value of the database and the database look like this

table name prospec
TranID data item
1 02/02/2004 2
2 02/02/2004 2
3 02/02/2004 3
4 02/02/2004 3
i want to get the value (4) which auto generate by itself
i try "select TranID from prospec order by TranID desc limit 1"
then when i run it then this error came out "incorrect syntax new 'limit' "
Reply With Quote
  #2 (permalink)  
Old 04-14-04, 06:20
ika ika is offline
Registered User
 
Join Date: Oct 2003
Location: Slovakia
Posts: 482
Re: last value of database

Quote:
Originally posted by mrbear
i want to get the last value of the database and the database look like this

table name prospec
TranID data item
1 02/02/2004 2
2 02/02/2004 2
3 02/02/2004 3
4 02/02/2004 3
i want to get the value (4) which auto generate by itself
i try "select TranID from prospec order by TranID desc limit 1"
then when i run it then this error came out "incorrect syntax new 'limit' "
What type of database are you using?
Reply With Quote
  #3 (permalink)  
Old 04-14-04, 20:55
mrbear mrbear is offline
Registered User
 
Join Date: Mar 2004
Posts: 21
Re: last value of database

Quote:
Originally posted by ika
What type of database are you using?
i'm using sqlserver databse
Reply With Quote
  #4 (permalink)  
Old 04-14-04, 22:16
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Re: last value of database

Quote:
Originally posted by mrbear
i want to get the last value of the database and the database look like this

table name prospec
TranID data item
1 02/02/2004 2
2 02/02/2004 2
3 02/02/2004 3
4 02/02/2004 3
i want to get the value (4) which auto generate by itself
i try "select TranID from prospec order by TranID desc limit 1"
then when i run it then this error came out "incorrect syntax new 'limit' "
For MS-SQL 7.0 or later, you probably want:
PHP Code:
SELECT TOP 1 TranID
   FROM prospec
   ORDER BY TranID DESC 
You can actually get the same result much more portably by using:
PHP Code:
SELECT Max(TranID)
   
FROM prospec 
This should work on any database that supports SQL-89 or later.

-PatP
Reply With Quote
  #5 (permalink)  
Old 04-14-04, 22:35
derrickleggett derrickleggett is offline
Registered User
 
Join Date: Apr 2004
Location: Kansas City, MO
Posts: 734
Re: last value of database

Quote:
Originally posted by Pat Phelan
For MS-SQL 7.0 or later, you probably want:
PHP Code:
SELECT TOP 1 TranID
   FROM prospec
   ORDER BY TranID DESC 
You can actually get the same result much more portably by using:
PHP Code:
SELECT Max(TranID)
   
FROM prospec 
This should work on any database that supports SQL-89 or later.

-PatP
In SQL 7 or later, you also want:

SELECT
MAX(TranID)
FROM
prospec
__________________
MeanOldDBA
derrickleggett@hotmail.com
When life gives you a lemon, fire the DBA.
Reply With Quote
  #6 (permalink)  
Old 04-15-04, 06:33
mrbear mrbear is offline
Registered User
 
Join Date: Mar 2004
Posts: 21
thanks

thanks guys, the statment works. Thanks a million times
Reply With Quote
  #7 (permalink)  
Old 04-15-04, 08:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Re: last value of database

Quote:
Originally posted by derrickleggett
In SQL 7 or later, you also want:

SELECT
MAX(TranID)
FROM
prospec
also?

isn't that what Pat said? you even quoted him!!!!!!

and what's up with the "SQL 7 or later" qualification??

are you saying select max(foo) from bar won't work in 6.5????

whoa
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On