2012-09-20 80 views
0

我在交易中插入記錄,然後通過Oracle函數檢索相同的記錄。 oracle函數沒有返回任何記錄。我的數據庫管理員告訴我,Oracle功能不在交易內部運作。我如何解決這個問題?交易中的Oracle函數

例子: 使用Oracle提供

執行一些SQL BEGIN TRANSACTION:

INSERT OWNER (名字,姓氏) VALUES ( 'JOHN', 'SMITH')

獲取(71是示例ID): select * from table(GET_OWNER_DETAILS_FNC(71))

這裏是Oracle函數:

CREATE OR REPLACE FUNCTION "AROH"."GET_OWNER_DETAILS_FNC" (p_owner_information_oid in aroh_owner_information.owner_information_oid%type) 
return OWNER_DETAILS_TABLE_TYPE_FNC 

IS 
PRAGMA AUTONOMOUS_TRANSACTION; 

v_owner_details_set OWNER_DETAILS_TABLE_TYPE_FNC := OWNER_DETAILS_TABLE_TYPE_FNC(); 

CURSOR c_owner_dtls IS 
select oi.owner_information_oid, 
oi.first_name, 
oi.last_name, 
oi.company_name, 
oi.license_information, 
oi.company_ind, 
oi.middle_initial, 
oi.title_type_oid, 
oi.suffix, 
oi.status_type_code, 
oi.primary_phone, 
oi.secondary_phone, 
oi.secondary_phone_type_code, 
oi.primary_phone_type_code, 
oi.email_address, 
oi.comments, 
oi.primary_phone_extension, 
oi.secondary_phone_extension, 
poa.owner_address_oid as primaryaddressid, 
poa.address_type_code as primaryaddresscode, 
poa.address1 as primaryaddress1, 
poa.address2 as primaryaddress2, 
poa.city as primarycity, 
poa.state as primarystate, 
poa.postal_code as primaryzip, 
poa.current_ind as primarycurrent, 
soa.owner_address_oid as secondaryaddressid, 
soa.address_type_code as secondaryaddresscode, 
soa.address1 as secondaryaddress1, 
soa.address2 as secondaryaddress2, 
soa.city as secondarycity, 
soa.state as secondarystate, 
soa.postal_code as secondaryzip, 
soa.current_ind as secondarycurrent, 
( select 
     ( select oa2.owner_information_oid 
      from aroh_owner_aircraft_rlshp oa2 
      where upper(primary_owner) like '%PRIMARY%' 
          and oa2.aircraft_oid = oa1.aircraft_oid 
          and rownum = 1) as prim_owner_oid 
       from aroh_owner_aircraft_rlshp oa1 
       where oa1.owner_information_oid = p_owner_information_oid 
        and rownum = 1 
) as primary_owner_oid, 
(   select 
       case when (upper(primary_owner) like '%PRIMARY%') 
               then 'Y' 
               else 'N' end as isprimary 
       from aroh_owner_aircraft_rlshp 
       where owner_information_oid = p_owner_information_oid 
       and rownum = 1 
) as is_primary 
from aroh_owner_information oi 
inner join (select * 
        from aroh_owner_address 
       where upper(current_ind) = 'Y' 
        and address_type_code = 'OPRIM') poa 
on oi.owner_information_oid = poa.owner_information_oid 
left outer join (select * 
         from aroh_owner_address 
         where upper(current_ind) = 'Y' 
         and address_type_code = 'OSEC') soa 
on oi.owner_information_oid = soa.owner_information_oid 
where oi.owner_information_oid = p_owner_information_oid; 

begin 

For main_row in c_owner_dtls 
loop 
    v_owner_details_set.EXTEND; 
    v_owner_details_set(v_owner_details_set.LAST) := OWNER_DETAILS_TYPE (main_row.owner_information_oid , main_row.first_name , main_row.last_name , main_row.company_name 
    , main_row.license_information , main_row.company_ind , main_row.middle_initial , main_row.title_type_oid , main_row.suffix , main_row.status_type_code , main_row.primary_phone 
    , main_row.secondary_phone , main_row.secondary_phone_type_code , main_row.primary_phone_type_code , main_row.email_address , main_row.comments , main_row.primary_phone_extension 
    , main_row.secondary_phone_extension , main_row.primaryaddressid , main_row.primaryaddresscode , main_row.primaryaddress1 , main_row.primaryaddress2 , main_row.primarycity 
    , main_row.primarystate , main_row.primaryzip , main_row.primarycurrent , main_row.secondaryaddressid , main_row.secondaryaddresscode , main_row.secondaryaddress1 
    , main_row.secondaryaddress2 , main_row.secondarycity , main_row.secondarystate , main_row.secondaryzip , main_row.secondarycurrent , main_row.primary_owner_oid , main_row.is_primary); 
end loop; 

return v_owner_details_set; 

EXCEPTION 
When others 
then dbms_output.put_line ('Oracle error: '||SQLERRM); 

end; 

回答

3

AUTONOMOUS_TRANSACTION編譯指示函數在單獨的事務環境中運行。默認情況下(參見http://docs.oracle.com/cd/B19306_01/server.102/b14220/consist.htm#sthref1972),它使用「讀取已提交」隔離級別,這意味着,當事務查詢數據時,它僅查看在查詢開始前已提交的數據。由於您插入的數據沒有被提交,這意味着函數看不到它。

1

對於Oracle文檔:

AUTONOMOUS_TRANSACTION編譯改變子程序工作的事務中的方式。標記有該編譯指示的子程序可以執行SQL操作並提交或回退這些操作,而無需提交或回滾主事務中的數據。

你可以嘗試擺脫該編譯?

最後,「我的DBA告訴我,Oracle函數不能在一個事務中操作」是值得懷疑的。我不是專家,但我真誠地懷疑這是否正確。