-3

因此,我删除了与该主题相关的最后一个问题,因为我认为我的逻辑已经解决。但是现在当我尝试构建一个 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
4

1 回答 1

3

我的建议如下 - 再次,不知道完整的工作流程,但这是我设计它的方式。

  1. 创建一个包,首先使用执行 SQL 任务检查表 [TempInvoices] 是否存在,如果不存在(它不应该存在),则创建它:

    IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TempInvoices]') AND type in (N'U')) DROP TABLE [dbo].[TempInvoices] GO

    CREATE TABLE [TempInvoices](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 )

  2. 重复第一步,但对于 [tempAgreements] 表:

    IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TempAgreements]') AND type in (N'U')) DROP TABLE [dbo].[TempAgreements] GO

    CREATE TABLE [TempAgreements] (AgreementID int, RowNum int)

  3. 设置数据流任务来填充您的表。您可以添加已包含在用于获取 OLE DB 数据源的 SQL 查询中的查询语言。确保在属性中将“延迟验证”设置为 True。

  4. 按照@Zane 的指示,使用这些临时表设置后续数据流任务所需的一些额外查询。根据临时表中的一些值,您的代码中似乎有多个路径,因此包中可能有多个路径。

  5. 最后,设置最终数据流任务来填充 [AgreementsInvoicesPivot] 表。

这就是我设想的构建它的方式 - 希望这会有所帮助。

于 2013-11-12T19:34:15.417 回答