Solved

insert into select

Posted on 2013-06-04
8
336 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
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 24

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
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.

 
LVL 24

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 40

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 24

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Accessing SQL Server Instance from outside the network 29 102
Need help on t-sql 2012 10 53
What's wrong with this T-SQL Foreign Key? 7 41
sql server tables from access 18 17
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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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

785 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