ORA-01704: string literal too long error update clob field - oracle

Can you advice on a method of doing updates on clob fields in Oracle?
The query is a simple one and running it I get
ORA-01704: string literal too long:
Update table_name
Set clob_field=value2
Where column=condition1 and clob_field=value1
The scope is to update the value in a clob column with a new value.
Thanks

Is Your code part of some PLSql procedure or it is simple SQL statement? Do You pass variable "value2" as bind variable or it is quoted string inside the query? Are You on 12c or some earlier release of Oracle DB?
Generally, the most common "not obvious" issue is related to the fact that varchar2 type is limited to 4000 characters in SQL statements. If You are in PLSql procedure, the limit is 32K characters.
Can You provide code sample? The fact is that following two statements result in different behavior:
update table_name
set clob_field=value2
where column=condition1
and clob_field=value1
update table_name
set clob_field='Some very long string.....end of very long string'
where column=condition1
and clob_field='Some even longer string.....end of even longer string'
Take a look at post Error : ORA-01704: string literal too long - example how to put update in plsql block in order to achieve 32.767 character limit.
edit:
Take a look at post Working with very large text data and CLOB column, too

As you may know, it is not possible to insert more than 4k characters at one time in a clob field with Oracle Database.
A workaround to solve this issue is to split the whole string in 2 string < 4k
Example :
create table t_test (id number, texte clob);
insert into t_test (id, texte) values(1, to_clob ('value of 3999 characters') || to_clob ('value of remaining 1001 characters'));
You can use the "Lorem ipsum" to do the test :-)

Put your string value to a CLOB variable first:
declare
c clob;
s varchar2(4000);
begin
s:=rpad('x',4000,'x');
for i in 1..100 loop
c:=c||s;
end loop;
dbms_output.put_line( dbms_lob.getlength(c) );
-- length of "c" is 400000 now
update table_name set clob_field=c where id=12345;
end;
/

Related

LISTAGG 4000 Character Limit - Result of string concatenation is too long [duplicate]

This question already has answers here:
Oracle - convert column values to comma separated values as CLOB without using XMLAGG
(1 answer)
ORA-64451: Conversion of special character to escaped character failed
(1 answer)
Closed 8 months ago.
select t.name, listagg(t.text)
from user_source t
group by t.name;
I am trying to execute the code above but since varchar2 is limited by 4000 chars it throws error. I tried to convert listagg to xml but I could not solve the
ORA-64451: Conversion of special character to escaped character failed.
error. I also tried the answers from other posts from various websites including stackoverflow.
I do not want to truncate the string, also I can't change MAX_STRING_SIZE parameter.
This example below throws ORA-64451 as well. I tried but could not solve the problem.
select rtrim(
xmlagg(
xmlelement(e, to_clob(t.TEXT), '; ').extract('//text()')
).GetClobVal(),
',')
from user_source t;
The best solution I know is posted somewhere in the Internet... You could probably just google for it. It basically consist of few steps:
Creating a collection type to store each text value to concatenate
create or replace type string_array_t as table of VARCHAR2(4000);
Creating a PL/SQL function which takes string_array_t as parameter and returns concatenated text as CLOB:
create or replace function
string_array2clob(
p_string_array string_array_t
,p_delimiter varchar2 default ','
) RETURN CLOB IS
v_string CLOB;
BEGIN
-- inside is a loop over p_string_array to concatenate all elements
--
-- below is just a draft because in real you should use a while loop
-- to handle sparse collection and you should put some checks to handle not initialized collection
-- and other important cases
-- furthermore it's better to create additional varchar2 variable as a buffer
-- and convert that buffer to clob when it's full for a better performance
for indx in p_string_array.first..p_string_array.last loop
v_string := v_string || to_clob(p_string_array(indx) || p_delimiter);
end loop;
RETURN substr(v_string, 1, nvl(length(v_string),0) - nvl(length(p_delimiter),0));
END string_array2clob;
/
Aggregate query as usual but using cast and collect instead of listagg and at the end convert it to clob with function from step above:
select t.name, string_array2clob(cast(collect(t.text order by t.line) as string_array_t ), p_delimiter => chr(10)) as text
from user_source t
group by t.name;
If your query is not just an example of concept and really you're trying to get a source of some object in database, then you should read about dbms_metadata.get_ddl function. It's made for it.

VARCHAR Leading 0's are lost when executing PLSQL procedures

I have a PL/SQL script which executes a number of procedures on an oracle DB.
The script defines:
DECLARE
productkey VARCHAR2(100);
BEGIN
productKey := '000000000070307037';
...
ProcedureName(productKey);
The procedure expects a VARCHAR2
PROCEDURE ProcedureName (
productKey VARCHAR2
)
The procedure inserts into a table:
BEGIN
Insert into Mytable
(
THIS_PRODUCT_KEY
)
Values
(productKey);
When I query that table, the product Key = 70307037, ie the leading 0's have been lost.
I saw some similar questions where TO_CHAR was suggested, I tried defining productKey in the script using TO_CHAR, and also modifiying the procedure to write using TO_CHAR:
BEGIN
Insert into Mytable
(
THIS_PRODUCT_KEY
)
Values
(TO_CHAR(productKey,'000000000000000000'));
Still coming through without the leading 0's.
There are multiple queries that join using the product key and don't work when the 0's are missing.
Why would I lose the 0's when the variable is a VARCHAR ?
I believe that "THIS_PRODUCT_KEY" column in your "Mytable" table is not a varchar2 column. I think it is number. If you change the datatype of the "THIS_PRODUCT_KEY" column to varchar2, it won't lose the 0s.

Is clob type using bad practice in PL/SQL code when you try to avoid to use magic numbers for string length?

Declaration of PL/SQL variable require setting length for varchar2 like types:
declare
query varchar2(2000);
begin
for i in 1..100 loop
query := query || to_char(i);
end loop;
dbms_output.put_line(query);
end;
/
I run in trap when product requirements changes so loop range become larger (set range to 1..1000 in my example) and I got error:
ORA-06502: PL/SQL: : буфер символьных строк слишком маленький ошибка числа или значения ORA-06512: на line 5
So I need to increase query length everywhere and even with some larger number to predicate future.
This is ugly. I am not expert of PL/SQL but by reading official docs I found clob type which doesn't require length and quick checks show me that code works!
PL/SQL code become less Pascal like and more Python/Ruby/Perl like. I really like it.
I apply type conversion only for dynamically built strings which are out of intensive loop usages.
Is such technique correct or I am wrong?
PS I am freshman to PL/SQL programming so ask such dumb question...
PPS One of working code (simplified):
queryStr := 'create or replace view "DATA_VIEW" ("DT", "MONEY", "EVENT") as ( ';
for eventRec in eventCur
loop
queryStr := queryStr || 'select DT, MONEY, ' || eventRec.ID
|| ' EVENT from ' || eventRec.tbl || ' union ';
end loop;
queryStr := rtrim(queryStr, ' union ') || ')';
execute immediate queryStr;
commit;
The CLOB datatype is your only real option if you want to manipulate strings larger than 4000 bytes in SQL or 32 kB in PL/SQL.
The operations on CLOB are a bit more expensive than on VARCHAR2 for small strings though, so you should only use the CLOB datatype when dealing with large strings. Thus the VARCHAR2 datatype still has its uses, don't go overboard and replace all your strings with CLOBs.
Looking at your example working code, what I understand is you want to dynamically generate a view definition which is made up of probably hundreds of union statements, each querying a different table.
Similar data stored in hundreds of different tables! Now thats a strange database architecture!!
anyways, just blindly replacing varcar2 with clob is not recommeneded from performance point of view.
Why dont you keep a counter/check for the number of records in your 'eventCur'(eventRec)?
Then you can optimally choose a value for that counter above which you could use clob datatype and below which a varchar2 datatype..

concatenate multiple clobs withing Oracle Procedure

I have an Oracle procedure that is going to accept multiple values within a parameter. Part of the procedure will run a select statement putting the results of the parameter in the where clause and placing the concatenated CLOBs into a variable. I am currently using the query below in the procedure but when I run it I get the error below.
If CLOB_ID is not null then
SELECT cast((collect(CLOB_TEXT) )as CLOB )
into v_MessageBody
FROM MESSAGE_CLOB_TABLE
WHERE MESSAGE_ID in CLOB_ID;
End If;
Error: ORA-00932: incosistant datatypes: expected - got CLOB
I also tried writing this using a LISTAGG function but LISTAGG doesnt work with the CLOB values in the MESSAGE_CLOB_TABLE
Any help would be greatly appreciated! I am using Oracle 11g.
If you need to concatenate in PL/SQL simplest variant is to loop through all selected records and append all found records to result:
create or replace function get_message(p_msg_id in number) return CLOB
is
v_MessageBody CLOB;
begin
-- create new instance of temporary CLOB
dbms_lob.createtemporary(v_MessageBody, true);
-- Fill instance with lines
for cMessages in (
select clob_text
from message_clob_table
where message_id = p_msg_id
order by message_row
)
loop
-- add line
dbms_lob.append(v_MessageBody, cMessages.clob_text);
end loop;
-- return collected lines as single CLOB
return v_MessageBody;
end;
Example above works if type of CLOB_TEXT field is CLOB and you need to collect only one message. You can test function in this SQLFiddle.
If you need to select many messages together based on list of his ID's, function becomes a little bit more complex, but principle remains the same.

How can fill a variable of my own created data type within Oracle PL/SQL?

In Oracle I've created a data type:
TABLE of VARCHAR2(200)
I want to have a variable of this type within a Stored Procedure (defined locally, not as an actual table in the DB) and fill it with data.
Some online samples show how I'd use my type if it was filled and passed as a parameter to the stored procedure:
SELECT column_value currVal FROM table(pMyPassedParameter)
However what I want is to fill it during the PL/SQL code itself, with INSERT statements.
Anyone knows the syntax of this?
EDIT: I should have clarified: my source data is entered as a VARCHAR2 parameter passed to the stored procedure: a separator (like comma) delimited string. I'm already iterating through the delimited string to get every separate value - I would like to INSERT each one into my type so I can treat it as a TABLE for the rest of the logic.
"I want is to fill it during the PL/SQL
code itself, with INSERT statements"
It's like populating any other PL/SQL variable: we have to use INTO. Only because we're populating multiple rows we need to use the BULK COLLECT syntax.
declare
l_array your_nested_table_type;
begin
select col1
bulk collect into l_array
from t72;
end;
/
If the result set returns a lot of records it is a good idea to use the LIMIT clause inside a loop. This is because PL/SQL collections - just like every other PL/SQL variable - are held in session memory. So we don't want the array to get too big, otherwise it might blow the PGA. Find out more.
edit
"I edited the question to clarify
specifically what I want"
sigh Tokenizing a string is an altogether different issue. I have previously posted solutions in two SO threads. If you're using 9i or earlier use this approach. Otherwise use this regex solution (actually this splits the string into numeric tokens, but it is easy enough to convert to characters).
edit 2
"I only want to be able to use the
type "internally" (within the Stored
Procedure) by adding values to it. Is
this something I can do with the first
link?"
Sure. Why not?
SQL> declare
2 local_array tok_tbl;
3 begin
4 local_array := parser.my_parse('Keith Pellig,Peter Wakeman,Ted Bentley,Eleanor Stevens');
5 local_array.extend();
6 local_array(5) := 'Reese Verrick';
7 for i in local_array.first()..local_array.last()
8 loop
9 dbms_output.put_line(local_array(i));
10 end loop;
11 end;
12 /
Keith Pellig
Peter Wakeman
Ted Bentley
Eleanor Stevens
Reese Verrick
PL/SQL procedure successfully completed.
SQL>
Here I have re-used my SQL type, but it would work just as well if TOK_TBL were declared in the PL/SQL package instead.
you don't mention if the type you created is a SQL type or a PL/SQL type. They are used similarly in Pl/SQL so I will assume you created a SQL type with a command like this:
SQL> CREATE TYPE tab_varchar IS TABLE of VARCHAR2(200);
2 /
Type created
This is a nested-table. Find out how to manipulate collections in PL/SQL it in the documentation, for example:
SQL> DECLARE
2 lt tab_varchar;
3 BEGIN
4 /* initialization */
5 lt := tab_varchar('a', 'b', 'c');
6 /* adding elements */
7 lt.extend(1);
8 lt(4) := 'd';
9 FOR i IN lt.FIRST .. lt.LAST LOOP
10 dbms_output.put_line('lt(' || i || ')=' || lt(i));
11 END LOOP;
12 END;
13 /
lt(1)=a
lt(2)=b
lt(3)=c
lt(4)=d

Resources