Solved

insert into select

Posted on 2013-06-04
8
345 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
Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.

691 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