[web] SQL Help -Stored Procedures

Started by
2 comments, last by Cygnus_X 17 years, 10 months ago
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
Advertisement
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
If you're using MySQL and PHP you can use mysql_insert_id.
--
Cheers,
Darren Clark
On a related note, just how reliable is mysql_insert_id() ? I have used it for several scripts, and I've noticed that sometimes it picks up a zero or null value for the auto-increment column.

This topic is closed to new replies.

Advertisement