﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-guangcaiwudong-文章分类-SQL</title><link>http://www.blogjava.net/guangcaiwudong/category/53311.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 08 Feb 2013 05:41:07 GMT</lastBuildDate><pubDate>Fri, 08 Feb 2013 05:41:07 GMT</pubDate><ttl>60</ttl><item><title>DB2、ORACLE SQL写法的主要区别</title><link>http://www.blogjava.net/guangcaiwudong/articles/395233.html</link><dc:creator>Kevin_YK</dc:creator><author>Kevin_YK</author><pubDate>Thu, 07 Feb 2013 08:23:00 GMT</pubDate><guid>http://www.blogjava.net/guangcaiwudong/articles/395233.html</guid><wfw:comment>http://www.blogjava.net/guangcaiwudong/comments/395233.html</wfw:comment><comments>http://www.blogjava.net/guangcaiwudong/articles/395233.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/guangcaiwudong/comments/commentRss/395233.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/guangcaiwudong/services/trackbacks/395233.html</trackback:ping><description><![CDATA[<div><div><strong style="color:black;background-color:#ffff66">DB2</strong>、ORACLE SQL写法的主要区别</div> <div>&nbsp;</div>      <p>说实话，ORACLE把国内的程序员惯坏了，代码中的SQL充斥着大量ORACLE特性，几乎没人知道ANSI的标准SQL是什么样子，导致程序脱离了ORACLE根本无法运行，还好随着hibernate的流行，情况有了很大改观</p> <p><strong style="color:black;background-color:#ffff66">DB2</strong>作为众多国际大客户的选择（据说世界500强80%用<strong style="color:black;background-color:#ffff66">DB2</strong>，前100强更是全部采用<strong style="color:black;background-color:#ffff66">DB2</strong>），在国内真的很不流行，属于小众数据库，但是没办法，现在的项目要用<strong style="color:black;background-color:#ffff66">DB2</strong>，所以不得不面对熟悉ORACLE的开发同事们写出的&#8220;ORACLE版代码&#8221;，众多的兼容性问题搞得很是头大，遂整理了一份经常遇到的兼容性问题列表供大家参考，貌似最近问题少了些，但愿这个势头能继续下去</p> <p><strong>1、数据类型转换函数</strong> </p> <p> </p><table style="border-right: medium none; border-top: medium none; border-left: medium none; border-bottom: medium none; border-collapse: collapse" border="1" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="#" valign="top"> <br /></td> <td style="#" valign="top"> <p>整型转字符型</p></td> <td style="#" valign="top"> <p>字符串转整形</p></td> <td style="#" valign="top"> <p>字符串转浮点型</p></td> <td style="#" valign="top"> <p>浮点型转字符串</p></td> <td style="#" valign="top"> <p>字符串转日期</p></td> <td style="#" valign="top"> <p>字符串转时间戳</p></td> <td style="#" valign="top"> <p>日期转字符串</p></td></tr> <tr> <td style="#" valign="top"> <p>ORACLE</p></td> <td style="#" valign="top"> <p>to_char(1)</p></td> <td style="#" valign="top"> <p>to_number('1')</p></td> <td style="#" valign="top"> <p>to_number('1.1')</p></td> <td style="#" valign="top"> <p>to_char(1.1)</p></td> <td style="#" valign="top"> <p><a name="baidusnap2"></a><strong style="color:black;background-color:#99ff99">to_date</strong>('2007-04-26','yyyy-mm-dd')</p></td> <td style="#" valign="top"> <p><strong style="color:black;background-color:#99ff99">to_date</strong>('2007-04-26 08:08:08','YYYY-MM-DD HH24:MI:SS')</p></td> <td style="#" valign="top"> <p>to_char(<strong style="color:black;background-color:#99ff99">to_date</strong>('2007-04-29','yyyy-mm-dd'),'yyyy-mm-dd') </p></td></tr> <tr> <td style="#" valign="top"> <p><strong style="color:black;background-color:#ffff66">DB2</strong></p></td> <td style="#" valign="top"> <p>char(1)</p></td> <td style="#" valign="top"> <p>int('1')</p></td> <td style="#" valign="top"> <p>double('1.1')</p></td> <td style="#" valign="top"> <p>char(1.1)</p></td> <td valign="top"> <p>date('2007-04-26')</p></td> <td style="#" valign="top"> <p><strong style="color:black;background-color:#99ff99">to_date</strong>('2007-04-26 08:08:08','YYYY-MM-DD HH24:MI:SS')</p></td> <td style="#" valign="top"> <p>char(date('2007-04-29'))</p></td></tr> <tr> <td style="#" valign="top"> <p>兼容写法</p></td> <td style="#" valign="top"> <p>cast(1 as char)</p></td> <td style="#" valign="top"> <p>cast('1' as int)</p></td> <td style="#" valign="top"> <p>无</p></td> <td style="#" valign="top"> <p>无</p></td> <td style="#" valign="top"> <p>无</p></td> <td style="#" valign="top"> <p>兼容</p></td> <td style="#" valign="top"> <p>无</p></td></tr></tbody></table> <p><span style="font-size: 10.5pt"><span><strong>2、Where条件弱类型判断</strong><br />oracle: where 字符型字段 in (整形) 是允许，<strong style="color:black;background-color:#ffff66">DB2</strong>不允许<br />select 'abc' from dual where '1' in (1) 在oracle下可通过<br />select 'abc' from sysibm.sysdummy1 where '1' in (1) 在<strong style="color:black;background-color:#ffff66">DB2</strong>下报错 </span></span></p> <p><span style="font-size: 10.5pt"><span>oracle：where 字符型字段=数字型字段 允许，<strong style="color:black;background-color:#ffff66">DB2</strong>不允许</span></span></p> <p><span style="font-size: 10.5pt"><span>select 'abc' from dual where '1'=1 在oracle下可通过<br />select 'abc' from sysibm.sysdummy1 whre '1'=1 在<strong style="color:black;background-color:#ffff66">DB2</strong>下报错</span></span></p><span style="font-size: 10.5pt"><span><span> <p><br /><strong>3、replace关键字</strong><br />oracle支持，<strong style="color:black;background-color:#ffff66">DB2</strong>不支持 create or replace语句在<strong style="color:black;background-color:#ffff66">DB2</strong>下是非法的</p> <p><br /><strong>4、子查询别名</strong><br />ORACLE 支持select * from(select 1 from dual) 或者 select * from(select 1 from dual) t</p> <p><strong style="color:black;background-color:#ffff66">DB2</strong> 支持select * from(select 1 from sysibm.sysdummy1) t 或者 select * from(select 1 from sysibm.sysdummy1) as t</p> <p>固兼容的写法是select * from(子查询) t</p> <p><br /><strong>5、DATE数据类型的区别</strong><br />ORACLE中DATE型也是带有时分秒的，但<strong style="color:black;background-color:#ffff66">DB2</strong>下DATE只是年月日，如'2007-04-28'，且可作为字符串直接操作，<strong style="color:black;background-color:#ffff66">DB2</strong>中要记录时分秒必须采用TIMESTAMP型</p> <p>一个采用hibernate后常见的兼容问题是：</p> <p>如果在映射文件中定义了某个字段为Date型</p> <p>&lt;property name="createTime" type="java.util.Date" &gt;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;column name="CREATE_TIME" length="7" /&gt;</p> <p>&lt;/property&gt;</p> <p>则在<strong style="color:black;background-color:#ffff66">DB2</strong>下，此字段必须定义为timestamp，而不能定义成DATE，不然会报出字符串右截断的错误</p> <p><br />对于<strong style="color:black;background-color:#ffff66">DB2</strong>来说，在查询条件 中可以直接用字符串指定日期或时间戳类型字段的值，例如 where create_date = '2007-04-26' 、where  create_timestamp = '2007-04-26 08:08:08' ，无须使用字符串转日期函数</p> <p><br /><strong>6、分页的处理</strong><br />如果采用JDBC分页的话，注意rownum在<strong style="color:black;background-color:#ffff66">DB2</strong>中不受支持，比如从masa_area表中取得area_id最小的10条记录，语句分别如下，注意这里的别名t书写方法</p> <p>ORACLE: select t.* from (select rownum as r1 ,masa_area.* from masa_area order by area_id) t where t.r1&lt;=10</p> <p><strong style="color:black;background-color:#ffff66">DB2</strong>: select t.* from (select rownumber() over() as r1 ,masa_area.* from masa_area order by area_id) t where t.r1&lt;=10</p> <p><br /><strong>7、decode函数<br /></strong>decode函数在<strong style="color:black;background-color:#ffff66">DB2</strong>不被支持，兼容的写法是采用case when</p> <p><br /><strong>8、NVL函数<br /></strong>nvl写法在<strong style="color:black;background-color:#ffff66">DB2</strong>不被支持，兼容的写法是采用coalesce</p> <p>ORACLE: select NVL(f_areaid,'空') from masa_user 等同于 select coalesce(f_areaid,'空',f_areaid) from masa_user</p> <p><strong style="color:black;background-color:#ffff66">DB2</strong>: select coalesce(f_areaid,'空',f_areaid) from masa_user</p> <p><br /><strong>9、substr的不同</strong><br /><strong style="color:black;background-color:#ffff66">DB2</strong> substr举例如下：</p> <p>masa_group表的f_groupCode字段定义成VARCHAR(100),所以下面这个语句不会出错，如果是substr(f_groupCode,1,101)就出错了</p> <p>select * from masa_group where substr(f_groupCode,1,50) = '001006' order by f_groupcode</p> <p>在<strong style="color:black;background-color:#ffff66">DB2</strong>下无错，但是</p> <p>select * from masa_group where substr('001006', 1, 50) = '001006' order by f_groupcode</p> <p>就报错，说第三个参数超限</p> <p>这是因为'001006'已经定义为一个长度为6的charater了</p> <p><br />这点和ORACLE有很大不同，请大家注意</p> <p><br />如果是要从第一位取到最后一位，稳妥的办法是不要加第三个参数</p> <p>ORACLE：select substr('123456',1) from dual</p> <p><strong style="color:black;background-color:#ffff66">DB2</strong>：select substr('123456',1) from sysibm.sysdummy1</p> <p>都没有问题</p> <p><br /><strong>10、获取操作系统当前日期</strong></p> <p>ORACLE Sysdate</p> <p><strong style="color:black;background-color:#ffff66">DB2</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CURRENT DATE</p></span></span></span></div><br /><br /><div>http://wjxk.blog.sohu.com/82119411.html</div><img src ="http://www.blogjava.net/guangcaiwudong/aggbug/395233.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/guangcaiwudong/" target="_blank">Kevin_YK</a> 2013-02-07 16:23 <a href="http://www.blogjava.net/guangcaiwudong/articles/395233.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>