Postgresql-动态字段查询操作

DROP FUNCTION test_DynamicColumn();
DROP TYPE sc_month_report_result;
CREATE TYPE sc_month_report_result AS (
  score TEXT,
  count INTEGER
);
CREATE OR REPLACE FUNCTION test_DynamicColumn()
  RETURNS VOID AS $$

DECLARE   sc_month_report_result_obj sc_month_report_result [];
  DECLARE columns               TEXT [];
  DECLARE result                TEXT;
BEGIN
  columns [0] = 'repay';

  EXECUTE 'SELECT apply_no FROM sc_month_report
  WHERE substr(' || scores[0] || '_date:: VARCHAR, 7, 1) = 1 :: VARCHAR
        AND sc_month_report.score = ''1_0-60''' INTO result;

  RAISE NOTICE '--->%', (columns [0] || '_date');
  RAISE NOTICE '--->%', result;
END;
$$ LANGUAGE plpgsql;
SELECT test_DynamicColumn();
    原文作者:一条湫刀鱼
    原文地址: https://www.jianshu.com/p/414ee9979ced
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞