Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle 在PLSQL中转义单引号_Oracle_Plsql - Fatal编程技术网

Oracle 在PLSQL中转义单引号

Oracle 在PLSQL中转义单引号,oracle,plsql,Oracle,Plsql,我希望PLSQL生成如下字符串: COMMENT ON COLUMN TABLE.COLUMN IS 'comment from database'; 我的解决办法是: declare str_comment varchar2(4000); begin for rec in (select table_name, column_name, description from description_table) loop str_comment:='COMMENT ON CO

我希望PLSQL生成如下字符串:

COMMENT ON COLUMN TABLE.COLUMN IS 'comment from database';
我的解决办法是:

declare
  str_comment varchar2(4000);
begin
  for rec in (select table_name, column_name, 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;
rec.description
中不包含单个QOUTE时,输出正常。否则就需要逃逸信。我应该如何实施它

OK输出行(它有转义符以保留单个qoute):

非NOK输出行,因为未添加单引号的转义字母,且未编译:

COMMENT ON COLUMN TABLE1.COLUMN1_LV  IS 'It's secret';
我的解决方案不是检查描述是否包含单引号。我只是将源(描述)列的单引号替换为两个单引号,然后在字符串上生成
注释,然后我
回滚


有更好的解决方案吗?

我做了很多类似的事情(通常生成insert/update语句)

您只需使用replace函数将所有的
'
转换为
'
。i、 e.改为:

str_comment:='COMMENT ON COLUMN '||rec.table_name||'.'||rec.column_name
            ||' IS '''||REPLACE( rec.description,'''','''''')||'''; ' ;

在选择中使用
REPLACE
功能

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;

您可以使用Quote操作符,如

str_comment:='COMMENT ON COLUMN '||rec.table_name||'.'||rec.column_name||' IS q''[' ||rec.description|| ']'';' ;
请参见

您需要在代码中使用“” 但在尝试使用实际代码之前

请尝试在双引号中包含引号的行

例如:

select '''sumesh''' from dual

用双引号代替单引号是可以的。
回滚的意义是什么?你能发布完整的代码(替换和回滚)吗?@Quassnoi他可能会更新描述表,然后回滚更新。我看不出这个答案与接受的答案有什么不同。如果描述本身包含
]'
,这将不起作用。但你可以使用其他字符代替[和]。事实上,除了空格、RETURN或tab之外的任何内容。@mik我知道这是一个旧的注释,但对于重定向到此答案的任何人来说,引号字符串包含引号字符是完全正确的。例如,至少在11.2.0.4中,以下操作非常有效:
从dual中选择q'{{test}{from}}}}'返回:
{test}{from}
@Boneist,但是,正如我所写的,它不能包含
}
(引号字符后跟一个撇号)@mik-ah,说得好。很明显,我需要一个新的验眼器,因为我不知何故没有看到
。。。哦!这将给出
PLS-00302:必须声明组件“说明”
,因为
rec
中没有
DESCRIPTION
字段。您应该在
REPLACE()
之后添加列别名,或者在
stru comment
语句的赋值中添加
REPLACE
select '''sumesh''' from dual