ebayboy 发表于 2011-12-22 08:54

SQL语句中&、单引号等特殊符号的处理

<span style="color: rgb(0, 153, 2);">今天遇到一个insert语句,在<a target="_self" href="http://hi.baidu.com/zzdegui/blog/item/%20:;"><u><strong>SQL</strong></u></a> Tools(链接<a target="_self" href="http://hi.baidu.com/zzdegui/blog/item/%20:;"><u><strong>Oracle</strong></u></a><a target="_self" href="http://hi.baidu.com/zzdegui/blog/item/%20:;"><u><strong>数据库</strong></u></a>)插入的某列值为“Computer Hardware &amp; Software&gt;&gt;CPU",这样执行会有问题,因为"&amp;"是一个<a target="_self" href="http://hi.baidu.com/zzdegui/blog/item/%20:;"><u><strong>特殊符号</strong></u></a>。
符号(&amp;) 在Oracle里有特殊含义,是一个宏变量标识符;在Oracle中,&amp;
是从需要外部输入输入的变量。PS:MySqL中可以直接用"P&amp;G"这样的字符串,故不存在本文所说的问题。要想插入带有&amp;符号的列,
需要用如下的方法(当然,用PL/SQL Developer的for update语句也是可的,但我一般不用此工具)。</span><br style="color: rgb(0, 153, 2);">
<br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">如下SQL语句就不能正确运行:</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">select 'Alibaba&amp;Taobao' from dual;</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">处理方法:</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">用Oracle的字符串处理函数chr处理。chr(38)表示 &amp;符号</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">如:select chr(38) from dual;</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">结果:&amp;</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">select 'Alibaba'||chr(38)||'Taobao' from dual;</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">结果:Alibaba&amp;Taobao</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);"><a target="_self" href="http://hi.baidu.com/zzdegui/blog/item/%20:;"><u><strong>其他</strong></u></a>不能处理的特殊符合,也用类似的方式处理。如果不知道该特殊符号的ascii值,可以调用ascii函数处理,</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">如:select ascii('&amp;') from dual;</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">结果:38</span><br style="color: rgb(0, 153, 2);">
<br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">再附一个单引号(')的例子(I'm OK):</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">1. &nbsp;&nbsp;&nbsp; SELECT 'I' || '''m OK' from dual;</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">2. &nbsp;&nbsp;&nbsp; SELECT 'I''m OK' from dual;</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">3. &nbsp;&nbsp;&nbsp; SELECT 'I'||Chr(39)||'m OK' FROM dual;</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">1、2两个都是<a target="_self" href="http://hi.baidu.com/zzdegui/blog/item/%20:;"><u><strong>转义字符</strong></u></a>(只是用不用连接符||的区别),第3中是一个字符替换。</span><br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">PS:我用字符替换来弄and符号(&amp;)就没有成功了。</span><br style="color: rgb(0, 153, 2);">
<br style="color: rgb(0, 153, 2);">
<span style="color: rgb(0, 153, 2);">当然,也看到网上有人提出了“Set define OFF;”和“Select 'Tom' || '&amp;' || 'Jerry' from dual;”的方法来解决,不过我试了一下,并没有成功。</span>
页: [1]
查看完整版本: SQL语句中&amp;、单引号等特殊符号的处理