温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PG存储过程是什么

发布时间:2021-10-11 21:52:06 来源:亿速云 阅读:118 作者:iii 栏目:编程语言

这篇文章主要讲解了“PG存储过程是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“PG存储过程是什么”吧!

-- 删除函数
drop function if exists upgrade_task_device(integer);

create or replace function upgrade_task_device(exec_way integer)
    returns integer as
$$
declare
    cron_prefix     varchar default '0 */';
    cron_suffix     varchar default ' * * * ?';
    cron_period     varchar;
    tmp_cron_period integer;
    cron_exp        varchar default '';
    cur_row         record;
    ret             integer default 0;
    task_cursor cursor (exec_way integer) for select t.*
                                              from task t
                                              where t.execution_way = exec_way;
begin
    -- task表如果 time_period > 59,设值为59
    update task set time_period = 59 where time_period is not null and time_period > 59;
    open task_cursor(exec_way);
    loop
        fetch task_cursor into cur_row;
        exit when not FOUND;
        -- 定时执行|立即执行
        if cur_row.execution_way = 1 or cur_row.execution_way = 2 then
            update task_device
            set task_type     = cur_row.task_type,
                task_name     = cur_row.task_name,
                retry         = cur_row.retry,
                start_time=cur_row.start_time,
                end_time=cur_row.end_time,
                -- 执行方式全部更改为‘周期执行’
                execution_way = 3,
                create_user_id=cur_row.create_user_id
            where task_id = cur_row.id
              and time_rule is null;
        elsif cur_row.execution_way = 3 then
            -- 周期执行
            tmp_cron_period = cur_row.time_period;
            if tmp_cron_period > 59 then
                tmp_cron_period = 59;
            end if;
            -- 转换为字符串进行拼接
            cron_period = TRIM(to_char(tmp_cron_period, '99'));
            -- cron 拼接
            cron_exp = cron_prefix || cron_period || cron_suffix;
            update task_device
            set task_type     = cur_row.task_type,
                task_name     = cur_row.task_name,
                retry         = cur_row.retry,
                start_time=cur_row.start_time,
                end_time=cur_row.end_time,
                time_rule=cron_exp,
                -- 执行方式全部更改为‘周期执行’
                execution_way = 3,
                create_user_id=cur_row.create_user_id
            where task_id = cur_row.id
              and time_rule is null;
        end if;
    end loop;
    close task_cursor;
    ret = 1;
    return ret;
end;
$$ language plpgsql;

-- 分别处理定时执行、周期执行、立即执行
select upgrade_task_device(1);
select upgrade_task_device(2);
select upgrade_task_device(3);

-- 删除函数
drop function if exists upgrade_task_device(integer);

感谢各位的阅读,以上就是“PG存储过程是什么”的内容了,经过本文的学习后,相信大家对PG存储过程是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pg
AI