Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 23rd, 2003, 04:47 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
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.

Reply With Quote
  #2 (permalink)  
Old July 23rd, 2003, 04:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
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.
Reply With Quote
  #3 (permalink)  
Old July 23rd, 2003, 05:02 AM
Authorized User
 
Join Date: Jun 2003
Location: Edinburgh, , United Kingdom.
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')
Reply With Quote
  #4 (permalink)  
Old July 23rd, 2003, 06:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:53 AM.


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