3

ローカルデータベースでopenqueryからsubqueryに変更するときに、このクエリが最後の括弧で「Incorrect Syntax near')'」を返すのはなぜですか?

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 に答える