简体   繁体   English

从SOCI用匿名PL SQL块调用PLsql脚本

[英]Calling PLsql script with an anonymous PL SQL block from SOCI

I'm searching for a way to call an anonymous PLsql block through SOCI. 我正在寻找一种通过SOCI调用匿名PLsql块的方法。 The data transfer takes place through a refcursor that was previously created as a variable in the script: 数据传输通过以前在脚本中作为变量创建的refcursor进行:

variable rc refcursor
declare
   v_obj_id number(4,0) := 1;
   v_obj_def varchar(30);
   v_obj_type number := 1;
begin
  open :rc for
     select v_obj_id, OBJ_DEF_ID
     from MY_OBJECT_DEFS
     where OBJECT_TYPE = v_obj_type;
end;

I need to read the refcursor from my application to retrieve the data. 我需要从应用程序中读取refcursor才能检索数据。 I tried to execute the above through a soci::statement but it gives me the error: ORA-24333: zero iteration count . 我试图通过soci::statement执行上述操作,但它给了我错误: ORA-24333: zero iteration count The PLsql script works fine when executed in SqlPlus. 在SqlPlus中执行时,PLsql脚本运行良好。

  1. How can I make the connection between the statement and the refcursor rc? 如何在语句和refcursor rc之间建立连接? Should I use some other SOCI construct (other than statement) for this purpose? 我是否应该为此目的使用其他SOCI构造(除了语句)?
  2. I understand there are two instructions in the above script; 我了解以上脚本中有两条指令; (i. the refcursor creation, ii. the anonymous PLsql block itself). (i。refcursor创建,ii。匿名PLsql块本身)。 I'm not sure whether its possible to call multiple instructions in a single SOCI statement. 我不确定是否可以在单个SOCI语句中调用多个指令。 Can this be confirmed? 可以确认吗?

Following is the what I tried. 以下是我尝试过的。 The sSQL contains the above PLsql script: sSQL包含上面的PLsql脚本:

dbConn.open("...");
int iObjId;
std::string iObjDefId;
soci::indicator ind_iObjId = soci::i_ok,
        ind_iObjDefId = soci::i_ok;

soci::statement stmt(dbConn);
stmt.alloc();
stmt.prepare(sSQL);
stmt.exchange(soci::into(iObjId, ind_iObjId));
stmt.exchange(soci::into(iObjDefId, ind_iObjDefId));
stmt.define_and_bind();
stmt.execute(false);

while (stmt.fetch())
{
    if (soci::i_ok == ind_iObjId)
        std::cout << "Obj ID: " << iObjId << std::endl;

    if (soci::i_ok == ind_iObjDefId)
        std::cout << "Obj Def ID: " << iObjDefId << std::endl;
}

EDIT: I'm using Oracle 11g 编辑:我正在使用Oracle 11g

The statement variable rc refcursor is neither SQL nor PL/SQL but part of Oracle's SQL*Plus command-line utility and compatible third party products. 语句variable rc refcursor既不是SQL也不是PL / SQL,而是Oracle SQL * Plus命令行实用程序和兼容的第三方产品的一部分。 I don't know C++ but presumably you would need to define a ref cursor object in the host program. 我不了解C ++,但大概您需要在主机程序中定义ref游标对象。

If this is not feasible, and you are on Oracle 12.1 or later, it's possible that you could use an implicit result set construction, along the lines of 如果这不可行,并且您使用的是Oracle 12.1或更高版本,则可能按照以下方式使用隐式结果集构造:

declare
    rc sys_refcursor;
begin
    open rc for select * from dual;
    dbms_sql.return_result(rc);
end;

as discussed in Is it possible to output a SELECT state from a PL/SQL block? 是否有可能从PL / SQL块输出SELECT状态?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM