简体   繁体   English

使用数据库链接调用 oracle function

[英]Calling oracle function using database link

I have created a oracle function called getEmployee(id in varchar) in my remote database and I'm trying to call it from my local database using database link.我在远程数据库中创建了一个名为getEmployee(id in varchar)的 oracle function ,我正在尝试使用数据库链接从本地数据库中调用它。

In getEmployee , I'm trying to return a cursor with employee data.(Table: Employee (ID, Name, address)):getEmployee ,我试图返回带有员工数据的 cursor。(表:员工(ID,姓名,地址)):

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

How can I get the result set with column name (ID, Name, address)?如何获得带有列名(ID、名称、地址)的结果集?

According to Contrad, I changed my local function like this;根据Contrad,我像这样更改了我的本地function;

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

OUTPUT Schema.SomeRefCursor;

BEGIN 

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

  RETURN OUTPUT;
END;  

But, when I call this function from Java Code, the following error is raised:但是,当我从 Java 代码中调用此 function 时,会出现以下错误:

"ORA-24338: statement handle not executed" “ORA-24338: 语句句柄未执行”

Fetching the Ref Cursor at Remote site:在远程站点获取 Ref Cursor:

Let's say we have two sites involved in Distributed transaction, Server1 and Server2.假设我们有两个站点涉及分布式事务,Server1 和 Server2。 The Ref Cursor opened on Server1 procedure, cannot be fetched at Server2 site.在 Server1 过程中打开的 Ref Cursor 无法在 Server2 站点获取。 If we try to fetch this cursor oracle raises an exception:如果我们尝试获取此 cursor oracle 会引发异常:

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

“We cannot use the Ref Cursor over DBLink” “我们不能在 DBLink 上使用 Ref Cursor”

Solutions:解决方案:

  1. Use PL-SQL Data table.使用 PL-SQL 数据表。 OR或者
  2. Provide select grant and use select command over DBLink from initiator site instead of opening the Cursor.提供 select 授权并从发起者站点通过 DBLink 使用 select 命令,而不是打开 Cursor。

Source: Distributed transaction in Oracle (Over Oracle DBLink)来源: 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. 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. I believe the only way to do this in Java is to wrap the function call in a bit of "procedural" code.我相信在 Java 中做到这一点的唯一方法是将 function 调用包装在一些“程序”代码中。 I don't have Oracle in front of me so this is some guesswork:我面前没有 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