从plsql输出/返回CLOB值(指定的无效LOB定位符:ORA-22275)

时间:2015-06-16 16:46:10

标签: sql oracle plsql blob clob

我已经存储了plsql程序,它从文件中获取大文本

FROM ubuntu:latest

#install node and npm
RUN apt-get update && \
    apt-get -y install curl && \
    curl -sL https://deb.nodesource.com/setup | sudo bash - && \
    apt-get -y install python build-essential nodejs

#install imagemagick, graphicsmagick and set-up aws-cli to send SQS messages
RUN sudo apt-get -y install imagemagick
RUN sudo apt-get -y install graphicsmagick
RUN sudo apt-get install unzip
RUN curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
RUN unzip awscli-bundle.zip
RUN sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

#set-up environment variables for AWS


#at some point set-up git and fetch repository from git


# Provides cached layer for node_modules
ADD package.json /tmp/package.json
RUN cd /tmp && npm install
RUN mkdir -p /home/image-resizer && cp -a /tmp/node_modules /home/image-resizer/

#bundle source code into image
COPY . /home/image-resizer

然后我尝试以这种方式使用它:

create or replace 
procedure dbst_load_a_file( p_file_name in varchar2, l_clob  out  clob )
    as
        l_bfile   bfile;
        dst_offset  number := 1 ;
        src_offset  number := 1 ;
        lang_ctx    number := DBMS_LOB.DEFAULT_LANG_CTX;
        warning     number;
    begin
        l_bfile := bfilename( 'SCHEMES_OF_PS', p_file_name );
        dbms_lob.fileopen( l_bfile );
        dbms_lob.loadclobfromfile(
          DEST_LOB     => l_clob
        , SRC_BFILE    => l_bfile
        , AMOUNT       => dbms_lob.getlength( l_bfile )
        , DEST_OFFSET  => dst_offset
        , SRC_OFFSET   => src_offset
        , BFILE_CSID   => DBMS_LOB.DEFAULT_CSID
        , LANG_CONTEXT => lang_ctx
        , WARNING      => warning);
        dbms_lob.fileclose( l_bfile );
    end;

或更正确:

  insert into SCHEME_SOURCE (SOURCE, ID, CODE) 
  values (exec dbst_load_a_file( 'SCHEMES_OF_PS', 'Konotop.svg' ), 15, 'Konotop');

在第二种情况下,我收到错误

  

PL / SQL:数字或值错误:指定了无效的LOB定位符:   ORA-22275

在第一种情况下,我怀疑语法不存在。

如何从程序/函数中输出/返回CLOB参数,以便在存储的plsql中使用它

如果我有这段代码

declare
  myVal clob := empty_clob();

begin
  DBMS_OUTPUT.PUT_LINE(myVal);
  dbst_load_a_file('Konotop.svg', myVal);
  DBMS_OUTPUT.PUT_LINE(myVal);
end;

然后再次出错

  

SQL错误:ORA-06502:PL / SQL:数字或值错误:指定了无效的LOB定位符:ORA-22275   ORA-06512:在“SYS.DBMS_LOB”,第890行   ORA-06512:在“VAG.DBST_LOAD_A_FILE2”第12行

由于

2 个答案:

答案 0 :(得分:5)

此时:

    dbms_lob.loadclobfromfile(
      DEST_LOB     => l_clob

...您的l_clob OUT参数尚未初始化。使它成为一个空的CLOB也不起作用(所以即使你使l_clob成为IN OUT参数,它仍然会抱怨)as the documentation for empty_clob mentions

  

您不能将此函数返回的定位符用作DBMS_LOB包或OCI的参数。

您可以使用临时CLOB,方法是在尝试使用之前添加对dbms_lob.createtemporary(l_clob, true)的调用:

...
begin
    l_bfile := bfilename( 'SCHEMES_OF_PS', p_file_name );
    dbms_lob.fileopen( l_bfile );
    dbms_lob.createtemporary(l_clob, true);
    dbms_lob.loadclobfromfile(
      DEST_LOB     => l_clob
      ...

然后在调用它时不需要给出初始空值:

declare
  myVal clob;
begin
  dbst_load_a_file('Konotop.svg', myVal);
  DBMS_OUTPUT.PUT_LINE(myVal);
end;
/

这也是一个功能:

create or replace 
function dbst_load_a_file( p_file_name in varchar2) return clob
as
    l_clob    clob;
    l_bfile   bfile;
    dst_offset  number := 1 ;
    src_offset  number := 1 ;
    lang_ctx    number := DBMS_LOB.DEFAULT_LANG_CTX;
    warning     number;
begin
    l_bfile := bfilename( 'SCHEMES_OF_PS', p_file_name );
    dbms_lob.fileopen( l_bfile );
    dbms_lob.createtemporary(l_clob, true);
    dbms_lob.loadclobfromfile(
      DEST_LOB     => l_clob
    , SRC_BFILE    => l_bfile
    , AMOUNT       => dbms_lob.getlength( l_bfile )
    , DEST_OFFSET  => dst_offset
    , SRC_OFFSET   => src_offset
    , BFILE_CSID   => DBMS_LOB.DEFAULT_CSID
    , LANG_CONTEXT => lang_ctx
    , WARNING      => warning);
    dbms_lob.fileclose( l_bfile );
    return l_clob;
end;
/

select dbst_load_a_file('Konotop.svg') from dual;

或者在编辑过的问题中使用insert

答案 1 :(得分:1)

您不能在SQL语句中使用过程,而是使用类似

的函数
create or replace 
FUNCTION dbst_load_a_file( p_file_name in varchar2) RETURN  clob
IS
.....
END; 
insert into SCHEME_SOURCE (SOURCE, ID, CODE) 
  values (dbst_load_a_file('SCHEMES_OF_PS'), 15, 'Konotop');