因此,我删除了与该主题相关的最后一个问题,因为我认为我的逻辑已经解决。但是现在当我尝试构建一个 SSIS 包(或者甚至只是使用导入/导出向导)时,我收到以下错误
This sql statement is not a query
代码如下:
DECLARE @start int, @end int
DECLARE @start_ag int, @end_ag int
DECLARE @custID varchar(25)
DECLARE @custID_final varchar(25)
DECLARE @product varchar(50)
DECLARE @AgreementID int
DECLARE @dept varchar (5)
DECLARE @invDate datetime2
DECLARE @startDate datetime2
DECLARE @endDate datetime2
DECLARE @tempInvoices TABLE
(
ProductNumber varchar(15) not null,
CustomerID varchar(25) not null,
CustomerDept varchar (4) null,
InvoiceDate datetime2 not null,
AgreementID int null,
Quantity int not null,
InvoiceNumber varchar(25) not null,
RowNum int null
)
DECLARE @tempAgreements TABLE
(
AgreementID int,
RowNum int
)
INSERT INTO @tempInvoices
(RowNum, CustomerID, CustomerDept, ProductNumber, Quantity, InvoiceDate, InvoiceNumber)
SELECT ROW_NUMBER() OVER (ORDER BY INVOICE) AS RowNum, Account, Dept, CustomerItem, Qty, InvoiceDate, Invoice
FROM [ensite].[dbo].[UI.CustomerInvoices]
where Suffix = 'GEP'
and InvoiceDate = CONVERT (date, GETDATE() - 1)
SET @custID_final = ''
SELECT @start = 1
SELECT @end = MAX(RowNum) FROM @tempInvoices
WHILE (@start <= @end)
BEGIN
SELECT @custID = CustomerID from @tempInvoices where RowNum = @start
SELECT @dept = CustomerDept from @tempInvoices where RowNum = @start
SELECT @product = ProductNumber from @tempInvoices where RowNum = @start
SELECT @invDate = InvoiceDate from @tempInvoices where RowNum = @start
BEGIN
if @dept <> ''
SET @custID_final = @custID + '-' + @dept
else
SET @custID_final = @custID
END
if @custID_final IN (SELECT DISTINCT CustomerID From [CompuData].[PriceAgreements].[AgreementsCustomersPivot])
BEGIN
INSERT INTO @tempAgreements
(RowNum, AgreementID)
SELECT ROW_NUMBER() OVER (ORDER BY AgreementID) AS RowNum, AgreementID
From [CompuData].[PriceAgreements].[AgreementsCustomersPivot]
WHERE CustomerID = @custID_final
SELECT @start_ag = 1
SELECT @end_ag = MAX(RowNum) FROM @tempAgreements
WHILE (@start_ag <= @end_ag)
BEGIN
SELECT @AgreementID = AgreementID FROM @tempAgreements WHERE RowNum = @start_ag
if @product IN (SELECT DISTINCT ProductNumber FROM [CompuData].[PriceAgreements].[AgreementsItemsPivot] WHERE AgreementID = @AgreementID)
BEGIN
SELECT @startDate = StartDate FROM [CompuData].[PriceAgreements].[PriceAgreemets] WHERE AgreementID = @AgreementID
SELECT @endDate = EndDate FROM [CompuData].[PriceAgreements].[PriceAgreemets] WHERE AgreementID = @AgreementID
if @invDate BETWEEN @startDate AND @endDate
BEGIN
UPDATE @tempInvoices
SET AgreementID = @AgreementID
WHERE RowNum = @start
DELETE FROM @tempAgreements
SELECT @start = @start + 1
END
else
BEGIN
if @start_ag = @end_ag
BEGIN
DELETE FROM @tempInvoices where RowNum = @start
SELECT @start = @start + 1
END
else
BEGIN
SELECT @start_ag = @start_ag + 1
END
END
END
else
BEGIN
if @start_ag = @end_ag
BEGIN
DELETE FROM @tempInvoices where RowNum = @start
SELECT @start = @start + 1
END
else
BEGIN
SELECT @start_ag = @start_ag + 1
END
END
END
END
else
BEGIN
DELETE FROM @tempInvoices where RowNum = @start
SELECT @start = @start + 1
END
END
INSERT INTO [CompuData].[PriceAgreements].[AgreementsInvoicesPivot]
(CustomerID, AgreementID, ProductNumber, Quantity, InvoiceNumber, InvoiceDate)
SELECT CustomerID, AgreementID, ProductNumber, Quantity, InvoiceNumber, InvoiceDate
FROM @tempInvoices