Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
| 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 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
  #1 (permalink)  
Old January 25th, 2005, 08:26 AM
Authorized User
Join Date: Jan 2005
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default inserting into multiple tables


I have an employers and an address table in my database. The employers table contains an address_id field that refers to the employers address in the address table. I would like to have a stored procedure that inserts the employers address into the address table and then the address_id into the employers table with the other employer details. I'm no SQL expert and I don't have a clue how to do this. Any ideas? Thanks

  #2 (permalink)  
Old January 25th, 2005, 12:05 PM
Friend of Wrox
Join Date: Jan 2004
Location: , , USA.
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts

This procedure assumes that employer_id and Address_Id (Primary keys) are integer identity fields in your Employers and Employers_Address table

CREATE PRocEDURE ProcAddEmployers

@Employer_Name varchar(50) = NULL,
@Employer_Address varchar(50) = NULL,
@Employer_Address2 varchar(50) = NULL,
@Employer_City varchar(30) = NULL,
@Employer_State char(2) = NULL,
@Employer_Zip varchar(10) = NULL,
@Employer_Phone varchar(14) = NULL,
@Employer_Fax varchar(14) = NULL,
@Employer_Email varchar(50) = NULL



--Insert into Address Table
INSERT INTO Employers_Address (Address,Address2,City,State, Zip, Phone, Fax, Email)
(@Employer_Address,@Employer_Address2,@Employer_Ci ty,@Employer_State, @Employer_Zip,@Employer_Phone,@Employer_Fax,@Emplo yer_Email)

DECLARE @Address_ID int --Foreign key
SET @Address_ID = @@IDENTITY --ID of the last inserted record

--Insert into Employers Table
INSERT INTO Employers (Employer_Name,Address_ID) VALUES (@Employer_Name,@Address_ID)



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting Data into Multiple Tables bfoley SQL Server ASP 1 January 3rd, 2008 12:28 AM
Inserting data in multiple tables rohits PHP How-To 1 August 29th, 2006 11:32 PM
Inserting a record and contents in multiple tables Shell84 ASP.NET 2.0 Professional 6 August 15th, 2006 05:31 PM
Inserting data into 2 tables chrscote Access ASP 1 August 1st, 2005 09:01 AM
Importing Multiple files in Multiple tables Versi Suomi Access 6 June 1st, 2005 08:47 AM

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