此SQL语句不是查询

时间:2013-11-12 17:02:57

标签: sql sql-server ssis

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

1 个答案:

答案 0 :(得分:3)

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

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

    IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo]。[TempInvoices]')并输入(N'U')) DROP TABLE [dbo]。[TempInvoices] GO

    创建表[TempInvoices] ( ProductNumber varchar(15)not null, CustomerID varchar(25)不为null, CustomerDept varchar(4)null, InvoiceDate datetime2不为null, AgreementID int null, 数量int不为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]')并输入(N'U')) DROP TABLE [dbo]。[TempAgreements] GO

    创建表[TempAgreements] ( AgreementID int, RowNum int )

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

  4. 按照@Zane的说明操作,并使用这些临时表来设置后续数据流任务所需的一些其他查询。看起来代码中有多个路径,基于临时表中的一些值,因此包中可能有多条路径。

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

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