mysql5.7迁移kingbasev8r6(oracle模式)

文章介绍了如何在Kingbase数据库中创建或替换不支持的MySQL内置函数,如curdate(),date_format(),date_sub(),datediff(),find_in_set(),locate(),rand()等,并提供了一些代码适配技巧和注意事项。
摘要由CSDN通过智能技术生成

参考地址:https://bbs.kingbase.com.cn/thread-15275-1-1.html

自定义函数解决kingbase不支持的mysql内置函数


curdate()

CREATE OR REPLACE FUNCTION curdate()
 RETURNS text
AS 
DECLARE 
BEGIN 
 RETURN CURRENT_DATE();
END;

date_format(para1 text, para2 text)

CREATE OR REPLACE FUNCTION date_format(para1 text, para2 text)
 RETURNS text
AS 
DECLARE
form1 TEXT;

BEGIN 
IF  REPLACE(para1, ' ', '') IS NULL THEN RETURN NULL;
END IF;
form1 = REPLACE(para2, '%W', 'Day');

form1 = REPLACE(form1, '%Y', 'YYYY');

form1 = REPLACE(form1, '%a', 'Dy');

form1 = REPLACE(form1, '%d', 'DD');

form1 = REPLACE(form1, '%e', 'DD');

form1 = REPLACE(form1, '%m', 'MM');

form1 = REPLACE(form1, '%c', 'MM');

form1 = REPLACE(form1, '%b', 'Mon');

form1 = REPLACE(form1, '%j', 'DDD');

form1 = REPLACE(form1, '%H', 'HH24');

form1 = REPLACE(form1, '%k', 'HH24');

form1 = REPLACE(form1, '%l', 'HH');

form1 = REPLACE(form1, '%i', 'MI');

form1 = REPLACE(form1, '%r', 'HH:MI:SS');

form1 = REPLACE(form1, '%T', 'HH24:MI:SS');

form1 = REPLACE(form1, '%s', 'SS');

form1 = REPLACE(form1, '%%', '%');

RETURN to_char(to_date(para1), form1);
END;

date_sub(v_date text, v_interval pg_catalog.interval)

CREATE OR REPLACE FUNCTION date_sub(v_date text, v_interval pg_catalog.interval)
 RETURNS text
AS 
declare
v_rt text;
begin
select to_char(v_date::timestamp(0) - v_interval,'yyyy-mm-dd hh24:mi:ss' ) into v_rt;
if length(v_date) = 10 and v_rt like '% 00:00:00' then
select substr(v_rt,0,10) into v_rt;
end if;
return v_rt;
end;

datediff(_start text, _end text)

CREATE OR REPLACE FUNCTION datediff(_start text, _end text)
 RETURNS bigint
AS DECLARE 
BEGIN 
RETURN days_between(_start, _end);
END;

find_in_set(str text, strlist text)

CREATE OR REPLACE FUNCTION find_in_set(str text, strlist text)
 RETURNS integer
AS 

declare

etv int;

len1 int;

arrlen int;

coun int;

tempstr text;

temparr text[];

begin

if strlist is null or strlist=''

then  return 0;

end if;

temparr=string_to_array(strlist, ',');

arrlen=array_length(temparr,1);

raise notice 'arrlen=%',arrlen;

coun=1;

loop if coun>arrlen then exit;

end if;

if str=temparr[coun] then return coun;

end if;

coun=coun+1;

end loop;

return 0;

end;

locate(a1 varchar, a2 varchar)

CREATE OR REPLACE FUNCTION locate(a1 varchar, a2 varchar)
 RETURNS varchar
AS 
BEGIN
return position(a1 in a2 );
END;

rand()

CREATE OR REPLACE FUNCTION rand()
 RETURNS double precision
AS DECLARE
BEGIN 
RETURN random();
END;

to_days(_date text)

CREATE OR REPLACE FUNCTION to_days(_date text)
 RETURNS bigint
AS DECLARE 
BEGIN 
 RETURN days_between(to_date(_date) , to_date('0000-01-01 00:00:00'));
END;

unix_timestamp(_value text)

CREATE OR REPLACE FUNCTION curdate()
 RETURNS text
AS 
DECLARE 
BEGIN 
 RETURN CURRENT_DATE();
END
;

CREATE OR REPLACE FUNCTION date_format(para1 text, para2 text)
 RETURNS text
AS 
DECLARE
form1 TEXT;

BEGIN 
IF  REPLACE(para1, ' ', '') IS NULL THEN RETURN NULL;
END IF;
form1 = REPLACE(para2, '%W', 'Day');

form1 = REPLACE(form1, '%Y', 'YYYY');

form1 = REPLACE(form1, '%a', 'Dy');

form1 = REPLACE(form1, '%d', 'DD');

form1 = REPLACE(form1, '%e', 'DD');

form1 = REPLACE(form1, '%m', 'MM');

form1 = REPLACE(form1, '%c', 'MM');

form1 = REPLACE(form1, '%b', 'Mon');

form1 = REPLACE(form1, '%j', 'DDD');

form1 = REPLACE(form1, '%H', 'HH24');

form1 = REPLACE(form1, '%k', 'HH24');

form1 = REPLACE(form1, '%l', 'HH');

form1 = REPLACE(form1, '%i', 'MI');

form1 = REPLACE(form1, '%r', 'HH:MI:SS');

form1 = REPLACE(form1, '%T', 'HH24:MI:SS');

form1 = REPLACE(form1, '%s', 'SS');

form1 = REPLACE(form1, '%%', '%');

RETURN to_char(to_date(para1), form1);
END
;

CREATE OR REPLACE FUNCTION date_sub(v_date text, v_interval pg_catalog.interval)
 RETURNS text
AS 
declare
v_rt text;
begin
select to_char(v_date::timestamp(0) - v_interval,'yyyy-mm-dd hh24:mi:ss' ) into v_rt;
if length(v_date) = 10 and v_rt like '% 00:00:00' then
select substr(v_rt,0,10) into v_rt;
end if;
return v_rt;
end
;

CREATE OR REPLACE FUNCTION "datediff"(_start text, _end text)
 RETURNS bigint
AS DECLARE 
BEGIN 
RETURN days_between(_start, _end);
END
;

CREATE OR REPLACE FUNCTION find_in_set(str text, strlist text)
 RETURNS integer
AS 

declare

etv int;

len1 int;

arrlen int;

coun int;

tempstr text;

temparr text[];

begin

if strlist is null or strlist=''

then  return 0;

end if;

temparr=string_to_array(strlist, ',');

arrlen=array_length(temparr,1);

raise notice 'arrlen=%',arrlen;

coun=1;

loop if coun>arrlen then exit;

end if;

if str=temparr[coun] then return coun;

end if;

coun=coun+1;

end loop;

return 0;

end
;

CREATE OR REPLACE FUNCTION locate(a1 varchar, a2 varchar)
 RETURNS varchar
AS 
BEGIN
return position(a1 in a2 );
END
;

CREATE OR REPLACE FUNCTION rand()
 RETURNS double precision
AS DECLARE
BEGIN 
RETURN random();
END
;

CREATE OR REPLACE FUNCTION to_days(_date text)
 RETURNS bigint
AS DECLARE 
BEGIN 
 RETURN days_between(to_date(_date) , to_date('0000-01-01 00:00:00'));
END
;

CREATE OR REPLACE FUNCTION unix_timestamp(_value text)
 RETURNS bigint
AS DECLARE 
BEGIN 
 RETURN date_part('epoch', CAST(_value AS TIMESTAMP WITH TIME ZONE));
END;

代码适配

1、INTERVAL类型
INTERVAL 1 DAY 可以用 INTERVAL ‘1’ DAY替换。
2、ANY_VALUE()函数
分组情况下MAX()替换,不分组情况下直接删除。
3、INSERT语句,插入数据值为null的字段。如果有表结构设置了默认值,mysql会使用默认值而不是null,kingbase会使用null而不是默认值。
4、替换isnull() 函数
isnull在kingbase中是关键字,所以不能写自定义函数,可以使用COALESCE函数替换。
举例:isnull(name) => COALESCE(name, ‘y’) = ‘y’。
5、kingbase中select 列表字段别名,如果是关键字则需要用AS 提示,否则报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值