| | 1 | = Oracle 转 PG 汇总 = |
| | 2 | |
| | 3 | == 语法 == |
| | 4 | * rownum 转为 limit |
| | 5 | {{{ |
| | 6 | oracle: |
| | 7 | select title from sns_pclady.msg_message where rownum<=100; |
| | 8 | |
| | 9 | pg: |
| | 10 | select title from sns_pclady.msg_message limit 100; |
| | 11 | }}} |
| | 12 | * 别名要显式指定 |
| | 13 | {{{ |
| | 14 | oracle: |
| | 15 | select count(*) from ( select * from |
| | 16 | (select * from sns_pclady.msg_message where receiver= 6499175 and delbyreceiver=0 order by posttime desc) where rownum <=1000) where status=0; |
| | 17 | |
| | 18 | pg: |
| | 19 | select count(*) from ( select * from |
| | 20 | (select * from sns_pclady.msg_message where receiver= 6499175 and delbyreceiver=0 order by posttime desc) a limit 1000) b where b.status=0; |
| | 21 | }}} |
| | 22 | * 外连接写法调整 |
| | 23 | {{{ |
| | 24 | oracle: |
| | 25 | select a.field1, b.field2 from a, b where a.item_id = b.item_id(+) |
| | 26 | |
| | 27 | pg: |
| | 28 | select a.field1, b.field2 from a left outer join b on a.item_id = b.item_id |
| | 29 | }}} |
| | 30 | * sequence写法调整 |
| | 31 | {{{ |
| | 32 | oracle: |
| | 33 | sequence_name.nextval |
| | 34 | |
| | 35 | pg: |
| | 36 | nextval('sequence_name') |
| | 37 | }}} |
| | 38 | * sysdate写法调整 |
| | 39 | {{{ |
| | 40 | oracle: |
| | 41 | select sysdate from dual |
| | 42 | |
| | 43 | pg: |
| | 44 | select [db_sysdate] from dual |
| | 45 | }}} |
| | 46 | * decode写法调整 |
| | 47 | {{{ |
| | 48 | oracle: |
| | 49 | decode(expr, search, expr[, search, expr...] [, default]) |
| | 50 | |
| | 51 | pg: |
| | 52 | CASE WHEN expr THEN expr [...] ELSE expr END |
| | 53 | CASE WHEN c1 = 1 THEN 'match' ELSE 'no match' END |
| | 54 | }}} |
| | 55 | * nvl写法调整 |
| | 56 | {{{ |
| | 57 | oracle: |
| | 58 | NVL(hire_date, SYSDATE) |
| | 59 | |
| | 60 | pg: |
| | 61 | coalesce(expr1, expr2, expr3,....) //这个是否可以代替decode? |
| | 62 | }}} |
| | 63 | * connect by 写法pg不支持 |
| | 64 | {{{ |
| | 65 | oracle: |
| | 66 | pg: |
| | 67 | WITH RECURSIVE source (part_no) AS ( |
| | 68 | SELECT 2 |
| | 69 | UNION ALL |
| | 70 | SELECT part.part_no |
| | 71 | FROM source JOIN part ON (source.part_no = part.parent_part_no) |
| | 72 | ) |
| | 73 | SELECT * FROM source; |
| | 74 | }}} |
| | 75 | |
| | 76 | == 驱动 == |
| | 77 | * pg 里没有rollback 语句,不会自动rollback ,需要应用里触发异常。 |
| | 78 | * 缺省返回的整型是Long |
| | 79 | {{{ |
| | 80 | oracle: |
| | 81 | BigDecimal count = (java.math.BigDecimal?)em.createNativeQuery("select count(*) from table").getSingleResult(); |
| | 82 | |
| | 83 | pg: |
| | 84 | Long count = (Long)em.createNativeQuery("select count(*) from table").getSingleResult(); |
| | 85 | }}} |