lightdb create or replace支持返回值类型不同的函数创建

文章目录

背景

在24.1之前的版本,LightDB 在PLoraSQL中使用CREATE OR REPLACE FUNCTION 语法进行函数创建时,如果更换了函数的返回值类型会提示创建失败。LightDB 计划在24.1的增量版本对该行为进行支持,允许函数无依赖情况下,成功替换函数返回值类型。

用法

CREATE TABLE test_create_function_emp(no int, ename varchar(32), job varchar(32));

INSERT INTO test_create_function_emp VALUES(1, 'zhangsan', 'job1');

INSERT INTO test_create_function_emp VALUES(2, 'lisi', 'job2');

CREATE or REPLACE FUNCTION get_employee_data RETURN SYS_REFCURSOR IS
    emp_cursor SYS_REFCURSOR;
BEGIN
    OPEN emp_cursor FOR
        SELECT * FROM test_create_function_emp;
    RETURN emp_cursor;
END get_employee_data;
/

DECLARE
    emp_ref_cursor SYS_REFCURSOR;
    emp_record test_create_function_emp%ROWTYPE;
BEGIN
    -- Call the function to get the refcursor
    emp_ref_cursor := get_employee_data;

    -- Fetch data from the refcursor
    LOOP
        FETCH emp_ref_cursor INTO emp_record;
        EXIT WHEN emp_ref_cursor%NOTFOUND;

        -- Process the fetched data (you can print it or do any other operations)
        DBMS_OUTPUT.PUT_LINE('Employee Name: ' || emp_record.ENAME || ', Job: ' || emp_record.JOB);
    END LOOP;

    -- Close the cursor
    CLOSE emp_ref_cursor;
END;
/

Employee Name: zhangsan, Job: job1
Employee Name: lisi, Job: job2


CREATE OR REPLACE FUNCTION get_employee_data RETURN NUMBER IS
    emp_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO emp_count FROM test_create_function_emp;
    RETURN emp_count;
END get_employee_data;
/

DECLARE
    total_employees NUMBER;
BEGIN
    total_employees := get_employee_data;
    DBMS_OUTPUT.PUT_LINE('Total number of employees: ' || total_employees);
END;
/

Total number of employees: 2

drop table test_create_function_emp;
drop function get_employee_data;

总结

在上述示例中,get_employee_data函数 第一次是返回游标类型,替换重建后返回数值类型,函数返回值类型的变化,需要调用者本身去适配。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值