Solved

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

Posted on 2011-09-14
2
196 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
[X]
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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Divide by zero error encountered. 2 45
Delete from table 6 48
Grid querry results 41 83
Replace Dates in query 14 56
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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