Solved

insert into select

Posted on 2013-06-04
8
343 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
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 39220795
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 25

Accepted Solution

by:
chaau earned 500 total points
ID: 39220797
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
ID: 39220828
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 25

Expert Comment

by:chaau
ID: 39220847
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
 
LVL 41

Expert Comment

by:Sharath
ID: 39220855
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
ID: 39220886
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 25

Expert Comment

by:chaau
ID: 39220890
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
ID: 39220909
2nd option worked. thank you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

749 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