?
Solved

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

Posted on 2011-09-14
2
Medium Priority
?
198 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 2000 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

762 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