Dynamic SQL: Inserting data value with ' (Single quotes)
425427Oct 15 2004 — edited Oct 18 2004
Hi,
We have written generic PL/SQL procedure that logs data
from multiple Tables into common LOG Table.
LOG Table has 3 columns, LOG_ID NUMBER(10),
LOG_TXT VARCHAR2(100),
LOG_TYPE VARCHAR(1)
Code for inserting data dynamically is as follows:
v_id NUMBER := 1;
v_txt VARCHAR2(100) := 'ABCDEF';
v_sql VARCHAR2(4000);
v_type VARCHAR(1) := 'E';
v_sql := 'INSERT INTO LOG VALUES(';
v_sql := v_sql || TO_CHAR(v_id);
v_sql := v_sql || ', ' || v_txt;
v_sql := v_sql || ', ' || v_type;
v_sql := v_sql || ')';
execute immediate v_sql;
Above code works fine for most of the cases.
It blows up when v_txt has any single quotes
such as ABC'DEF
In these cases it gives error ORA-00917: missing comma
How do we escape the Single Quote within the v_txt
so that Oracle would be able to insert data into the
table properly.
Any help/clues would be appreciated
Thanks
Auro