CLOB不包含任何数据

时间:2012-09-27 10:24:49

标签: c++ oracle stored-procedures oracle11g

从商店程序我要输出CLOB,因为varchar2不够长(即使我在这个例子中只使用255个字符)。

我找到了一些oracle示例,并尝试将它们与我的代码一起使用。但遗憾的是,CLOB内部似乎没有任何内容。如何正确写入CLOB

这是C ++代码的样子:

extern "C" DLLEXPORT 
void version(OCIExtProcContext* context, OCILobLocator **out, sb4 *ind_out){
    OCIEnv* envh;
    OCISvcCtx* svch;
    OCIError* errh;
    OCIExtProcGetEnv (context, &envh, &svch, &errh);

    std::string versioninfo;
    versioninfo = "103";

    oraub8 written = 10;

    OCIDescriptorAlloc(envh, 
                       (dvoid **) out,
                       (ub4) OCI_DTYPE_LOB,       /* Type of Descriptor */
                       (size_t) 0, 
                       (dvoid **) 0);
    OCILobTrim2(svch, 
               errh, 
               *out,
               (ub4)1);


    OCILobWrite2(/*svcctx*/svch, /*errh*/errh, /*ociloblocator*/*out,
                 /*byte_amtp*/NULL, /*char_amtp*/NULL, /*offset*/1,
                 /*bufp*/(void*)versioninfo.c_str(), /*buflen*/versioninfo.size(),
                 /*piece*/OCI_ONE_PIECE, /*context for callback*/NULL,
                 /*callback*/NULL, /*csid*/0, /*csfrm*/SQLCS_IMPLICIT);
    *ind_out = 0;
    return;
}

SP注册如下:

create or replace
procedure version(versioninfo OUT clob) as
    external name "version" library myLib language c with context 
    parameters (context, versioninfo, versioninfo INDICATOR SB4);

我这样打电话给SP:

declare
  res clob;
  begin
  --dbms_lob.createtemporary(res,true);
  version(res);
  dbms_output.put_line(dbms_lob.substr(res, 255, 0));
end;

正如你所看到的,我在调用SP之前也试过分配CLOB,但这并没有多大帮助。我错过了什么?

1 个答案:

答案 0 :(得分:0)

在使用该CLOB之前使用OCILobCreateTemporary解决了这个问题。

    OCIDescriptorAlloc(envh, 
                       (dvoid **) out,
                       (ub4) OCI_DTYPE_LOB,       /* Type of Descriptor */
                       (size_t) 0, 
                       (dvoid **) 0);
    OCILobCreateTemporary(svch, errh, *out, 0, SQLCS_IMPLICIT, OCI_TEMP_CLOB, OCI_ATTR_NOCACHE, OCI_DURATION_CALL);
    ub4 amt = static_cast<ub4>(result.size());
    OCILobTrim2(svch, 
                errh, 
                *out,
                (ub4)amt);

    oraub8 amtp = static_cast<oraub8>(result.size());
    OCILobWrite2(/*svcctx*/svch, /*errh*/errh, /*ociloblocator*/*out,
                 /*byte_amtp*/NULL, /*char_amtp*/&amtp, /*offset*/1,
                 /*bufp*/reinterpret_cast<dvoid*>(const_cast<char*>(result.c_str())), /*buflen*/amt,
                 /*piece*/OCI_ONE_PIECE, /*context for callback*/NULL,
                 /*callback*/NULL, /*csid*/0, /*csfrm*/SQLCS_IMPLICIT);
    *ind = 0;