I want PLSQL to generate strings like:
COMMENT ON COLUMN TABLE.COLUMN IS \'comment from database\';
My solution is:
declare
I do this sort stuff a fair bit (usually generating insert/update statements).
You just need to use the replace function to turn all the '
into ''
. i.e. Change it to:
str_comment:='COMMENT ON COLUMN '||rec.table_name||'.'||rec.column_name
||' IS '''||REPLACE( rec.description,'''','''''')||'''; ' ;
You need to use '' in the code But Before trying it to the actual code ,
try the line which has quotes in the dual
For example:
select '''sumesh''' from dual
Use the REPLACE
function in your select.
declare
str_comment varchar2(4000);
begin
for rec in (SELECT table_name, column_name, REPLACE(description, '''', '''''')
FROM description_table)
loop
str_comment:='COMMENT ON COLUMN ' || rec.table_name || '.'
||rec.column_name|| ' IS ''' ||rec.description|| '''; ' ;
dbms_output.put_line(str_comment);
end loop;
end;
You can use the Quote operator like
str_comment:='COMMENT ON COLUMN '||rec.table_name||'.'||rec.column_name||' IS q''[' ||rec.description|| ']'';' ;
see http://psoug.org/reference/string_func.html