数据库postgresql函数(存储过程)的三个例子

 例子1:

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

 

例子2:

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

 

例子3:

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

你可能感兴趣的:(数据库应用)