如何从 OCI 调用 ORACLE 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/591467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:52:32  来源:igfitidea点击:

How do I call an ORACLE function from OCI?

coracleoci

提问by David

I can call an ORACLE stored procedure through OCI in a C program by constructing the SQL command for the command, here's a brief snippet from my code:

我可以通过为命令构造 SQL 命令,在 C 程序中通过 OCI 调用 ORACLE 存储过程,这是我的代码中的一个简短片段:

      /* build sql statement calling stored procedure */
      strcpy ( sql_stmt, "call get_tab_info(:x)" );
      rc = OCIStmtPrepare(p_sql, p_err, sql_stmt,
          (ub4) strlen (sql_stmt), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);

But how do I construct a call (in my C program) to an ORACLE function with the following signature:

但是我如何构造一个调用(在我的 C 程序中)具有以下签名的 ORACLE 函数:

      CREATE OR REPLACE FUNCTION get_seq_number (p_table_name IN VARCHAR2, p_seq_type IN VARCHAR2) 
      RETURN NUMBER IS

To call the function in PL/SQL I would use for example:

要在 PL/SQL 中调用该函数,我将使用例如:

      v_seq := get_seq_number(v_tabname, v_seqtype);

How do I construct the SQL character array (sql_stmt) to call the ORACLE function in my C program ?

如何构造 SQL 字符数组 (sql_stmt) 以在我的 C 程序中调用 ORACLE 函数?

回答by Thomas Jones-Low

Construct your SQL statment as follows

构造您的 SQL 语句如下

strcpy ( sql_stmt, "BEGIN :v_seq := get_seq_number(:v_tabname, :v_seqtype); END;" );

Prepare your statement as previously. Bind the variables by name (including the v_seqas previous in your code and execute the statement. When the procedure completes, the value of :v_seq will be set correctly.

像以前一样准备你的陈述。按名称绑定变量(包括v_seq前面代码中的变量并执行该语句。过程完成后,:v_seq 的值将被正确设置。

回答by Quassnoi

You either issue:

你要么发出:

SELECT my_udf()
FROM dual

and parse the result as in SELECT query, or call anonymous block:

并将结果解析为SELECT query,或调用匿名块:

BEGIN
   :test := my_udf();
END;

, and bind :testas an output parameter.

, 并:test作为输出参数绑定。