資料庫postgresql函數(預存程序)的三個例子

來源:互聯網
上載者:User

 例子1:

-- Function: getopngrd(numeric)</p><p>-- DROP FUNCTION getopngrd(numeric);</p><p>CREATE OR REPLACE FUNCTION getopngrd(opnsq numeric)<br /> RETURNS character varying AS<br />$BODY$<br />DECLARE<br /> rec RECORD;<br /> OpnGrd "varchar" := '';<br /> sql "varchar";<br />BEGIN<br /> sql := 'select opn_grd from t_opn_grd_asg where opn_sq = ' || opnSq || ' order by opn_grd asc';<br /> FOR rec IN EXECUTE sql LOOP<br />OpnGrd := OpnGrd || rec.opn_grd || ',';<br /> END LOOP;<br /> IF OpnGrd != '' THEN<br />OpnGrd := substring(OpnGrd, 0, character_length(OpnGrd));<br /> END IF;<br /> RETURN OpnGrd;<br />END;<br />$BODY$<br /> LANGUAGE 'plpgsql' VOLATILE<br /> COST 100;<br />ALTER FUNCTION getopngrd(numeric) OWNER TO postgres;

 

例子2:

-- Function: getfacdpt(numeric, character varying, character varying)</p><p>-- DROP FUNCTION getfacdpt(numeric, character varying, character varying);</p><p>CREATE OR REPLACE FUNCTION getfacdpt(opnsq numeric, categorycd character varying, schoolid character varying)<br /> RETURNS character varying AS<br />$BODY$<br />DECLARE<br /> rec RECORD;<br /> FacDpt "varchar" := '';<br /> sql "varchar";<br />BEGIN</p><p> sql := ' select t1.org_unit_cd,t2.orgunit_nm,t2.orgunit_snm '<br />|| ' from t_opn_orgunit_ast t1 '<br />|| ' inner join t_orgunit t2 on t1.org_unit_cd=t2.org_unit_cd and t1.school_id=t2.school_id '<br />|| ' where t1.opn_sq=' || opnSq<br />|| ' and t1.opn_category_cd=/'' || categoryCd || '/''<br />|| ' and t1.school_id=/'' || schoolId || '/''<br />|| ' order by t1.org_unit_cd asc ';</p><p> FOR rec IN EXECUTE sql LOOP<br />FacDpt := FacDpt || rec.orgunit_nm || ',';<br /> END LOOP;<br /> IF FacDpt != '' THEN<br />FacDpt := substring(FacDpt, 0, character_length(FacDpt));<br /> END IF;<br /> RETURN FacDpt;<br />END;<br />$BODY$<br /> LANGUAGE 'plpgsql' VOLATILE<br /> COST 100;<br />ALTER FUNCTION getfacdpt(numeric, character varying, character varying) OWNER TO postgres;

 

例子3:

-- Function: getopntmasg(numeric)</p><p>-- DROP FUNCTION getopntmasg(numeric);</p><p>CREATE OR REPLACE FUNCTION getopntmasg(opnsq numeric)<br /> RETURNS character varying AS<br />$BODY$<br />DECLARE<br /> rec RECORD;<br /> OpnTmAsg "varchar" := '';<br /> sql "varchar";<br />BEGIN<br /> sql := ' select distinct t1.opn_sq, t1.opn_day_of_wk_dv as opn_day_of_wk_dv, '<br />|| ' t2.day_of_wk_nm as day_of_wk_nm, t1.opn_stt_prd_crd_cd as opn_stt_prd_crd_cd, '<br />|| ' t1.opn_prd_crd_lg as opn_prd_crd_lg, t1.mn_opn_tm_flg as mn_opn_tm_flg, '<br />|| ' t1.prd_crd_ord as prd_crd_ord, t1.prd_ord as prd_ord '<br />|| ' from t_opn_tm_asg t1, t_day_of_wk t2 where t1.opn_sq = ' || opnSq<br />|| ' and t1.opn_day_of_wk_dv = t2.day_of_wk_dv order by t1.opn_day_of_wk_dv asc ';<br /> FOR rec IN EXECUTE sql LOOP<br />IF rec.prd_ord + ceiling(rec.opn_prd_crd_lg / 2) - 1 = rec.prd_ord THEN<br /> OpnTmAsg := OpnTmAsg || substring(rec.day_of_wk_nm, 0, 2) || rec.prd_ord || ',';<br />ELSE<br /> OpnTmAsg := OpnTmAsg || substring(rec.day_of_wk_nm, 0, 2) || rec.prd_ord || '-' || rec.prd_ord + ceiling(rec.opn_prd_crd_lg / 2) - 1 || ',';<br />END IF;</p><p> END LOOP;<br /> IF OpnTmAsg != '' THEN<br />OpnTmAsg := substring(OpnTmAsg, 0, character_length(OpnTmAsg));<br /> END IF;<br /> RETURN OpnTmAsg;<br />END;<br />$BODY$<br /> LANGUAGE 'plpgsql' VOLATILE<br /> COST 100;<br />ALTER FUNCTION getopntmasg(numeric) OWNER TO postgres;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.