Oracle到高斯数据库的SQL语法迁移手册(建议收藏)

概述

异构数据库的迁移(譬如从Oracle迁移到openGauss)工作主要包括三个方面,

  • 数据库对象的迁移,包括库、模式、表、索引、视图、触发器、存储过程等等;

  • 数据内容的迁移,主要指的是数据表中数据的迁移;

  • 数据应用的迁移,主要指的是应用中SQL语句的迁移。

目前对于数据库对象以及数据内容的迁移有很多成熟的工具,而对于应用迁移的工具却很少能够见到。原因是因为DML语句比DDL复杂的多,不同的数据库语法差异也比较大。目前市场上的迁移工具大多使用正则表达式来解析SQL语句,而DML语句的复杂性导致此类工具的解析成功率较低,难以作为一个成熟地商业产品进行推广。

PawSQL团队开发的DML语法转换工具Ora2ogSQL,通过PawSQL强大的SQLParser,能够解析几乎所有的Oracle语法,并将其转换为对应的openGauss语法,支持数据库应用的平滑迁移。

本手册介绍了Oracle和openGauss的语法区别,以及转换映射关系,可以作为迁移人员的SQL迁移参考手册。

虚拟表(dual)

虚拟表dual

Oracle获取一个常量需要通过一个dual,Opengauss不需要

编号 Oracle Opengauss
1 select 2 from dual select 2




虚拟列

虚拟列rownum

  对于查询返回的每行数据,rownum虚拟列会返回一个数字,第一行的ROWNUM为1,第二行为2,以此类推。

  • rownum在select列表中时重写为row_number() over ()

  • rownum在where子句中时重写为limit... offset...

编号 Oracle Opengauss
1 select rownum from customer; select row_number() over () as rownum  from customer
2 select tableoid from customer where rownum = 2; select tableoid  from customer limit 9 OFFSET 2
3 select c_name from customer where rownum < 10 and c_phone = '111'; select customer.c_name  from customer  where customer.c_phone = '111' limit 9
4 select * from customer where rownum between 1 and 10; select * from customer limit 10 OFFSET 0

虚拟列rowid

  Oracle中的rowid虚拟列返回特定行的具体地址,在Opengauss中重写为tableoid || '#' || ctid

编号 Oracle Opengauss
1 select rowid, c.* from customer c; select tableoid || '#' || ctid, c.* from customer as c




字符串函数

nvl(col, value)

Oracle中的nvl(col, value)用来设置默认值,col为空就设置为value;

在Opengauss中重写为coalesce

编号 Oracle Opengauss
1 select nvl(c_phone, 1) from customer; select coalesce(customer.c_phone, '1')  from customer



nvl2(col, v1, v2)

nvl2对col的null值进行处理,如果col为null,则返回v1, 否则返回v2;

postgre中没有类似的函数,可以重写为case... when...

编号 Oracle Opengauss
1 select nvl2(c_phone, 1, 2) from customer; select case when c_phone is null then 1  else 2  end  from customer



decode(arg1, arg2, arg3, arg4)

Oracle中的decode(arg1, arg2, arg3, arg4)函数, 表示当 arg1 等于 arg2 时,取 arg3,否则取 arg4。

postgre中没有类似的函数,可以重写为case... when...

编号 Oracle Opengauss
1 select decode(c_phone,'110', 1 , 2) from customer; select case when c_phone = '110' then 1  else 2  end   from customer
2 select decode(c_phone,null, 1 , 2) from customer; select case when c_phone is null then 1  else 2  end   from customer

substr(str, int, int)

Oracle中的substr用来取一个字符串的子串,Opengauss有同名的函数实现类似功能。不同的是Oracle中,第二、第三个参数可以为负数,代表从后面进行计数,Opengauss不允许其为负数,需对其进行转换。Oracle中是以0开始计数,Opengauss以1开始计数(需确认)。

编号 Oracle Opengauss
1 select substr(c_phone, 1 , -2 ) from customer; select substr(c_phone, 1, length(c_phone) - 2)  from customer
2 select substr(c_phone, -3 , 1 ) from customer; select substr(c_phone, length(c_phone) - 3, 1) from customer

instr(str1, str2)

Oracle中的instr用来取一个字符串的子串位置,当其只有两个参数时,表示子串的第一次出现的位置,和Opengauss中对应的函数为strpos。当其有多个参数时,无对应函数。

编号 Oracle Opengauss
1 select instr('123', '23') select strpos('123', '23')



replace(srcstr, oldsub[, newsub ])

在Oracle中,replace()函数用于替换字符串, replace(srcstr, oldsub[, newsub ] ),和Opengauss中的replace函数用法基本一致。只是需要注意在Oracle中无第三个参数时,代表删除此字符,在Opengauss可将第三个参数设置为''。

编号 Oracle Opengauss
1 select replace('123','1'); select replace('123','1','');



stragg(str,[str])

Oracle里的stragg函数实现在分组内对列值的拼接,它和listagg类似,但是不可以指定拼接的顺序。在Opengauss中,可以使用string_agg函数来替换。其第二个参数可选,默认值为'',在Opengauss需补充第二个参数。

编号 Oracle Opengauss
1 select listagg(c_name,',') as name from customer group by c_phone select string_agg(c_name,',') as name from customer group by c_phone
2 select listagg(c_name) as name from customer group by c_phone select listagg(c_name,'') as name from customer group by c_phone

listagg(str, [str])

Oracle里的listagg函数实现对列值的拼接,它可以在分组内以指定顺序对非分组列进行拼接。在Opengauss中,可以使用string_agg函数来实现,需注意语法方面也有区别. 另外,其第二个参数可选,默认值为'',在Opengauss需补充第二个参数。

  • 当没有group by子句时,可以使用over(partiton by... order by...)进行替换

  • 当指定group by子句时,它的重写算法比较复杂

    • 如果需要保持拼接的顺序,需要通过子查询来实现(见编号2)

    • 如果不需要保持拼接顺序,可以把它转化为简单的聚集函数(编号3)