gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:27:21 27 4
gpt4 key购买 nike

我有存储的 plsql 过程,它从文件中获取大文本

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');

或更正确:

declare
myVal clob := empty_clob();

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

在第二种情况下我得到一个错误

PL/SQL: numeric or value error: invalid LOB locator specified: ORA-22275

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

我如何从存储的 plsql 中使用它的过程/函数输出/返回 CLOB 参数

如果我有这个代码

create or replace
function dbst_load_a_file2( p_file_name in varchar2 ) return clob
is
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.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;

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

然后又报错

SQL Error: ORA-06502: PL/SQL: numeric or value error: invalid LOB locator specified: ORA-22275 ORA-06512: at "SYS.DBMS_LOB", line 890 ORA-06512: at "VAG.DBST_LOAD_A_FILE2", line 12

谢谢

最佳答案

此时:

    dbms_lob.loadclobfromfile(
DEST_LOB => l_clob

...您的l_clob OUT 参数尚未初始化。使它成为一个空的 CLOB 也不起作用(因此,即使您将 l_clob 设置为 IN OUT 参数,它仍然会报错)as the documentation for empty_clob mentions :

You cannot use the locator returned from this function as a parameter to the DBMS_LOB package or the 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

关于sql - 从 plsql 输出/返回的 CLOB 值(指定的 LOB 定位器无效 : ORA-22275),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30873543/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com