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
185 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
 

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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parse field in SQL View 15 98
grouping logic 6 49
MS SQL Inner Join - Multiple Join Parameters 2 23
SQL bit field not working as expected 3 21
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

896 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now