Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 13th, 2003, 03:06 PM
nyoumans nyoumans is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 2 nyoumans User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Getting unique id for a newly added record (SQL)

Hi All,

I am converting an ASP web application that originally used MS Access to MS SQL.

The database is fairly complex and I had no problem importing the access DB to MS SQL.

In one section, the user adds a new record to the DB. There are several pages of form information that is part of a new record.

With Access, I was able to use the following to grab the counter (unique id) from the newly added record (so send so the subsequent pages:

rs.AddNew
[assigning rs fields from form info]
rs.Update
id = rs("counter")
[redirect to next page, if no errors]

using SQL, id & rs("counter") never has a value.

I assuming I am missing something simple, but cannot figure out what it is. Any help would be great appreciated.

thanks,
Nick

Reply With Quote
  #2  
Old November 13th, 2003, 04:01 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 8 m 57 sec
Reputation Power: 9
Send a message via ICQ to stumpy Send a message via MSN to stumpy
It's a little tricky the first time around. All DB's have a unique way of doing it. For SQL Server check out this code.
Code:
'--Execute the INSERT statement and the SELECT @@IDENTITY
Set rcs = db.execute("insert into tablename (fields,..) " & _
    "values (values,...);" & _
    "SELECT @@IDENTITY").nextrecordset

'--Retrieve the @@IDENTITY value
new_identity = rcs(0)
Notice the "SELECT @@IDENTITY" at the end of the query - this will go and get the value of the last inserted ID and return it as the first and only field. Hence using "rs(0)" to retrieve the value.

Reply With Quote
  #3  
Old November 14th, 2003, 06:44 AM
nyoumans nyoumans is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 2 nyoumans User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks!

That seemed to do the trick. I doubt i would figured that out on my own

Thanks again,
Nick

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > Getting unique id for a newly added record (SQL)


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 3 hosted by Hostway