tags:

views:

52

answers:

1

i want to create an insert script which will be used only to insert one record into one table.

It has 5 columns and one of them is of type CLOB.

when ever i am trying it says can not insert string is so long . larger than 4000.

i need an insert statement with clob as one field.

insert into tblTableName (id, name, description, accountNumber, fathername)
values(1,N'Name', CLOB'some very long string here, greater than 4000 characters', 23,  'John')

Please advice.

Thanks in advance Ashish

+2  A: 

Keep in mind that SQL strings can not be larger than 4000 bytes, while Pl/SQL can have strings as large as 32767 bytes. see below for an example of inserting a large string via an anonymous block which I believe will do everything you need it to do.

note I changed the varchar2(32000) to CLOB

set serveroutput on    
CREATE TABLE TESTCLOB(ID  NUMBER, C  CLOB, d varchar2(4000));

DECLARE
 REALLYBIGTEXTSTRING CLOB := '123';
 i int;
BEGIN

    WHILE LENGTH(REALLYBIGTEXTSTRING) <= 60000 LOOP
        REALLYBIGTEXTSTRING := REALLYBIGTEXTSTRING || '000000000000000000000000000000000';
    END LOOP;

   INSERT INTO TESTCLOB(ID, C,D) VALUES(0, REALLYBIGTEXTSTRING, 'done');
    DBms_output.put_line('I have finished inputting your clob: '|| length(REALLYBIGTEXTSTRING)); 
end ;
/

select * from TESTCLOB ;

--> "I have finished inputting your clob: 60030"

tanging
That's a good answer for CLOBs up to 32'000 characters. For bigger CLOBs, you need to create a temporary CLOB (see DBMS_LOB package), append data to it in chunks of 32'000 and the insert that temporary CLOB into the table.
Codo
@codo, that is a really good point, but in this example dbms_lob was unnecessary - at least in 11r1 (and I should not have set the ReallyBigTextString as a VARCHAR2(32000) -- I will amend the above example.
tanging
@tanging: You're partially right. The use of DBMS_LOB is probabably not needed. But if you're not using an artificial string - as you do by repeating the same substring again and again - then you're limited by the size of the declare/begin/end block. At a certain size (about 100K), you get an error message. So you have to split your long string into chunks and split it over several declare/begin/end blocks. At that's rather tricky.
Codo
@codo, got it, thanks.
tanging
Also keep in mind that SQL*Plus only supports 2499 characters on a line.
jonearles