3

为什么从 openquery 更改为本地数据库上的子查询时,此查询在最后一个括号中返回“')' 附近的语法不正确?

您可以看到 openquery 之前开始(并且有效)的注释行

select right(rtrim(a.vouchernumber),9) as voucherkey
, a.vouchernumber
, vendorkey
, rtrim(invoicenumber) as invoicenumber
, invoicedate
, duedate
, documenttype
, PostDate
, qty
, amt = 
    CASE 
        WHEN documenttype = 'D' THEN -(amt)
        WHEN b.transactiontype = 'V' THEN -(amt)
    ELSE 
        amt 
    End
, extendedlineamt
, intercompanyid
, acct
, NaturalAcct
, c.segmentdescription as NaturalAcctDesc
, Dept
, f.segmentdescription as DeptDesc
, Site
, d.segmentdescription as SiteDesc
, Project
, e.segmentdescription as ProjDesc
, b.*
, rtrim(vendorname) as vendorname
, rtrim(vendoraddress1) as vendoraddress1
, vendoraddress2
, vendoraddress3
, vendorcity
, rtrim(vendorstate) as vendorstate
, rtrim(vendorzipcode) as vendorzipcode
, rtrim(vendoraddress1) + ' ' + rtrim(vendoraddress2) +' ' + rtrim(vendorcity) + ', ' + rtrim(vendorstate) as VendorAddress
, Dept + '.' + Site + '.' + Project as ProjectId 
--from openquery (LinkedServer,
--  'select a.vouchernumber
from  (
    select a.vouchernumber
    , a.vendorkey
    , a.invoicenumber
    , a.invoicedate
    , a.duedate
    , a.documenttype
    , a.recdate as PostDate
    , b.qty
    , b.amt
    , b.extendedlineamt
    , b.intercompanyid
    , b.acct
    , Substring(Acct, 1, 4) as NaturalAcct
    , Substring(Acct,8, 3 ) as Site
    , Substring(Acct,11, 4 ) as Project
    , Substring(Acct,5,3) as Dept
    , v.vendorname
    , v.vendoraddress1
    , v.vendoraddress2
    , v.vendoraddress3
    , v.vendorcity
    , v.vendorstate
    , v.vendorzipcode 
from aphdr a 
join aplin b 
    on a.vouchernumber = b.vouchernumber
left join apvend v 
    on a.vendorkey = v.vendorkey
where a.recdate between '2011-01-01' and '2012-10-02' 
    and cast(Substring(Acct,11, 4 ) as varchar(4)) like 'VIR'
    and left(a.vouchernumber,1) <> 'G'
    and Company = 'ASSFD'
)
4

2 回答 2

4

您需要为子查询设置别名。

在查询末尾添加别名。

于 2012-10-04T17:56:33.073 回答
3

您需要给子查询一个别名(或名称)。

在这里,我刚刚将 tbl1 添加到最后

select right(rtrim(a.vouchernumber),9) as voucherkey
, a.vouchernumber
, vendorkey
, rtrim(invoicenumber) as invoicenumber
, invoicedate
, duedate
, documenttype
, PostDate
, qty
, amt = 
    CASE 
        WHEN documenttype = 'D' THEN -(amt)
        WHEN b.transactiontype = 'V' THEN -(amt)
    ELSE 
        amt 
    End
, extendedlineamt
, intercompanyid
, acct
, NaturalAcct
, c.segmentdescription as NaturalAcctDesc
, Dept
, f.segmentdescription as DeptDesc
, Site
, d.segmentdescription as SiteDesc
, Project
, e.segmentdescription as ProjDesc
, b.*
, rtrim(vendorname) as vendorname
, rtrim(vendoraddress1) as vendoraddress1
, vendoraddress2
, vendoraddress3
, vendorcity
, rtrim(vendorstate) as vendorstate
, rtrim(vendorzipcode) as vendorzipcode
, rtrim(vendoraddress1) + ' ' + rtrim(vendoraddress2) +' ' + rtrim(vendorcity) + ', ' + rtrim(vendorstate) as VendorAddress
, Dept + '.' + Site + '.' + Project as ProjectId 
--from openquery (LinkedServer,
--  'select a.vouchernumber
from  (
    select a.vouchernumber
    , a.vendorkey
    , a.invoicenumber
    , a.invoicedate
    , a.duedate
    , a.documenttype
    , a.recdate as PostDate
    , b.qty
    , b.amt
    , b.extendedlineamt
    , b.intercompanyid
    , b.acct
    , Substring(Acct, 1, 4) as NaturalAcct
    , Substring(Acct,8, 3 ) as Site
    , Substring(Acct,11, 4 ) as Project
    , Substring(Acct,5,3) as Dept
    , v.vendorname
    , v.vendoraddress1
    , v.vendoraddress2
    , v.vendoraddress3
    , v.vendorcity
    , v.vendorstate
    , v.vendorzipcode 
from aphdr a 
join aplin b 
    on a.vouchernumber = b.vouchernumber
left join apvend v 
    on a.vendorkey = v.vendorkey
where a.recdate between '2011-01-01' and '2012-10-02' 
    and cast(Substring(Acct,11, 4 ) as varchar(4)) like 'VIR'
    and left(a.vouchernumber,1) <> 'G'
    and Company = 'ASSFD'
) tbl1
于 2012-10-04T17:55:39.353 回答