【转】Oracle中如何插入特殊字符:& 和 ' (多种解决方案)

今天在导入一批数据到Oracle时,碰到了一个问题:Toad提示要给一个自定义变量AMP赋值,一开始我很纳闷,数据是一系列的Insert语句,怎么会有自定义变量呢?后来搜索了一下关键字AMP发现,原来是因为在插入数据中有一个字段的内容如下:

http://mobile.three.com.hk/3DX?uid=0676&sid=rt_060908

Oracle把这里的URL的参数连接符&当成是一个自定义变量了,所以要求我给变量AMP赋值。怎么办呢?方法有三:

[color=red]·方法一:在要插入的SQL语句前加上Set define off;与原SQL语句一起批量执行[/color]

我们在SQL*PLUS下执行 SQL> show all命令时,可以发现一个参数:define "&" (hex 26),如下图所示

......
concat "." (hex 2e)
copycommit 0
copytypecheck ON
define "&" (hex 26)
describe DEPTH 1 LINENUM OFF INDENT OFF
echo OFF
......

这个是Oracle里面用来识别自定义变量的设置,现在我们在SQL*PLUS下将其关闭:

SQL> Set define OFF;

然后再次执行导入脚本,OK!问题搞定。

注意:如果是在TOAD中执行,建议在每一个要导入的脚本第一行加上前面那句关闭define的话,否则当你导入第二个含有特殊字符的脚本的时候,又会出错。
如果是在SQL*PLUS中执行,则只需要设置一次define OFF,后面就可以连续导入了。直到你重新设置define ON为止。
[color=red]
·方法二:在SQL语句中将'&'替换成chr(38),因为chr(38)是‘&’的ASCII码[/color]

SQL> Select 'Tom' || chr(38) || 'Jerry' from dual;
[color=red]
·方法三:分拆原来的字符串[/color]

SQL> Select 'Tom' || '&' || 'Jerry' from dual;


我们可以看到,方法一最为简便,而且效率也最高。方法二因为有一个调用函数的过程,所以性能稍差。方法三需要两次连接字符串,效率最差!


那么如果字段的内容中包含了单引号要怎么插入呢?例如:It's fine。方法同样有三
[color=red]
·方法一:使用转义字符[/color]

SQL > Select 'test' || '''' from dual;

注意:这里的''''四个单引号是什么意思呢?首先第一个和最后一个都是Oracle中的字符串连接符,这个没有异议。那么第二个'和第三'又表示什么意思呢?第二个'是一个转义字符
第三个'才是我们真正的内容

[color=red]·方法二:同样是使用转义字符,只不过方式不同而已[/color]

SQL > Select 'test ''' from dual;

注意:这里的第二个,第三个'就是我们上面方法一中提到的转义符和真正的内容
[color=red]
·方法三:在SQL中将'替换成chr(39),因为chr(39)是'的ASCII码[/color]

SQL > Select 'It' || chr(39) || 'fine' from dual;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/app/smsprev4/src/NewCore/piliang_src_cluster_2023/plcb_2/src/util.cpp:683: undefined reference to `GetHistParam(inpackage*, std::string&, std::string&, std::string&, std::string&, std::string&, std::string&, int&, std::string&, oracle::occi::Date&, int&, std::string&, oracle::occi::Date&, oracle::occi::Date&, oracle::occi::Date&, std::string&, std::string&, std::string&, std::string&, std::string&, std::string)' /app/smsprev4/src/NewCore/piliang_src_cluster_2023/plcb_2/src/util.cpp:714: undefined reference to `updatePreCB(inpackage*, oracle::occi::Date, std::string, std::string, std::string&)' /app/smsprev4/src/NewCore/piliang_src_cluster_2023/plcb_2/src/util.cpp:808: undefined reference to `updatePreCB(inpackage*, oracle::occi::Date, std::string, std::string, std::string&)' bin/util.o: In function `process_xhx_file(std::string, std::string, std::string, int, CFileControl*)': /app/smsprev4/src/NewCore/piliang_src_cluster_2023/plcb_2/src/util.cpp:987: undefined reference to `GetHistParam(inpackage*, std::string&, std::string&, std::string&, std::string&, std::string&, std::string&, int&, std::string&, oracle::occi::Date&, int&, std::string&, oracle::occi::Date&, oracle::occi::Date&, oracle::occi::Date&, std::string&, std::string&, std::string&, std::string&, std::string&, std::string)' /app/smsprev4/src/NewCore/piliang_src_cluster_2023/plcb_2/src/util.cpp:1107: undefined reference to `updatePreCB(inpackage*, oracle::occi::Date, std::string, std::string, std::string&)' /app/smsprev4/src/NewCore/piliang_src_cluster_2023/plcb_2/src/util.cpp:1201: undefined reference to `updatePreCB(inpackage*, oracle::occi::Date, std::string, std::string, std::string&)' collect2: error: ld returned 1 exit status
07-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值