Solved

How can i create a record and find its id to create more records in other tables all at the same time?

Posted on 2008-06-26
8
189 Views
Last Modified: 2010-03-20
I have 2 tables.

employee
employee_cell

When i insert an employee a employeeid is created, how can i get that key and insert something into employee_cell all at the same time?




0
Comment
Question by:mmedi005
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 21876131

you have to enable the identity property for EmployeeID  after the first insert, the last inserted EmployeeID can be retrieved using SCOPE_IDENTITY()

DECLARE @LastEmpID int
INSERT INTO Employee
SELECT ....
SELECT @LastEmpID = scope_identity()
INSERT INTO Employee_Cell (EmployeeID, ... )
select @LastEmpID, ....
0
 
LVL 10

Expert Comment

by:Dxpert
ID: 21876140
Use the @@IDENTITY system function.

http://msdn.microsoft.com/en-us/library/ms187342.aspx
0
 
LVL 10

Expert Comment

by:Dxpert
ID: 21876183
Oops.. I guess SCOPE_IDENTITY would do a better job since it works within the same scope  :-)
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:mmedi005
ID: 21876248
>>you have to enable the identity property for EmployeeID  after the first insert, the last inserted EmployeeID can be retrieved using SCOPE_IDENTITY()

DECLARE @LastEmpID int
INSERT INTO Employee
SELECT ....
SELECT @LastEmpID = scope_identity()
INSERT INTO Employee_Cell (EmployeeID, ... )
select @LastEmpID, ....

so will this work?

INSERT INTO employee(john,doe)VALUES(first,last)
@lastID = scope_identity()
INSERT INTO employee_cell(@lastID,555-5555)
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 21876297
if there is an identity column on the Employee table thhis will work

INSERT INTO employee(first,last)VALUES('john','doe')
SELECT @lastID = scope_identity()
INSERT INTO employee_cell
SELECT @lastID,'555-5555'
0
 

Author Comment

by:mmedi005
ID: 21876342
makes sense....

why this in the second insert?
INSERT INTO employee_cell
SELECT @lastID,'555-5555'
0
 

Author Comment

by:mmedi005
ID: 21876389
this is what i have set for employeeid

Identity Specification    Yes
      (Is Identity)             Yes
      Identity Increment   1
      Identity Seed          1


are these settings correct to use scope_identity()?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21876735
Yes, it means it will start the seeding at 1 and increment by 1 for each row entered.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

831 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