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

2024-07-11 1725阅读

Copyright © 2022 PawSQL

文章目录

    • 概述
    • 虚拟表(dual)
      • 虚拟表dual
      • 虚拟列
        • 虚拟列rownum
        • 虚拟列rowid
        • 字符串函数
          • nvl(col, value)
          • nvl2(col, v1, v2)
          • decode(arg1, arg2, arg3, arg4)
          • substr(str, int, int)
          • instr(str1, str2)
          • replace(srcstr, oldsub[, newsub ])
          • stragg(str,[str])
          • listagg(str, [str])
          • 日期函数
            • sysdate/systimestamp
            • to_date(str, fmt)
            • trunc(arg1, [arg2])
            • add_months(date, int)
            • last_day(date)
            • SQL语句
              • HAVING子句顺序
              • 括号中的表名
              • UNIQUE关键字
              • MINUS关键字
              • FROM关键字
              • NOLOGGING关键字
              • AS关键字
              • FROM子查询的别名
              • UPDATE语句里的字段名
              • 左(右)外连接
              • CONNECT BY子句
              • 操作符的强类型限制
                • 数值运算(+,-,*,/,%)
                • 日期计算(+,-)
                • 字符串拼接(||)
                • 函数参数的强类型限制
                  • substr(arg1, arg2, arg3)
                  • sum(arg)
                  • avg(arg)
                  • round(arg)
                  • 条件判断中的强类型限制
                    • 比较运算(=、>、=、= 2;select tableoid from customer limit 9 OFFSET 23select c_name from customer where rownum select customer.c_name from customer where customer.c_phone = ‘111’ limit 94select * from customer where rownum between 1 and 10;select tableoid from customer limit 10 OFFSET 0

                      虚拟列rowid

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

                      编号OracleOpengauss
                      1select rowid, c.* from customer c;select tableoid || ‘#’ || ctid, c.* from customer as c

                      字符串函数

                      nvl(col, value)

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

                      在Opengauss中重写为coalesce

                      编号OracleOpengauss
                      1select 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…

                      编号OracleOpengauss
                      1select 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…

                      编号OracleOpengauss
                      1select decode(c_phone,‘110’, 1 , 2) from customer;select case when c_phone = ‘110’ then 1 else 2 end from customer
                      2select 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开始计数(需确认)。

                      编号OracleOpengauss
                      1select substr(c_phone, 1 , -2 ) from customer;select substr(c_phone, 1, length(c_phone) - 2) from customer
                      2select 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。当其有多个参数时,无对应函数。

                      编号OracleOpengauss
                      1select instr(‘123’, ‘23’)select strpos(‘123’, ‘23’)

                      replace(srcstr, oldsub[, newsub ])

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

                      编号OracleOpengauss
                      1select replace(‘123’,‘1’);select replace(‘123’,‘1’,‘’);

                      stragg(str,[str])

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

                      编号OracleOpengauss
                      1select listagg(c_name,‘,’) as name from customer group by c_phoneselect string_agg(c_name,‘,’) as name from customer group by c_phone
                      2select listagg(c_name) as name from customer group by c_phoneselect 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)
                          编号OracleOpengauss
                          1select listagg(c_name,‘,’) within group(order by c_name) over (partition by c_phone) as name from customer;sselect string_agg(customer.c_name, ‘,’) over (partition by customer.c_phone order by c_custkey) as name from customer
                          2select listagg(c_name,‘,’) within group(order by c_name) as name from customer group by c_phone;select max(paw_dt.name) as name from (select string_agg(customer.c_name, ‘,’) over (partition by customer.c_phone order by c_name) as name,

                          customer.c_phone

                          from customer) as paw_dt

                          group by c_phone

                          3select listagg(c_name,‘,’) within group(order by c_name) as name from customer group by c_phoneselect string_agg(c_name,‘,’) as name from customer group by c_phone

                          日期函数

                          sysdate/systimestamp

                          Oracle中的sysdate()/sysdate返回系统当前时间(日期+时分秒),在Opengauss中对应now()或是current_timestamp(日期+时分秒+毫秒)。

                          Oracle中的systimestamp返回系统当前时间戳(日期+时分秒+毫秒),在Opengauss中对应now()或是current_timestamp。

                          编号OracleOpengauss
                          1select sysdateselect current_timestamp
                          2select sysdate()select now()
                          3select systimestampselect current_timestamp

                          to_date(str, fmt)

                          Oracle中的to_date返回的是时间类型,而在Opengauss中to_date是日期类型,所以Oracle中的to_date在Opengauss中应该对应to_timestamp

                          编号OracleOpengauss
                          1select to_date( endTime ,‘yyyy-mm-dd hh24:mi:ss’) from tselect to_timestamp( endTime ,‘yyyy-mm-dd hh24:mi:ss’) from t

                          trunc(arg1, [arg2])

                          在Oracle中trunc函数有两种用法

                          • 第一种是对数字进行截取, trunc(num,[int]); 是去掉数字num小数位以后的部分,并且不进行四舍五入。这种用法和在Opengauss的trunc用法一致,不需要转换

                          • trunc函数的第二种用法是对日期进行提取,trunc(date,[fmt])。这种用法在Opengauss对应的函数是date_trunc(fmt, date),需注意在Opengauss中fmt是第一个参数,且不可省略。

                            编号OracleOpengauss
                            1select trunc( 111.23,2)select trunc( 111.23,2)
                            2select trunc(sysdate,‘year’)select date_trunc(‘year’, current_timestamp)
                            3select trunc(sysdate)select date_trunc(‘dd’, current_timestamp)

                            add_months(date, int)

                            Oracle中的add_months 函数主要是对日期函数进行操作,对日期按月增加。在Opengauss没有对应的函数,需将其转化为基于日期和interval的运算。

                            编号OracleOpengauss
                            1select add_months(sysdate, 2)select current_timestamp + 2 * interval ‘1 month’

                            last_day(date)

                            Oracle中的last_day返回指定日期所在月份的最后一天; 在Opengauss没有对应的函数,需将其转化为基于日期和interval的运算。

                            编号OracleOpengauss
                            1select add_months(sysdate, 2)select cast(date_trunc(‘MONTH’, current_timestamp) + interval ‘1 MONTH - 1 DAY’ as date)

                            SQL语句

                            HAVING子句顺序

                            Oracle允许HAVING在GROUP BY子句之前或之后。在Opengauss中,HAVING子句必须出现在GROUP BY子句后面。

                            编号OracleOpengauss
                            1select c_name from customer having count(*) > 2 group by c_nameselect c_name from customer group by c_name having count(*) > 2

                            括号中的表名

                            Oracle中单表引用允许使用括号括起来,Opengauss不允许。

                            编号OracleOpengauss
                            1SELECT * FROM (CUSTOMER);SELECT * FROM CUSTOMER;

                            UNIQUE关键字

                            Oracle中允许使用UNIQUE进行去重,在Opengauss中迁移为DISTINCT关键字

                            编号OracleOpengauss
                            1select unique c_phone from customerselect distinct customer.c_phone from customer

                            MINUS关键字

                            Oracle中可以使用minus关键字来取两个结果集的差,在Opengauss中需迁移为except.

                            编号OracleOpengauss
                            1select c_custkey from customer minus select o_custkey from ordersselect c_custkey from customer except select o_custkey from orders

                            FROM关键字

                            Oracle的delete语句的FROM关键字可以省略,迁移至Opengauss需补充上。

                            编号OracleOpengauss
                            1delete customer where 1=0;delete from customer where 1 = 0

                            NOLOGGING关键字

                            Oracle在执行INSERT语句时,可以通过指定NOLOGGING关键字来减少日志记录,提升操作性能。Opengauss不支持此关键字。

                            编号OracleOpengauss
                            1insert into customer nologging select * from customer_bk;insert into customer select * from customer_bk;

                            AS关键字

                            INSERT INTO 后面不需要添加as关键字,insert into ... as select... 修改为insert into... select...

                            编号OracleOpengauss
                            1insert into t as select c1 from t1insert into t select c1 from t1

                            FROM子查询的别名

                            Oracle中在不引起歧义的情况下子查询可以不带别名,而在Opengauss中,所有的FROM子查询都必须带有别名

                            编号OracleOpengauss
                            1select * from (select * from CUSTOMER)select * from (select * from CUSTOMER) as foo

                            UPDATE语句里的字段名

                            在Opengauss中,Update的时候,更新列不允许添加表名前缀。

                            编号OracleOpengauss
                            1update customer c set c.c_name = ‘xxx’ where c_custkey = 1;update customer set c_name = ‘xxx’ where c_custkey = 1

                            左(右)外连接

                            在Oracle中,外连接可以通过在条件上添加(+)来定义, 连接符(+)跟在哪个条件后面就是哪张表被左连。在Opengauss中,需将其重写为标准的外连接语法。

                            编号OracleOpengauss
                            1select * from customer, orders where c_custkey = o_custkey(+)select * from customer left outer join orders on c_custkey = o_custkey
                            2select * from customer, orders where c_custkey(+) = o_custkey and c_name(+) = o.o_clerk and o_custkey>100select * fromcustomer right outer join orders on (c_custkey = o_custkey and c_name = o_clerk) where o_custkey > 100

                            CONNECT BY子句

                            Oracle中,CONNECT BY 用于存在上下级等层级关系的数据表进行递归查询。语法格式: START WITH condition1 CONNECT BY [ NOCYCLE ] condition2。在Opengauss通过Recursive Common Table Expression来实现此功能,主要是把START WITH… CONNECT BY Prior拆成两个部分,查询表一致,但条件不一致,用UNION ALL合并.

                            编号OracleOpengauss
                            1select id from city_branch start with id=rolebranchid connect by prior id=parent_id;with RECURSIVE MIG_CTE as (
                            select id, 1 as level from city_branch where id = rolebranchid

                            union all
                            select id, level + 1 from city_branch, MIG_CTE where MIG_CTE.id = parent_id)
                            select * from MIG_CTE

                            2select t.branch_level, t.id from city_branch c where (c.branch_level = ‘1’ or t.branch_level = ‘2’) and (t.sign = ‘1’ or t.sign = ‘4’ or t.sign = ‘8’) and t.status = ‘1’ start with c.id = i_branch_id connect by c.id = prior c.parent_id order by c.branch_level descwith RECURSIVE MIG_CTE as

                            (select t.branch_level, t.id, 1 as level from city_branch as cwhere ((((branch_level = ‘1’ or t.branch_level = ‘2’)and ((t.sign = ‘1’ or t.sign = ‘4’) or t.sign = ‘8’)) and t.status = ‘1’) and c.id = i_branch_id)
                            union all
                            select t.branch_level, t.id, level + 1 from city_branch as c, MIG_CTE where ((((branch_level = ‘1’ or t.branch_level = ‘2’) and ((t.sign = ‘1’ or t.sign = ‘4’) or t.sign = ‘8’)) and t.status = ‘1’) and c.id = MIG_CTE.parent_id))
                            select * from MIG_CTE order by MIG_CTE.branch_level desc

                            操作符的强类型限制

                            Oracle中不同类型进行基于操作符的运算,会自动转化类型,譬如select 1 + '1' from dual。Opengauss是强类型,不同类型的运算会提示类型不匹配,执行select 1 + '1'会报错,需要进行显式的类型转换。

                            涉及的操作符类型包括:

                            操作符操作符名称
                            +加法
                            -减法
                            /除法
                            %取余
                            *乘法
                            ||字符串拼接

                            数值运算(+,-,*,/,%)

                            编号OracleOpengauss
                            1select 1 + ‘1’select 1 + 1
                            2select 1 + charCol from tblselect 1 + cast(charCol as numeric) from tbl
                            3select ‘1’ - 1select 1- 1
                            4select 1 * charCol from tblselect 1 * **cast(charCol as numeric) ** from tbl
                            5select 1 / charCol from tblselect 1 /cast(charCol as numeric) from tbl
                            6select charCol % 2 from tblselect cast(charCol as numeric) % 2 from tbl

                            日期计算(+,-)

                            编号OracleOpengauss
                            1select sysdate - 1select current_timestamp - interval ‘1’ DAY
                            2select 1 + sysdate()select interval ‘1’ DAY + now()
                            3select systimestamp +1select current_timestamp + interval ‘1’ DAY
                            4select systimestamp - 1select current_timestamp - interval ‘1’ DAY

                            字符串拼接(||)

                            编号OracleOpengauss
                            1select 1||1select ‘1’||‘1’
                            2select 1 || c_custkeyselect 1 || cast(c_custkey as text)

                            函数参数的强类型限制

                            Oracle中在函数调用时,参数类型进行会自动转化类型,譬如 select substr(123.12,0,2)是合法的,且返回123。Opengauss是强类型, 执行select substr(123.12,0,2)会报错,需要进行显式的类型转换。

                            substr(arg1, arg2, arg3)

                            编号OracleOpengauss
                            1select substr(1234.1, 0, 4)select substr(‘1234.1’, 1, 4+1)
                            2select substr(‘1234.1’, 0, ‘2’)select substr(‘1234.1’, 0, 2)

                            sum(arg)

                            编号OracleOpengauss
                            1select sum(‘2’)select sum(2)

                            avg(arg)

                            编号OracleOpengauss
                            1select avg(‘2’)select avg(2)

                            round(arg)

                            编号OracleOpengauss
                            1select round(‘2’)select round(2)

                            条件判断中的强类型限制

                            Oracle中在进行条件判断时,左右表达式的类型进行会自动转化,譬如 where c_phone = 110是合法的。Opengauss是强类型, 执行where c_phone = 110会报错,需要进行显式的类型转换。

                            比较运算(=、>、=、

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]