Solved

trying to insert output OrderID

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

707 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

15 Experts available now in Live!

Get 1:1 Help Now