I've been stuck on this all afternoon and finally give up. I need to create monthly invoices for multiple customers regularly every month.
我整个下午都困在这里,最后放弃了。我需要每月定期为多个客户开具发票。
So I have taken the data from [Customers - Main] table and create all the [INVOICES] rows that I need to. However, I cannot get [Customers - Invoices] to populate each invoice correctly. What should happen is that [Invoices - Stock Link] gets populated with the correct information from [Customers - Invoices]. Currently each customers items are all populating 1 invoice.
因此,我从[Customers - Main]表中获取数据,并创建所有需要的[发票]行。但是,我不能让[客户-发票]正确填写每个发票。应该发生的是[发票-股票链接]从[客户-发票]中得到正确的信息。目前每个客户的项目都是填充1份发票。
Here is the code I've reached so far and any help would be gratefully received.
这是我到目前为止所达成的准则,任何帮助都会得到感激。
ALTER PROCEDURE [aa test]
AS
INSERT INTO dbo.[INVOICES]
(
CompanyName, InvoiceDate, PurchaseOrderNo, Terms
, JobNumber, PrintableNotes, Initials
)
SELECT dbo.[CUSTOMERS - Main].CompanyName
, DATEADD(d, - 15, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0)) AS Expr1
, 'test3' AS pono, 7 AS terms, 0 AS jobno
, 'We will attempt to collect this invoice by Direct Debit' AS printnotes
, 'KA' AS initials
FROM dbo.[CUSTOMERS - Main]
INNER JOIN dbo.[CUSTOMERS - Invoices]
ON dbo.[CUSTOMERS - Main].CustID = dbo.[CUSTOMERS - Invoices].CustID
WHERE (dbo.[CUSTOMERS - Invoices].Annual <> 1) And
(dbo.[CUSTOMERS - Invoices].DayofMonth = 15)
GROUP BY dbo.[CUSTOMERS - Main].CompanyName
SELECT @endInvoice=MAX(InvoiceNo) FROM INVOICES
INSERT INTO dbo.[INVOICES - Stock Link] (InvoiceNo, StockID, SalePrice)
SELECT @endinvoice, StockID, Price
FROM dbo.[CUSTOMERS - Invoices]
1 个解决方案
#1
5
You can use an OUTPUT
clause as mentioned by Nikola Markovinović:
您可以使用一个输出由尼古拉Markovinović条款如前所述:
ALTER PROCEDURE [aa test]
AS
-- Setup storage for the inserted keys
DECLARE @INVOICES TABLE (InvoiceNo int not null primary key)
INSERT INTO dbo.[INVOICES]
(
CompanyName, InvoiceDate, PurchaseOrderNo, Terms
, JobNumber, PrintableNotes, Initials
)
-- Grab the inserted keys
OUTPUT INSERTED.InvoiceNo INTO @INVOICES
SELECT dbo.[CUSTOMERS - Main].CompanyName
, DATEADD(d, - 15, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0)) AS Expr1
, 'test3' AS pono, 7 AS terms, 0 AS jobno
, 'We will attempt to collect this invoice by Direct Debit' AS printnotes
, 'KA' AS initials
FROM dbo.[CUSTOMERS - Main]
INNER JOIN dbo.[CUSTOMERS - Invoices]
ON dbo.[CUSTOMERS - Main].CustID = dbo.[CUSTOMERS - Invoices].CustID
WHERE (dbo.[CUSTOMERS - Invoices].Annual <> 1) And
(dbo.[CUSTOMERS - Invoices].DayofMonth = 15)
GROUP BY dbo.[CUSTOMERS - Main].CompanyName
INSERT INTO dbo.[INVOICES - Stock Link] (InvoiceNo, StockID, SalePrice)
-- Not sure where StockID and Price come from
SELECT a.InvoiceNo, a.StockID, a.Price
FROM dbo.[CUSTOMERS - Invoices] a
-- Join on the keys from above
JOIN @INVOICES b ON a.InvoiceNo = b.InvoiceNo
#1
5
You can use an OUTPUT
clause as mentioned by Nikola Markovinović:
您可以使用一个输出由尼古拉Markovinović条款如前所述:
ALTER PROCEDURE [aa test]
AS
-- Setup storage for the inserted keys
DECLARE @INVOICES TABLE (InvoiceNo int not null primary key)
INSERT INTO dbo.[INVOICES]
(
CompanyName, InvoiceDate, PurchaseOrderNo, Terms
, JobNumber, PrintableNotes, Initials
)
-- Grab the inserted keys
OUTPUT INSERTED.InvoiceNo INTO @INVOICES
SELECT dbo.[CUSTOMERS - Main].CompanyName
, DATEADD(d, - 15, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0)) AS Expr1
, 'test3' AS pono, 7 AS terms, 0 AS jobno
, 'We will attempt to collect this invoice by Direct Debit' AS printnotes
, 'KA' AS initials
FROM dbo.[CUSTOMERS - Main]
INNER JOIN dbo.[CUSTOMERS - Invoices]
ON dbo.[CUSTOMERS - Main].CustID = dbo.[CUSTOMERS - Invoices].CustID
WHERE (dbo.[CUSTOMERS - Invoices].Annual <> 1) And
(dbo.[CUSTOMERS - Invoices].DayofMonth = 15)
GROUP BY dbo.[CUSTOMERS - Main].CompanyName
INSERT INTO dbo.[INVOICES - Stock Link] (InvoiceNo, StockID, SalePrice)
-- Not sure where StockID and Price come from
SELECT a.InvoiceNo, a.StockID, a.Price
FROM dbo.[CUSTOMERS - Invoices] a
-- Join on the keys from above
JOIN @INVOICES b ON a.InvoiceNo = b.InvoiceNo