?
Solved

Inserting auto id into tables when new record is inserted

Posted on 2009-02-13
4
Medium Priority
?
518 Views
Last Modified: 2012-05-06
I have a tbl_test that has an identity field; this field is used in all other tables...when a user inserts new information, I need all other tables to have the new ID inserted as well.

@test
@test1
@test2
@test3

begin

insert into table1 (test1) values (@test1)
@@SCOPE IDENTITY
insert into table2 (id, test2) values (@test2)
ETC


how do you write this code?

Thx
0
Comment
Question by:Glen_D
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 23633528
this will do:
declare @id int
insert into table1 (test1) values (@test1)
set @id = SCOPE IDENTITY()
insert into table2 (id, test2) values (@id, @test2)
insert into table3 (id, test3) values (@id, @test3)
... etc .. 
note: you will need error handling ...

Open in new window

0
 

Author Comment

by:Glen_D
ID: 23633557
thx but what kind of error handling...in case the id doesn't get inserted?  G
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 23633658
like this:

declare @id int
declare @err int
begin transaction
 
insert into table1 (test1) values (@test1)
 
set @err = @@ERROR, @id = SCOPE_IDENTITY()
 
if @err = 0
begin
  insert into table2 (id, test2) values (@id, @test2)
  set @err = 0
end
 
if @err = 0
begin
  insert into table3 (id, test3) values (@id, @test3)
  set @err = 0
end
 
if @err = 0
begin
  commit 
end
else
begin
  rollback
  raiserror('insert failed', 16,1)
end

Open in new window

0
 

Author Closing Comment

by:Glen_D
ID: 31546606
Thx...I have another question but will save 4 later..G
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

829 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