Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

insert into select

Posted on 2013-06-04
8
Medium Priority
?
349 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

609 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