converting a DB2 query into oracle query

假如想象 提交于 2019-12-05 04:19:01

问题


Previously we used DB2 as database, but now we are migrating to Oracle. Now, in our project we have extensively used sql's that were Db2 specific.

Is there any way to convert those DB2 specific queries to oracle supported queries.

Thanks


回答1:


You have a lot of work ahead!

Between DB2 and Oracle, some important differences are (just an arbitrary enumeration of what I can think of):

Data types

  • Number data types: DB2 has many more standard types, such as SMALLINT, INTEGER, DOUBLE, etc. Those don't exist in Oracle SQL (although some exist in PL/SQL). This is important for DDL and for casting and some other use cases, such as the correctness of predicates
  • Date data types: Oracle's only difference between DATE and TIMESTAMP is the fact that TIMESTAMP has microseconds. But DATE may also contain time information. In DB2, DATE has no time information, I think.
  • Character data types: Read about the difference between VARCHAR and VARCHAR2 in Oracle
  • NULL. In Oracle, NULL is much more general than in DB2. Before DB2 v9.7, you had to cast NULL to any explicit type, e.g. cast(null as integer). That's not necessary in Oracle.

System objects

  • SYSIBM.DUAL simply becomes DUAL
  • Functions: They're all a bit different. You'll have to check case by case. For example, LOCATE becomes INSTR

Syntax

  • TRUNCATE IMMEDIATE becomes TRUNCATE
  • EXCEPT becomes MINUS
  • DB2's FETCH FIRST n ROWS ONLY: There is no such clause in Oracle. You'll have to use ROWNUM or ROW_NUMBER() OVER() filtering (see this example)
  • DB2's MERGE statement is more powerful than that of Oracle, in case you use this.
  • DB2 supports INSERT INTO .. (..) VALUES (..), (..), (..). With Oracle, you'd have to write INSERT INTO .. SELECT .. UNION ALL SELECT .. UNION ALL SELECT ..

Advanced

  • If you use stored procedures, they work a bit differently, especially with advanced data types involved, but that's out of scope here.

Your most efficient shot at this might be to use SQL abstraction of some sort. If you're using Java, I would recommend you wrap your SQL statements with jOOQ (Disclaimer: I work for the company behind jOOQ). jOOQ provides API-level abstraction for all of the above facts. A great deal of SQL can be executed both on DB2 and Oracle, without adaptation. We're also working on a more independent translator product: https://www.jooq.org/translate

On a higher level of abstraction, Hibernate (or other JPA implementations) can do the same for you




回答2:


I found out that there are also some differences in the management of character strings.

  • DB2 doesn't care about the trailing whitespaces when comparing:

    /* DB2 */
    SELECT CASE WHEN ('A   ' = 'A') THEN 'true' ELSE 'false' END FROM SYSIBM.SYSDUMMY1
    --> true
    
    /* Oracle */
    SELECT CASE WHEN ('A   ' = 'A') THEN 'true' ELSE 'false' END FROM DUAL
    --> false
    
  • Oracle considers that '' equals NULL:

    /* DB2 */
    SELECT CASE WHEN ('' IS NULL) THEN 'true' ELSE 'false' END FROM SYSIBM.SYSDUMMY1
    --> false
    
    /* Oracle */
    SELECT CASE WHEN ('' IS NULL) THEN 'true' ELSE 'false' END FROM DUAL
    --> true
    


来源:https://stackoverflow.com/questions/6951713/converting-a-db2-query-into-oracle-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!