I have a NoteDetail table with a varchar(4000) field for NoteText and number(4,0) for NoteNumber. The idea is that if a note longer than 4000 characters comes in it's broken into multiple notes with incremented NoteNumber entries.
The logic for doing the inserts is below and it worked beautifully on Oracle 10. Recently the app was moved to Oracle 12c and I'm getting the error: "ORA-01461: can bind a LONG value only for insert into a LONG column" The Oracle DBA can't figure out why this is happening.
Below is the essence of what my insert code is; my best guess is that if the incoming string is long enough then the SUBSTR() function returns a long even when I'm assigning to a variable of the type varchar2(4000). For reference: Oracle SQL Developer indicates the error is happening at the point of the insert (in the loop of the else block below).
Does anyone know how to fix this?
DECLARE
s_incoming_string varchar2(32000);
s_substring_value varchar2(4000);
i_note_iteration number(4,0);
i_note_iterations number(4,0);
i_substr_start number(6,0);
k_NoteId number(19,0);
BEGIN
SELECT noteid_seq.nextval INTO k_NoteId FROM dual;
s_incoming_string := {a 7000 character long note};
i_note_iterations := ceil(length(s_incoming_string)/4000);
IF i_note_iteration = i_note_iterations THEN
--I NEVER GET AN ERROR ON THIS BRANCH!!!
INSERT INTO NoteDetail (NoteId, NoteNumber, NoteText)
VALUES (k_NoteId, 1, s_incoming_string);
ELSE
FOR i_note_iteration IN 1..i_note_iterations
LOOP
i_substr_start := (4000 * (i_note_iteration - 1)) + 1;
IF i_note_iteration = i_note_iterations THEN
--this is the last chunk of text; no need
--to read past the end of the buffer
s_substring_value = SUBSTR(s_incoming_string, i_substr_start);
ELSE
--I ONLY GET AN ERROR if this branch is executed before the insert:
s_substring_value = SUBSTR(s_incoming_string, 4000);
END IF;
INSERT INTO NoteDetail (NoteId, NoteNumber, NoteText)
VALUES (k_NoteId, i_note_iteration, s_incoming_string);
END LOOP;
END IF;
END;
Table schema is:
DCLARE TABLE NoteDetail (
NoteId number(19,0),
NoteNumber number(4,0),
NoteText varchar2(4000)
);
insert
statements. – krokodilko