Solved

trying to insert output OrderID

Posted on 2013-05-27
2
465 Views
Last Modified: 2013-05-27
I am trying to insert bot Orders and OrderDetails from one table into 2 tables but cannot remember how to use the Output. inserted in the second insert into statement.

Any ideas?

INSERT INTO WW.ClientOrders
                      (OrderDate, ClientOrderNumber, Client, ResellerName, PaymentOption)
SELECT     MIN(OrderDate) AS OrderDate, ClientOrderNumber, MIN(Client) AS Client, MIN(ResellerName) AS ResellerName, MIN(PaymentOption) AS PaymentOption
FROM         WW.ClientOrderImport
GROUP BY ClientOrderNumber
Output inserted.OrderID

INSERT INTO WW.ClientOrderDetails
           (DeliveryDateRequest, Product, GrowthStage, StandingOrder, Comments, 
		Quantity, ClientSpecialDiscount, ResellerSpecialDiscount,OrderID)
SELECT     i.DeliveryDateRequest, i.Product, i.GrowthStage, i.StandingOrder, i.Comments, 
i.Quantity, i.ClientSpecialDiscount, i.ResellerSpecialDiscount, 
                      inserted.OrderID
FROM         WW.ClientOrderImport AS i INNER JOIN
                      WW.ClientOrders AS o ON i.ClientOrderNumber = o.ClientOrderNumber

Open in new window

0
Comment
Question by:Shawn
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 39199844
You need to insert them into a temp table


INSERT INTO WW.ClientOrders
                      (OrderDate, ClientOrderNumber, Client, ResellerName, PaymentOption)
Output inserted.OrderID into @TmpTable
SELECT     MIN(OrderDate) AS OrderDate, ClientOrderNumber, MIN(Client) AS Client, MIN(ResellerName) AS ResellerName, MIN(PaymentOption) AS PaymentOption
FROM         WW.ClientOrderImport
GROUP BY ClientOrderNumber

then you can use the values from this table
0
 
LVL 1

Author Closing Comment

by:Shawn
ID: 39199860
great, thank you!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

867 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

22 Experts available now in Live!

Get 1:1 Help Now