Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get the last product id and increase it by one "very urgent"

Status
Not open for further replies.

koniba

Programmer
Nov 4, 2000
4
US
I am very new in sql and asp.

I am updating several table with information. I needs to capture the last product id in the Product table and increase it by before inserting it in other tables. How do I do that using vbscript and sql?

Dim proid
proid = "SELECT (Last_ProductId) + 1 FROM Product


Then I would insert proid in the other table for the ProductId column information.

Please help. It is very urgent.


Thanks,

Koniba
Niarela@aol.com


 
very simple, you will need one query to get the latest productid.

Assuming that your productid field name is "PRODUCTID" and table is "PRODUCT", do this:

'this will get the latest productid
'either use this
SELECT PRODUCTID FROM PRODUCT ORDER BY PRODUCT ID DESC
'or this
SELECT MAX(PRODUCTID) FROM PRODUCT

save the value of PRODUCTID to a variable and increment by one, then run whatever query you want.

:)
good luck and god bless
 
If your key is an identity - that is, an autonumber, autoincrement ...
you do your insert, and afterwards you want to know which key was generated for this newly inserted row.

"select @@identity" will return this key.

When you're working on a databaseconnection in a multiuser environment, this is the safe approach, opposed to the select max(...) .. described above.

Cheers

Søren
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top