Solved

trying to insert output OrderID

Posted on 2013-05-27
2
472 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

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

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

861 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