2017-08-02 7 views
0

への不正な参照、私はSybase IQの上でこのスクリプトを実行します。「SQL Anywhereのエラー-824:相関名への不正な参照SQL Anywhereのエラー-824:相関名tableNameの

declare @YEAR int=2017 
declare @MON int=6 
declare @DAY int=7 

update MainTable 

set MainTable.Amount=(X.Number+Y.Number), 
    MainTable.Total=(X.Total+Y.Total) 

from (select 'Number'= count(*), 'Total'=case when SUM(T1_Total) is null then 0 else SUM(T1_Total) end 
     from Table1  
     where T1_Account_NO=MainTable.Account_NO 
     and [email protected] and [email protected] and [email protected]) X, 
    (select 'Number'= count(*), 'Total'=case when SUM(T2_TOTAL) is null then 0 else SUM(T2_TOTAL) end 
     from Table2 where T2_Account_NO = MainTable.Account_NO 
     and [email protected] and [email protected] and [email protected])Y 

where  [email protected] 
     and MainTable.MON = @MON 
     and [email protected] 

私はこのようなエラーが発生しましたMainTable」

は、どのように私はこの問題を上回ることができますか?

update Maintable 
set ... 
from MainTable, 
     (select ...)X, 
     (select ...)Y 
where ... 


注:

答えて

0

あなたはたとえば、from句にMainTableを追加しようとしたことがあり、私は '外部' の相関名に許可していないのSybase ASE、と連携しますSQLAnywhereに似たような制限があるのだろうか?あなたはMainTableは、クエリの最上位レベルに出加入引っ張ったらどうなりますか

、例えば:

declare @YEAR int=2017 
declare @MON int=6 
declare @DAY int=7 

update MainTable 

set MainTable.Amount=(X.Number+Y.Number), 
    MainTable.Total=(X.Total+Y.Total) 

from (select T1_account_NO, 'Number'= count(*), 'Total'=case when SUM(T1_Total) is null then 0 else SUM(T1_Total) end 
     from Table1  
     where [email protected] and [email protected] and [email protected] 
     group by T1_Account_NO) X, 
    (select T2_Account_NO, 'Number'= count(*), 'Total'=case when SUM(T2_TOTAL) is null then 0 else SUM(T2_TOTAL) end 
     from Table2 where [email protected] and [email protected] and [email protected] 
     group by T2_Account_NO)Y 

where  [email protected] 
     and MainTable.MON = @MON 
     and [email protected] 
     and MainTable.Account_NO = X.T1_Account_NO 
     and MainTable.Account_NO = Y.T2_Account_NO 

派生テーブルは、現在の大規模なセットを生成する場合は、1つの潜在的パフォーマンス関連の欠点は次のようになりますMainTableと結合されないレコード(SQLAnywhereクエリエンジンが何らかの方法でクエリをフラット化できない限り...)。


これが派生テーブルに「外部」相関名を許可しないの問題である場合は、別の(明白な?)ソリューションは、表1 /表2にMainTableに参加した結果から、カップル#TEMPテーブルを作成することです、 Maintableの更新を#tempテーブルとの結合として実行します。 [可能性として、データボリュームが十分な大きさであれば#tempテーブルのインデックスを作成することができます。

+0

はい私はそれを試みました、同じエラーがスローされました。 –

+0

Hmmm、SQLAnywhereがASEのようなもので、相関名が '派生テーブルに渡されました'、つまり派生テーブルが外部テーブル/ビュー/相関名を参照することを許可されていないかどうか疑問に思っていますか?それが事実なら、別のアイデアで私の答えを更新させてください...私に数分を与えてください... – markp

+0

あなたに有用なアドバイスをお願いします。 –

0

MainTableをFROM句に追加しましたか?

+0

はい私はそれを試しました。 –

0

私はこのように、この問題を上回る:

declare @YEAR int=2017 
declare @MON int=6 
declare @DAY int=7 

update MainTable 
set MainTable.Amount= (X.Number), 
    MainTable.Total = (X.Total) 
from (select T1_Account_NO,'Number'= count(*), 'Total'=case when SUM(T1_Total) is null then 0 else SUM(T1_Total) end 
     from Table1  
     where [email protected] and [email protected] and [email protected] 
     group by T1_Account_NO) X, 
where X.T1_Account_NO=MainTable.Account_NO 
     and [email protected] 
     and MainTable.MON = @MON 
     and [email protected] 


update MainTable 
set MainTable.Amount= coalesce(MainTable.Amount,0)+(Y.Number), 
    MainTable.Total = coalesce(MainTable.Total,0)+(Y.Total) 
    (select T2_Account_NO,'Number'= count(*), 'Total'=case when SUM(T2_TOTAL) is null then 0 else SUM(T2_TOTAL) end 
     from Table2 
     where [email protected] and [email protected] and [email protected] 
     group by T2_Account_NO) Y 

where  [email protected] 
     and MainTable.MON = @MON 
     and [email protected] 
     and Y.T2_Account_NO = MainTable.Account_NO 

私は更新スクリプトを二つの部分に区切られています。

関連する問題