SQL OpenQuery - 转义行情

时间:2015-12-15 23:18:06

标签: sql sql-server-2014 openquery

尝试了几个小时将其转换为我可以在SQL Server 2014中与where used = 0一起使用的查询(通过ODBC与Progress OpenEdge 10.2B一起使用)。似乎无法正确逃避报价。有人可以提供一些帮助吗?有没有工具可以做到?

(这里有一个名为OPENQUERY的SQL表,但我从这段代码中省略了它)

@tAPBatches

TIA

标记

这是一个给我一个语法错误的例子。这有效,但是" M_AP _"是传递给SP的参数

    DECLARE
    @NoDays AS INT = 30
   ,@Prefix AS VARCHAR(5) = 'M_AP_'

SELECT
                    @Prefix + LTRIM(CAST(gh.[Batch-Number] AS VARCHAR(20))) AS BatchNo
                    ,gh.[Batch-Number] AS BatchNo8
                   , aph.[Reference-number]  AS InvoiceNo
                   ,aph.[Voucher-Number] AS VoucherNo
                   ,aph.[Amount] AS InvoiceTotal
                  ,gh.[Journal-Number] AS JournalNo
                   ,4 AS FacilityID
                   ,CASE aph.[voucher-type]
                        WHEN 'DM' THEN 5 
                        ELSE 1 
                        END AS DocType 
                   ,apb.[Batch-Desc] AS BatchDesc
                    ,apb.[Posting-Date] AS PostingDate
                   ,apb.[Posting-Period]
                   ,apb.[Posting-Fiscal-Year]
                   ,apb.[Batch-Status]
                   ,apb.[Expected-Count]
                   ,apb.[Expected-Amount]
                   ,apb.[Posted-To-GL-By]
                   ,'Broadview' AS FacilityName
                   ,apb.[Date-Closed] AS BatchDate 
                   ,gh.[Posted-by] AS PostUser
                   ,gh.[Posted-Date] AS PostDT
                   ,gh.[Created-Date] AS CreateDT
                   ,gh.[Created-By] AS CreateUser
                   ,aph.[Supplier-Key] AS VendorID 
                   ,sn.[Supplier-Name]
                   ,aph.[Invoice-Date] AS InvoiceDate
                   ,-1 AS Total
                   ,-1 AS Discount
                   ,gh.[Posted-by] AS Username
                    ,CASE gt.[Credit-Debit]
                   WHEN 'CR' THEN LEFT(CAST(gacr.[GL-Acct] AS VARCHAR(20)), 2) + '.' + SUBSTRING(CAST(gacr.[GL-Acct] AS VARCHAR(20)), 3, 6) + '.'
                    + RIGHT(CAST(gacr.[GL-Acct] AS VARCHAR(20)), 3)
                    ELSE NULL
                    END AS GLCreditAcct
                    ,CASE gt.[Credit-Debit]
                   WHEN 'DR' THEN LEFT(CAST(gacr.[GL-Acct] AS VARCHAR(20)), 2) + '.' + SUBSTRING(CAST(gacr.[GL-Acct] AS VARCHAR(20)), 3, 6) + '.'
                    + RIGHT(CAST(gacr.[GL-Acct] AS VARCHAR(20)), 3)
                    ELSE NULL
                    END AS GLDebitAcct
                    ,CASE gt.[Credit-Debit]
                   WHEN 'CR' THEN gacr.[Report-Label] 
                    ELSE NULL
                    END AS GLCreditDesc
                      ,CASE gt.[Credit-Debit]
                   WHEN 'DR' THEN gacr.[Report-Label] 
                    ELSE NULL
                    END AS GLDebitDesc
                   ,'D' AS [Status]
                   ,aph.[PO-Number] AS PoNo
                   ,aph.[Terms-Code] AS TermsCode
                   ,aph.[Due-Date] AS DueDate
                   ,'' AS Comments
                   ,aph.[Discount-Date] AS DiscountDate
                   ,aph.[Discount-Amount] AS DiscountAmount
                   ,aph.[Discount-Taken] AS DiscountTaken
                   ,aph.[Amount] AS APAmount
                   ,gt.[Amount]
                   ,'BA REGULAR     ' AS CheckBookID  --ToDO
                   ,0 AS Transferred
                   ,aph.[voucher-type] AS VoucherType
                   ,gt.[Credit-Debit]
                   ,gacr.[Account-type]
                   ,aph.[Freight-Ref-Num]
                 FROM
                    [Progress].[GAMS1].pub.[GL-Entry-Header] gh
                    INNER JOIN [Progress].[GAMS1].pub.[gl-entry-trailer] gt ON gt.[System-ID] = gh.[System-ID] AND gt.[Origin] = gh.[Origin] AND gt.[Journal-Number] = gh.[Journal-Number] 
                INNER JOIN [Progress].[GAMS1].pub.[apinvhdr] aph ON (gh.[Journal-Number] = aph.[Journal-Number]  
                OR (gh.[Journal-Num-Reversal-Of] = aph.[Journal-Number] AND aph.[Journal-Number] <> '        ' AND gh.[Journal-Num-Reversal-Of] <> '        '))
                                                                    AND gh.[system-id] = aph.[system-id-gl]
                                                                    AND gh.origin = 'inv'  
                                                                    AND gh.[system-id] = 'arcade' 
                INNER JOIN [Progress].[GAMS1].pub.[APInvoiceBatch] apb ON gh.[Batch-number] = apb.[Batch-number]
                                                                          AND apb.[system-id] = 'lehigh'
                                                                          AND apb.[Posted-To-GL] = 1 
                INNER JOIN [Progress].[GAMS1].pub.[GL-accts] gacr ON gacr.[system-id] = gt.[system-id]
                                                                     AND gacr.[Gl-Acct-Ptr] = gt.[GL-Acct-Ptr] 

                INNER JOIN  [Progress].[GAMS1].pub.[suppname] sn  ON  sn.[Supplier-Key] = aph.[Supplier-Key]
                                                                        AND sn.[system-id] = 'arcade'

                WHERE
                    gh.[Posted-Date] > CAST(DATEADD(DAY, -@NoDays, GETDATE()) AS DATE)
                    AND case 
                        when CAST(gh."Posting-Period" as int) < 10 then gh."Posting-Year" + '0' + ltrim(gh."Posting-Period")
                        else gh."Posting-Year" + Ltrim(gh."Posting-Period")
                        end > '201501'
                    AND gh.[Batch-number] NOT IN (SELECT
                                                            BatchNo COLLATE SQL_Latin1_General_CP1_CI_AS
                                                         FROM
                                                            @tAPBatches) 

这样可行,但是当我尝试交换变量时,我会在&#39; +&#39;

附近得到错误的语法
 DECLARE
    @NoDays AS INT = 5
   ,@Prefix AS VARCHAR(5) = 'M_AP_';

  DECLARE
    @InterestDate AS varchar(20)

   SELECT @InterestDate = CAST(CAST(DATEADD(DAY, -@NoDays, GETDATE()) AS DATE) AS VARCHAR(20))

   SELECT * FROM OPENQUERY(PROGRESS, 
   'SELECT TOP 100 ''M_AP_'' + LTRIM(CAST(gh."Batch-Number" AS VARCHAR(20))) AS BatchNo
               , gh."Batch-Number" 

1 个答案:

答案 0 :(得分:0)

OPENQUERY仅支持小于8K的字符串文字查询。如果你有更多你没有在这里展示的代码,你可能会遇到这个限制。确保您的查询少于8000字节,或创建过程或视图以减小查询的大小。

它只接受单个字符串文字...所以如果你试图将字符串和参数连接在一起,它将无法工作。通过使用动态SQL或为过滤器创建支持表或视图,可以some ways to work around this