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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM