We help IT Professionals succeed at work.

Insert data into two tables and retrieve last ID

I have a form that has a checkbox list.  The form gets submitted and the checkbox list is saved into an array list.  I want to save the main form data to one table and then save the ID created to the linking table with each item checked in the checkbox list.  

I want to use a stored procedure to do this, so how do I get the ID that was just inserted and how can I pass the list of checkbox values that I stored in the array list?
Comment
Watch Question

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
if the field is identity, the query to get the last inserted id (within the same session) is
SELECT SCOPE_IDENTITY()

with sql 2005, you have also the possiblity to get the id values when inserting multiple rows at once, using the OUTPUT clause...

Author

Commented:
I think that solves my first problem.  Now I need to know how to pass an array to a stored proc and insert the values into a linking table with the ID.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:

Author

Commented:
Thank you, your answers were very helpful

Explore More ContentExplore courses, solutions, and other research materials related to this topic.