Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL How do I INSERT INTO two tables at once and pass the ID from the first to the second

Posted on 2015-02-20
5
Medium Priority
?
7,087 Views
Last Modified: 2015-02-20
--
-- Two tables w two colums
-- Table "TestContacts", Colums "ContactID", "Name"
-- Table "TestAddress", Colums "ContactID", "Address"
-- Procedure to receive two params
--      INSERT INTO TestContacts.Name
--   ?get the ContactID of the inserted row
--  1. INSERT INTO TestAddress "ContactID, then INSERT INTO the TestAddress "Address" Where ContactID's match
--    or  
--  2. INSERT INTO TestAddress "the ContactID from TestContacts INSERT OPERATION above to ContactID" and "Address"
--    or??
-- result is to have ContactID in each table join(able) and get the right Name and Adress
--  
--         

CREATE PROCEDURE [dbo].[TestAddContact]
      (
      @Name nvarchar(50)
      ,@Address nvarchar (50)
      )
AS
BEGIN
      declare @IdentityValue int
      
INSERT INTO dbo.TestContacts ([Name])
OUTPUT INSERTED.ContactID INTO dbo.TestAddress([ContactID])
VALUES (@Name)

INSERT INTO dbo.TestAddress ([Address])
VALUES (@Address)
WHERE ContactID = INSERTED.ContactID
END
0
Comment
Question by:SamCash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 40622073
Give this a whirl..
Declare @ContactID int 

-- Creating these tables for demo purposes only. 
CREATE TABLE TestContacts (ContactID int identity(1,1), Name varchar(100)) 
CREATE TABLE TestAddress (ContactID int, whatever varchar(100))

INSERT INTO TestContacts (Name) 
VALUES ('McPaddy O''Flavo') 

-- Grab the newly-created id
SET @ContactID = SCOPE_IDENTITY()

-- Now use it in another table
INSERT INTO TESTADDRESS(ContactID, whatever) 
VALUES (@ContactID, 'whatever')

SELECT * FROM TestContacts
SELECT * FROM TestAddress

Open in new window

1
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40622075
CREATE PROCEDURE [dbo].[TestAddContact]
       (
       @Name nvarchar(50)
       ,@Address nvarchar (50)
       )
AS
SET NOCOUNT ON
DECLARE @IdentityValue TABLE (
    ContactID int
    )
       
INSERT INTO dbo.TestContacts ([Name])
OUTPUT INSERTED.ContactID INTO @IdentityValue
VALUES (@Name)

INSERT INTO dbo.TestAddress ([ContactID], [Address])
SELECT ContactID, @Address
FROM @IdentityValue
0
 

Author Comment

by:SamCash
ID: 40622156
Thanks much!

Worked perfect.  I read that "SCOPE_IDENTITY()" could change if a trigger or function occurred, and "SCOPE_IDENTITY()" may not be reliable in threaded and parallel systems ... to use OUTPUT INSERTED.fieldname to be safe and allow for future if not using these now.

Thanks Again
0
 

Author Closing Comment

by:SamCash
ID: 40622158
Thanks again for the simple solution.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40622166
I believe you are referring to @@IDENTITY, as I haven't experienced any issues with SCOPE_IDENTITY before.  But OUTPUT is guaranteed to return the inserted.ContactID.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

618 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