Solved

insert into select

Posted on 2013-06-04
8
329 Views
Last Modified: 2013-06-04
I am having trouble remembering how to do this. I need to insert the inserted InvoiceID into the ClientInvoiceDetails table below and the variable doesn't seem to be passing. I also need to use the InvoiceID in the the Access front end. That's why I have the @TmpTable.

Any ideas where I am going wrong?

@OrderID Int

AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

DECLARE @TmpTable table(InvoiceID int);
DECLARE @InvoiceID Int;

INSERT INTO WW.ClientInvoices
                      (OrderID, InvoiceDate,  OrderDate, ClientOrderNumber, PaymentOptionID, ClientID, InvoiceAddressID, InvoiceAddressName, InvoiceAddressBlock, 
                      InvoiceAddressPostalCode, InvoiceAddressCity, InvoiceAddressProvince, DeliveryAddressID, DeliveryAddressName, DeliveryAddressBlock, 
                      DeliveryAddressPostalCode, DeliveryAddressCity, DeliveryAddressProvince, TotalPrice, ToatalGST, TotalPST, TotalIncludingTax)
Output inserted.InvoiceID into @TmpTable 

SELECT     OrderID, Getdate(), OrderDate, ClientOrderNumber, PaymentOptionID, ClientID, InvoiceAddressID, InvoiceAddressName, InvoiceAddressBlock, 
                      InvoiceAddressPostalCode, InvoiceAddressCity, InvoiceAddressProvince, DeliveryAddressID, DeliveryAddressName, DeliveryAddressBlock, 
                      DeliveryAddressPostalCode, DeliveryAddressCity, DeliveryAddressProvince, TotalPrice, ToatalGST, TotalPST, TotalIncludingTax
FROM         WW.ClientOrders
WHERE     (OrderID = @OrderID)
INSERT INTO WW.ClientInvoiceDetails
(InvoiceID, DeliveryDateRequest, GrowthStageID, Quantity, 
					ProductID, UnitPrice,  TotalPrice, SpecialDiscount)
SELECT     @InvoiceID, DeliveryDateRequest, GrowthStageID, Quantity,  
                     ProductID, UnitPrice, TotalPrice, SpecialDiscount
FROM         WW.ClientOrderDetails
WHERE     (OrderID = @OrderID)

Open in new window

0
Comment
Question by:Shawn
  • 3
  • 3
  • 2
8 Comments
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
Can you check this?
@OrderID Int

AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

DECLARE @TmpTable table(InvoiceID int);
DECLARE @InvoiceID Int;

INSERT INTO WW.ClientInvoices
                      (OrderID, InvoiceDate,  OrderDate, ClientOrderNumber, PaymentOptionID, ClientID, InvoiceAddressID, InvoiceAddressName, InvoiceAddressBlock, 
                      InvoiceAddressPostalCode, InvoiceAddressCity, InvoiceAddressProvince, DeliveryAddressID, DeliveryAddressName, DeliveryAddressBlock, 
                      DeliveryAddressPostalCode, DeliveryAddressCity, DeliveryAddressProvince, TotalPrice, ToatalGST, TotalPST, TotalIncludingTax)
SELECT     OrderID, Getdate(), OrderDate, ClientOrderNumber, PaymentOptionID, ClientID, InvoiceAddressID, InvoiceAddressName, InvoiceAddressBlock, 
                      InvoiceAddressPostalCode, InvoiceAddressCity, InvoiceAddressProvince, DeliveryAddressID, DeliveryAddressName, DeliveryAddressBlock, 
                      DeliveryAddressPostalCode, DeliveryAddressCity, DeliveryAddressProvince, TotalPrice, ToatalGST, TotalPST, TotalIncludingTax
FROM         WW.ClientOrders
WHERE     (OrderID = @OrderID)

INSERT into @TmpTable 
SELECT @InvoiceID


INSERT INTO WW.ClientInvoiceDetails
(InvoiceID, DeliveryDateRequest, GrowthStageID, Quantity, 
					ProductID, UnitPrice,  TotalPrice, SpecialDiscount)
SELECT     @InvoiceID, DeliveryDateRequest, GrowthStageID, Quantity,  
                     ProductID, UnitPrice, TotalPrice, SpecialDiscount
FROM         WW.ClientOrderDetails
WHERE     (OrderID = @OrderID)

Open in new window

0
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
Instead of creating the @TmpTable you could just use @InvoiceID variable. I guess you have an Identity column in your ClientInvoices table. To get the Identity of just inserted record you need to use @@IDENTITY variable. So, your statement will be:

DECLARE @InvoiceID Int;

INSERT INTO WW.ClientInvoices
                      (OrderID, InvoiceDate,  OrderDate, ClientOrderNumber, PaymentOptionID, ClientID, InvoiceAddressID, InvoiceAddressName, InvoiceAddressBlock, 
                      InvoiceAddressPostalCode, InvoiceAddressCity, InvoiceAddressProvince, DeliveryAddressID, DeliveryAddressName, DeliveryAddressBlock, 
                      DeliveryAddressPostalCode, DeliveryAddressCity, DeliveryAddressProvince, TotalPrice, ToatalGST, TotalPST, TotalIncludingTax)
SELECT     OrderID, Getdate(), OrderDate, ClientOrderNumber, PaymentOptionID, ClientID, InvoiceAddressID, InvoiceAddressName, InvoiceAddressBlock, 
                      InvoiceAddressPostalCode, InvoiceAddressCity, InvoiceAddressProvince, DeliveryAddressID, DeliveryAddressName, DeliveryAddressBlock, 
                      DeliveryAddressPostalCode, DeliveryAddressCity, DeliveryAddressProvince, TotalPrice, ToatalGST, TotalPST, TotalIncludingTax
FROM         WW.ClientOrders
WHERE     (OrderID = @OrderID)

SET @InvoiceID = @@IDENTITY

INSERT INTO WW.ClientInvoiceDetails
(InvoiceID, DeliveryDateRequest, GrowthStageID, Quantity, 
					ProductID, UnitPrice,  TotalPrice, SpecialDiscount)
SELECT     @InvoiceID, DeliveryDateRequest, GrowthStageID, Quantity,  
                     ProductID, UnitPrice, TotalPrice, SpecialDiscount
FROM         WW.ClientOrderDetails
WHERE     (OrderID = @OrderID)

Open in new window

0
 
LVL 1

Author Comment

by:Shawn
Comment Utility
Sharath_123.
getting the error: Cannot insert the value NULL into column 'InvoiceID', table 'araxiSQLsite.WW.ClientInvoiceDetails'; column does not allow nulls. INSERT fails.

 chaau.
sp executes but the identity value isn't rendered. (can't retrieve it from access).
0
 
LVL 24

Expert Comment

by:chaau
Comment Utility
I did not understand your last statement. What do you mean by
(can't retrieve it from access).

Can you provide the whole SP script?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 40

Expert Comment

by:Sharath
Comment Utility
You did not assign any value to the variable @InvoiceID. Assign some value and see if it is working.
@OrderID Int

AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

DECLARE @TmpTable table(InvoiceID int);
DECLARE @InvoiceID Int;
SELECT @InvoiceID = 100

INSERT INTO WW.ClientInvoices
                      (OrderID, InvoiceDate,  OrderDate, ClientOrderNumber, PaymentOptionID, ClientID, InvoiceAddressID, InvoiceAddressName, InvoiceAddressBlock, 
                      InvoiceAddressPostalCode, InvoiceAddressCity, InvoiceAddressProvince, DeliveryAddressID, DeliveryAddressName, DeliveryAddressBlock, 
                      DeliveryAddressPostalCode, DeliveryAddressCity, DeliveryAddressProvince, TotalPrice, ToatalGST, TotalPST, TotalIncludingTax)
SELECT     OrderID, Getdate(), OrderDate, ClientOrderNumber, PaymentOptionID, ClientID, InvoiceAddressID, InvoiceAddressName, InvoiceAddressBlock, 
                      InvoiceAddressPostalCode, InvoiceAddressCity, InvoiceAddressProvince, DeliveryAddressID, DeliveryAddressName, DeliveryAddressBlock, 
                      DeliveryAddressPostalCode, DeliveryAddressCity, DeliveryAddressProvince, TotalPrice, ToatalGST, TotalPST, TotalIncludingTax
FROM         WW.ClientOrders
WHERE     (OrderID = @OrderID)

INSERT into @TmpTable 
SELECT @InvoiceID


INSERT INTO WW.ClientInvoiceDetails
(InvoiceID, DeliveryDateRequest, GrowthStageID, Quantity, 
					ProductID, UnitPrice,  TotalPrice, SpecialDiscount)
SELECT     @InvoiceID, DeliveryDateRequest, GrowthStageID, Quantity,  
                     ProductID, UnitPrice, TotalPrice, SpecialDiscount
FROM         WW.ClientOrderDetails
WHERE     (OrderID = @OrderID)

Open in new window

0
 
LVL 1

Author Comment

by:Shawn
Comment Utility
I trigger the sp via ms access and need to retrieve the InvoiceID.
here is the whole script.

USE [araxiSQLsite]
GO
/****** Object:  StoredProcedure [WW].[sp_ClientInvoice]    Script Date: 06/04/2013 18:26:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [WW].[sp_ClientInvoice]
      
@OrderID Int

AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;


DECLARE @InvoiceID Int;


INSERT INTO WW.ClientInvoices
                      (OrderID, InvoiceDate,  OrderDate, ClientOrderNumber, PaymentOptionID, ClientID, InvoiceAddressID, InvoiceAddressName, InvoiceAddressBlock, 
                      InvoiceAddressPostalCode, InvoiceAddressCity, InvoiceAddressProvince, DeliveryAddressID, DeliveryAddressName, DeliveryAddressBlock, 
                      DeliveryAddressPostalCode, DeliveryAddressCity, DeliveryAddressProvince, TotalPrice, ToatalGST, TotalPST, TotalIncludingTax)
SELECT     OrderID, Getdate(), OrderDate, ClientOrderNumber, PaymentOptionID, ClientID, InvoiceAddressID, InvoiceAddressName, InvoiceAddressBlock, 
                      InvoiceAddressPostalCode, InvoiceAddressCity, InvoiceAddressProvince, DeliveryAddressID, DeliveryAddressName, DeliveryAddressBlock, 
                      DeliveryAddressPostalCode, DeliveryAddressCity, DeliveryAddressProvince, TotalPrice, ToatalGST, TotalPST, TotalIncludingTax
FROM         WW.ClientOrders
WHERE     (OrderID = @OrderID)

SET @InvoiceID = @@IDENTITY

INSERT INTO WW.ClientInvoiceDetails
(InvoiceID, DeliveryDateRequest, GrowthStageID, Quantity, 
					ProductID, UnitPrice,  TotalPrice, SpecialDiscount)
SELECT     @InvoiceID, DeliveryDateRequest, GrowthStageID, Quantity,  
                     ProductID, UnitPrice, TotalPrice, SpecialDiscount
FROM         WW.ClientOrderDetails
WHERE     (OrderID = @OrderID)

SET NOCOUNT OFF -- for final select




END 

Open in new window

0
 
LVL 24

Expert Comment

by:chaau
Comment Utility
You have two options:
1. Add an output parameter.
ALTER PROCEDURE [WW].[sp_ClientInvoice]
      
@OrderID Int,
@InvoiceID Int OUTPUT
AS ....

Open in new window


2. (much easier) Add this statement as a last statement in your existing SP code (just before the last END):

SELECT @InvoiceID

Open in new window

0
 
LVL 1

Author Comment

by:Shawn
Comment Utility
2nd option worked. 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.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

763 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

12 Experts available now in Live!

Get 1:1 Help Now