Escaping single quote in PLSQL

前端 未结 4 1308
旧时难觅i
旧时难觅i 2020-12-01 12:13

I want PLSQL to generate strings like:

COMMENT ON COLUMN TABLE.COLUMN IS \'comment from database\';

My solution is:

declare         


        
相关标签:
4条回答
  • 2020-12-01 12:32

    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,'''','''''')||'''; ' ;
    
    0 讨论(0)
  • 2020-12-01 12:39

    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
    
    0 讨论(0)
  • 2020-12-01 12:42

    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;
    
    0 讨论(0)
  • 2020-12-01 12:47

    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

    0 讨论(0)
提交回复
热议问题