Oracle insert/update CLOB field reports ORA-01704: string literal is too long [solution]

I recently encountered such a problem. When inserting or updating a CLOB field in an Oracle database, I directly spliced ​​an INSERT INTO and UPDATE statement, similar to the following:

INSERT INTO table name (field 1, field 2, CLOB field) VALUES ('XX', 'XXX', 'a lot of content');
UPDATE table name T SET T.CLOB field name = 'There is a lot of content';

 When executing SQL, an error [ORA-01704: string file too long] was reported, as shown in the following figure:

There is no problem with the above SQL syntax, but if the content of the CLOB field is very large, the SQL statement will be too long. I agree with this statement on the Internet: implicit conversion, oracle converts strings to varchar2 type by default, and this string The length is larger than 4000, so an ora-01704 error will be reported. In layman's terms, the number of characters between two single quotation marks cannot exceed 4000.

When you encounter this problem, you can use PL/SQL syntax to solve it by binding variables instead of directly splicing SQL, for example:

DECLARE
  clobValue tablename.fieldname%TYPE;
BEGIN
  clobValue := 'XXX'; --field content
  UPDATE table name T SET T. field name = clobValue WHERE condition;
  COMMIT;
END;
/
Note: Adding "/" at the end of END; means executing this PL/SQL code. If multiple PL/SQL codes need to be executed at the same time, this symbol must be added at the end.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326704506&siteId=291194637