Sign in to follow this  

[web] SQL Help -Stored Procedures

This topic is 4196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm trying to build a stored procedure that inserts some rows into a couple tables..we'll call them table1 and table2 The problem is I need to know the Primary ID of the record that I insert into table1 in order to insert my second record into table2.. For example I have this right now
INSERT INTO table1 (name, value) VALUES ('kag1' , 'value1')
after inserting that record do I have to SELECT it again in order to get the ID of it? (The ID is an integer that increments 1 every record) and then after I get that ID i wanted to go like
INSERT INTO table2 (table1_ID, name, value) VALUES (@ID, 'kag1' ,' value2')
I know such a thing is possible...I just can't find out how to do it.. Thanx kag1

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you are using MS Sql Server or Sql Express you can use the SELECT @@IDENTITY after the insert to retrieve the last Primary Key inserted in the table. For the other databases, I am not aware.

HTH

Share this post


Link to post
Share on other sites

This topic is 4196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this