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 > ASP > Get the last inserted row id from a table and update it in another table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-21-03, 13:27
geckobtz geckobtz is offline
Registered User
 
Join Date: Aug 2003
Posts: 3
Get the last inserted row id from a table and update it in another table

Hi Guys,

I am not sure weather if i am in the right track. First of all i am using DreamweaverMX and the Application server is ASP with MS Sql2000 as the DB.
I am not a developer but i can fiddle around and done some lite programming in Php as well as in Asp with the aid of Dreamweaver.

Right now i am stuck with a situation don’t know how to proceed, wondering weather if it is the right approach.

I developing an application that has about 20 tables, some of them are so called lookup tables that you enter the information first in order to relate that to another table and so on.

The trouble is i've done very well in inserting and updating deleting etc. Now i am stuck wondering how to go about.

I have User_table, and a transaction_table; what i wish to do is when a new user is created which is done by inserting a new row in the User_table, i need at the same time pick that new row's user_id which is auto_increment that was just created and insert it in the field situated in the transaction_table (user_id as FK) and so on. I just realized that i will be needing this technique for other situation in the other tables.

I also realized that the id should be the one just inserted and not any body else's id that was inserted just after me.

Looking through different forums i found solutions like @@IDENTITY, select max (user_id) through procedure etc but unfortunately i am stuck so as to how i'll implement these solution.

I'd appreciate if some one could help me guide through the procedure or some solution or example where i could take a look or even any tutorial etc. i am opened to all proposition.

Thanks in advance.

Gecko
Reply With Quote
  #2 (permalink)  
Old 08-21-03, 13:56
Seppuku Seppuku is offline
Useless...
 
Join Date: Jul 2003
Location: SoCal
Posts: 721
The best, and most reliable, way to accomplish this is through a SQL Trigger. A SQL Trigger is bound to an action on a table. So if you were to INSERT a new User into your Users table, and you had a trigger created on the INSERT action of the Users table, it would automatically execute the SQL defined in the trigger, which could replicate the user's information into your secondary "transaction" table.

The benefit of this method is that you now ensure referential integrity down at the database and not in your ASP. If you do this same thing in ASP, I could go into the database and add a user without updating the transaction table. By adding a trigger to the table, even if I go into the DB directly and add a user, that trigger will still fire and update the transaction table.
Reply With Quote
  #3 (permalink)  
Old 08-21-03, 17:13
geckobtz geckobtz is offline
Registered User
 
Join Date: Aug 2003
Posts: 3
Thumbs up

Hi thanks for that quick advice, its lot clearer for me now.
After having passed weeks over this, thinking weather I’ve got the db design correct so as to insert the new id after having just inserted a new row one. So I guess this is how it’s happening in the real world of relational DB.
Now all I need to now is to figure out how to implement this trigger through dreamweaver or by doing some hand coding.

So thanks Seppuku for the tip.
I was bit skeptic when I took this project to work on Asp thinking of where to find help when stuck, because when I was helping a friend on his site on Php I was using the Devshed [ http://www.devshed.com ]forum for all my questions and there was always some to help you,i would recomend it for Php and Mysql.
Now I guess I found a superb forum for Asp and MS Sql.
I shall post here if I have any questions or doubts.

Thanks

Gecko
Reply With Quote
  #4 (permalink)  
Old 08-25-03, 12:51
Seppuku Seppuku is offline
Useless...
 
Join Date: Jul 2003
Location: SoCal
Posts: 721
No problem.. you may also want to look into Stored Procedures. Some conversations with others here have lead me to believe there are times and places to use both, so weigh the pros and cons of each.
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On