• GreenPlum学习笔记:create or replace function创建函数


      原始表数据如下:

      需求:现要求按分号“;”将rate_item列进行分割后插入到新的数据表中。

    CREATE OR REPLACE FUNCTION fun_gp_test_xxx_20181026(v_month int)
    RETURNS INT AS
    $BODY$
    
    declare
    v_num int;
    v_count int;
    
    begin
        v_num := 0;
        v_count := 1;
        
        while v_count > 0 loop
            v_num := v_num + 1;
            select count(1) into v_count from temp_cwh_test_1106 where split_part(rate_item,';',v_num) <> '';
            if v_count > 0 then
                insert into temp_cwh_test_1106_02 select serv_id,usage_date,latn_id,split_part(rate_item,';',v_num) from temp_cwh_test_1106 where split_part(rate_item,';',v_num) <> ''; ---- 需要提前建好temp_cwh_test_1106_02表
            end if;
        end loop;
    return 0;
    end;
    
    
    $BODY$
      LANGUAGE plpgsql VOLATILE;

      创建函数之后,调用函数。

    ---- 函数调用 ----
    select fun_gp_test_xxx_20181026('201810')
    
    ---- 查看结果表 ----
    select * from temp_cwh_test_1106_02;  -- 182

      结果如下:


    END 2018-11-07 00:10:02

  • 相关阅读:
    条件、循环、函数定义 练习
    turtle库基础练习
    Python基础练习
    理解管理信息系统
    上拉刷新下拉加载的网站 mescroll
    数组的方法
    input只允许输入数字和小数
    selcet 苹果兼容
    base64码转图片的方式
    前端下载文件的方式
  • 原文地址:https://www.cnblogs.com/hider/p/9919830.html
Copyright © 2020-2023  润新知