Solved

Adding multiple lines to a table using the identity from another table

Posted on 2011-09-14
2
192 Views
Last Modified: 2013-11-05
Hi,
Currntly I am using the following code to enter details of the customer into the customers table and details of their order into the orders table.  Is it possib;e to add multiple rows into the orders table where one customer requres more than one product
ALTER PROCEDURE [dbo].[usp_AddOrder] 

	
	@CompName		VARCHAR(50),
	@CustNo			VARCHAR(50),
	@LeadSource		VARCHAR(50),
	@Staff			VARCHAR(25),
	@ContactName	VARCHAR(50),
	@ContactTelNo	VARCHAR(20),
	@ProdID			VARCHAR(25),
	@ProdQty		FLOAT,
	@Discount		FLOAT
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

DECLARE @CompanyID	INT

INSERT INTO Company(CompName, CustNo, LeadSource, Staff, ContactName, ContactTelNo)
VALUES  (@CompName, @CustNo, @LeadSource, @Staff, @ContactName,@ContactTelNo)

SELECT @CompanyID=@@Identity

INSERT INTO Orders(CompanyID, ProdID, ProdQty, Discount, OrdDate)
VALUES(@CompanyID, @ProdID, @ProdQty, @Discount, GETDATE())

Open in new window


If I use the @@Identity again, I think it will pick up this from the recently added line in the orders tabl

Any help would be appreciated
0
Comment
Question by:Zbiebu
2 Comments
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 36537436
For the first order, use Scope_Identity() instead of @@Identity.

For the second order, use @CompanyID which was saved before inserting the first order.


Now, let's get real....


You really need to pull product out of the Orders table:

Order (OrderNo, CompanyID, OrderDate)
OrderLine (OrderNo, LineNo, ProdID, ProdQty, Discount


Also, you need to think about your procedure design.  You can't add a second order for the same customer.  Even if you change it to add a second product to the same order, then you need to pass two sets of product info in the parameters. What then, if you want to add a third product to the order?

I suggest creating three independent procs:
  addCustomer
  addOrder
  addOrderLine


If desired, add a fourth proc to orchestrate the first three.






 



















 
0
 

Author Closing Comment

by:Zbiebu
ID: 36541312
Thanks.
I have a go at that.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

18 Experts available now in Live!

Get 1:1 Help Now