?
Solved

Insert data into two tables and retrieve last ID

Posted on 2008-01-29
4
Medium Priority
?
1,296 Views
Last Modified: 2010-05-18
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?
0
Comment
Question by:monica73174
  • 2
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20771452
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...
0
 

Author Comment

by:monica73174
ID: 20771785
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.
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20771860
0
 

Author Closing Comment

by:monica73174
ID: 31426133
Thank you, your answers were very helpful
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

594 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question