Oracle PLSQL中的LOB操作

时间:2018-02-20 13:25:10

标签: oracle plsql

有人可以指出这段代码有什么问题。

declare 
locations CLOB;
offset NUMBER :=1;
begin
  if locations is null then 
    dbms_output.put_line('location is null');
  else
    dbms_output.put_line('location is not null');
  end if;
  --assign an empty CLOB
  locations := empty_clob();
  --locations := dbms_lob.createTemporary();

  if locations is null then 
    dbms_output.put_line('location is null');
  else
    dbms_output.put_line('location is not null and length of the lob is: '||dbms_lob.getlength(locations));
  end if;
  --write something
  dbms_lob.open(locations,dbms_lob.lob_readwrite);
  dbms_lob.write(locations,length('i am not alone'),1,'i am not alone');
  --add more lines
  dbms_lob.writeappend(locations,length('i am not alone'),'i am not alone');
  dbms_lob.close(locations);

end;
/

我收到错误22275. 00000 - "指定了无效的LOB定位符" 不知道为什么我得到这个,因为我已经初始化了CLOB"位置"使用EMPTY_CLOB()函数。

2 个答案:

答案 0 :(得分:3)

文档for empty_clob()说明了限制:

  

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

如果您按照该部分you can also see中的链接:

  

在其自身中运行EMPTY_BLOB()EMPTY_CLOB()函数不会引发异常。但是,使用设置为空的LOB定位器来访问或操作任何PL / SQL DBMS_LOB或OCI函数中的LOB值会引发异常。

     

可以使用空LOB定位符的有效位置包括VALUES语句的INSERT子句和SET语句的UPDATE子句。

您似乎尝试使用createtemporary,因为已注释掉了;但是你把它称为功能而不是程序。您可以改用此表单:

dbms_lob.createtemporary(locations, false);

所以:

set serveroutput on

declare 
  locations CLOB;
  offset NUMBER :=1;
begin
  if locations is null then 
    dbms_output.put_line('location is null');
  else
    dbms_output.put_line('location is not null');
  end if;
  --assign an empty CLOB
--  locations := empty_clob();
--  locations := dbms_lob.createTemporary();
  dbms_lob.createtemporary(locations, false);

  if locations is null then 
    dbms_output.put_line('location is null');
  else
    dbms_output.put_line('location is not null and length of the lob is: '||dbms_lob.getlength(locations));
  end if;
  --write something
  dbms_lob.open(locations,dbms_lob.lob_readwrite);
  dbms_lob.write(locations,length('i am not alone'),1,'i am not alone');
  --add more lines
  dbms_lob.writeappend(locations,length('i am not alone'),'i am not alone');
  dbms_lob.close(locations);

  -- added this for fun
  if locations is null then 
    dbms_output.put_line('location is null');
  else
    dbms_output.put_line('location is not null and length of the lob is: '||dbms_lob.getlength(locations));
  end if;

end;
/

得到输出:

location is null
location is not null and length of the lob is: 0
location is not null and length of the lob is: 28


PL/SQL procedure successfully completed.

答案 1 :(得分:0)

此代码意味着获取名为empty lob的系统常量并尝试在其中编写其他内容的silimar。它失败。为什么? :)

locations := empty_lob()替换为dbms_lob.createTemporary(locations, true);,您的代码将有效。