簡體   English   中英

使用數據庫鏈接調用 oracle function

[英]Calling oracle function using database link

我在遠程數據庫中創建了一個名為getEmployee(id in varchar)的 oracle function ,我正在嘗試使用數據庫鏈接從本地數據庫中調用它。

getEmployee ,我試圖返回帶有員工數據的 cursor。(表:員工(ID,姓名,地址)):

SELECT schema.getEmployee@dblink(id) 
  FROM DUAL;

如何獲得帶有列名(ID、名稱、地址)的結果集?

根據Contrad,我像這樣更改了我的本地function;

FUNCTION LocalGetEmployee(ID in varchar2)
RETURN Schema.SomeRefCursor
AS  

OUTPUT Schema.SomeRefCursor;

BEGIN 

  OUTPUT := schema.getEmployee@dblink(ID);

  RETURN OUTPUT;
END;  

但是,當我從 Java 代碼中調用此 function 時,會出現以下錯誤:

“ORA-24338: 語句句柄未執行”

在遠程站點獲取 Ref Cursor:

假設我們有兩個站點涉及分布式事務,Server1 和 Server2。 在 Server1 過程中打開的 Ref Cursor 無法在 Server2 站點獲取。 如果我們嘗試獲取此 cursor oracle 會引發異常:

[ORA-02055: distributed update operation failed; rollback required
 ORA-24338: statement handle not executed]

“我們不能在 DBLink 上使用 Ref Cursor”

解決方案:

  1. 使用 PL-SQL 數據表。 或者
  2. 提供 select 授權並從發起者站點通過 DBLink 使用 select 命令,而不是打開 Cursor。

來源: Oracle 中的分布式事務(通過 Oracle DBLink)

As far as I can tell your question isn't really about database links but rather how, from a Java client, to call a function that returns a cursor and retrieve the data from that cursor. 我相信在 Java 中做到這一點的唯一方法是將 function 調用包裝在一些“程序”代碼中。 我面前沒有 Oracle 所以這是一些猜測:

String fncall = "begin ? :=  schema.getEmployee@dblink(?) end";
CallableStatement stm = con.prepareCall(fncall);
stm.registerOutParameter(1, Types.CURSOR);
stm.setInt(2, 123);
stm.execute();
ResultSet rs = (ResultSet) stm.getObject(1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM