2

我知道在 sql varchar2 中只能是 4000 左右。

我知道在 oracle PL varchcar2 中可以在 32000 左右。

我定义了一个长度超过 4000 个字符的 varchar2 变量,我想在查询中使用它。我不想将值插入表中。该值是我使用此查询解析并插入到表中的分隔字符串。当变量长度少于 4000 个字符时,此查询有效。有没有办法让它使用多达 32000 个字符?

create global temporary table t(single_element varchar(500),element_no number);
declare
--declared as 32767 but this string contains less than 4000 characters. 
--This will work. If you expand the string to 32000 characters it will not work.
myvar varchar2(32767) := 'tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4';
begin
delete from t;
insert into t
SELECT SUBSTR(str, start_pos, (next_pos-start_pos)) AS single_element, element_no
FROM  (
        SELECT 
              ilv.str, 
              nt.column_value AS element_no, 
              INSTR(ilv.str, '^~', DECODE(nt.column_value, 1, 0, 1), DECODE(nt.column_value, 1, 1, nt.column_value-1)) + 2 AS start_pos,
              INSTR(ilv.str, '^~', 1, DECODE(nt.column_value, 1, 1, nt.column_value)) AS next_pos
        FROM   (
                select '~' || myvar || '^~' as str, 
                (Length(myvar) - length(replace(myvar,'^~','')))/2 + 2 as no_of_elements 
                from dual) ilv,

              TABLE(
                    CAST(
                       MULTISET(
                          SELECT ROWNUM FROM dual CONNECT BY ROWNUM < ilv.no_of_elements
                          ) AS number_ntt )) nt
         );
end;

将“myvar”扩展为 32000 个字符时出现的错误是

can bind a LONG value only for insert into a LONG column

有没有办法可以绕过这个大小限制,因为我实际上并没有将此值插入到表中,我只是在查询中使用它?

4

2 回答 2

4

您是否必须将变量定义为 VARCHAR2?您可以将其定义为 CLOB 吗?

如果我将声明MYVAR从 a更改VARCHAR2(32767)为 aCLOB并定义NUMBER_NTT类型,您的代码将为我运行

SQL> ed
Wrote file afiedt.buf

SP2-0161: line 2 truncated.
  1  declare
  2  myvar clob := 'tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testms
  <<snip>>
~tcd3~#testmsg3^~tcd4~#testmsg4';
  4  begin
  5  delete from t;
  6  insert into t
  7  SELECT SUBSTR(str, start_pos, (next_pos-start_pos)) AS single_element, elem
ent_no
  8  FROM  (
  9          SELECT
 10                ilv.str,
 11                nt.column_value AS element_no,
 12                INSTR(ilv.str, '^~', DECODE(nt.column_value, 1, 0, 1), DECODE
(nt.column_value, 1, 1, nt.column_value-1)) + 2 AS start_pos,
 13                INSTR(ilv.str, '^~', 1, DECODE(nt.column_value, 1, 1, nt.colu
mn_value)) AS next_pos
 14          FROM   (
 15                  select '~' || myvar || '^~' as str,
 16                  (Length(myvar) - length(replace(myvar,'^~','')))/2 + 2 as n
o_of_elements
 17                  from dual) ilv,
 18                TABLE(
 19                      CAST(
 20                         MULTISET(
 21                            SELECT ROWNUM FROM dual CONNECT BY ROWNUM < ilv.n
o_of_elements
 22                            ) AS number_ntt )) nt
 23           );
 24* end;
 25  /

PL/SQL procedure successfully completed.

SQL> select count(*) from t;

  COUNT(*)
----------
       172

话虽如此,这不是我解析分隔字符串的方式,尤其是在 PL/SQL 中。但它完成了这项工作。

于 2011-09-06T19:37:03.013 回答
1

好的,这很接近你的实现偏差的边缘,虽然记住 forall 是一个批量绑定操作,而不是一个真正的循环,但是你看过 dbms_utility.comma_to_table 函数吗?

它是一个优化的内部 oracle 解析函数,尽管有一些限制,您可以在此处阅读:http ://www.techiegyan.com/2009/02/17/oracle-break-comma-separated-string-using-dbms_utilitycomma_to_table/

如果您解析的字段以数字、特殊字符、包含逗号等开头,则需要 replace() 使其以逗号分隔,并且还需要双引号括起来

但是如果你的数据允许 - 它肯定会让你的代码看起来更干净(并且可能工作得更快)

declare
   myvar      varchar2(32000) := 'tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3';
   mycnt      binary_integer;
   myresults  sys.dbms_utility.lname_array;
begin
   sys.dbms_utility.comma_to_table('"'||replace(myvar,'^~','","')||'"', mycnt, myresults );
   delete from t;
   forall ix in myresults.first..myresults.last 
      insert into tvalues (myresults(ix));
   commit;
end;
于 2011-09-07T16:10:00.723 回答