2015-08-27 44 views
1

我有下面的代码来展示一下我“想”在一个存储过程来完成:与子查询的SELECT语句对两个数据库

select * from 
    ( 
    select to_char(sum(aa.amount)) 
    from additional_amount aa, status st 
    where aa.int_tran_id = st.int_tran_id 
    and st.stage in ('ACHPayment_Confirmed') 
    and aa.entry_timestamp > ( 
           select to_date(trunc(last_day(add_months(sysdate,-1))+1), 'DD-MON-RR') AS "day 1" 
           from dual 
          ) 

    ) 
    UNION ALL 
    (
    select distinct it.debit_acct as "debit_accounts" 
    from internal_transactions it 
    where it.debit_acct IN (select texe_cnasupro 
           from service.kndtexe, service.kndtctc 
           where texe_cncclipu = tctc_cncclipu 
           and tctc_cntipcli = 'C' 
          ) 
    ) 
     union all 
    (select distinct it.credit_acct as "credit_account" 
    from internal_transactions it 
    where it.credit_acct IN (select texe_cnasupro 
           from service.kndtexe, service.kndtctc 
           where texe_cncclipu = tctc_cncclipu 
           and tctc_cntipcli = 'C' 
          ) 
    ) 
    ; 

输出:

TO_CHAR(SUM(AA.AMOUNT))     
---------------------------------------- 
130250292.22        
6710654504        
0000050334        
2535814905        
0007049560           
5 rows selected 

顶行的输出是我需要在SP作为输出基于以下两个查询,我猜测需要对顶部选择语句进行子查询。

最上面的选择是选择一个表与一个联合对另一个表进行筛选的总和(输出:130250292.22)。

第二个和第三个选择实际上是检查internal_transactions表中的帐户是否注册了服务数据库中对应的两个表,这是同一服务器上的不同数据库(由同一应用程序拥有)。

“服务”数据库中的表不具有与第一次选择相同的公共主键,这是与相同数据库相反的。

谢谢你的帮助!

+0

在另一个数据库上创建数据库链接,并将数据库链接和表的访问权授予第一个数据库模式。 –

+0

我可以从SP所在的第一个数据库访问第二个数据库中的表。我正在访问“服务”。在第二个数据库的代码中。 – QuickDrawMcgraw

回答

0

我不明白你的问题,但我知道你可以简化这个位:

to_date(trunc(last_day(add_months(sysdate,-1))+1), 'DD-MON-RR') AS "day 1" 

这个

trunc (sysdate, 'mm') 

,你不需要选择DUAL做那也是。

and aa.entry_timestamp > trunc (sysdate, 'mm')