Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2011-09-14
2
195 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Copy Database Wizard 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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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