Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 23rd, 2003, 04:47 AM
Registered User
 
Join Date: Jul 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default inserting into joined tables

I need to insert data into 2 linked tables which have unique identifiers created automatically (set as an identity in sql server). Do i need to execute multiple statements in order to do the insert or can it be done with just one.

If i do it with multiple statements how do i return the unique id from table 1 in order to insert into table 2.

 
Old July 23rd, 2003, 04:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

After you do the 1st insert, select @@identity (or SCOPE_IDENTITY() in SQL 2000) to get the id you need to insert in the 2nd table.
 
Old July 23rd, 2003, 05:02 AM
Authorized User
 
Join Date: Jun 2003
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

This has to be done using multiple statements. e.g.

Insert table1
values ('x', 'x')

declare @ident numeric
select @ident = IDENT_CURRENT('table1')

Insert table2
values (@ident, 'x', 'x')
 
Old July 23rd, 2003, 06:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

The use of IDENT_CURRENT() in this situation is not wise. IDENT_CURRENT() returns the last identity value used for the indicated table across all connections. Thus, it is quite possible that you could run into concurrency issues (another user also inserts into 'Table1'). Much better to use scope_identity() here.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting values from other tables TheAndruu SQL Language 7 May 27th, 2007 10:47 AM
Listing results from joined tables henry-horse Classic ASP Basics 5 March 9th, 2007 07:08 AM
Inserting data into 2 tables chrscote Access ASP 1 August 1st, 2005 09:01 AM
PROBLEM Inserting data into 2 tbls that r JOINED shyster1977 Classic ASP Databases 2 March 10th, 2005 10:40 AM
UPDATE with joined tables DanKent SQL Server 2000 4 July 6th, 2004 08:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.