我正在 PostgreSQL 中编写一个函数,想知道是否可以执行以下操作:
我在每个 if 循环中都有一个插入语句。我可以为 formatdate1 和 formatdate2 传递这样的值吗?
我也在更新一张桌子。是我们如何做 PostgreSQL 吗?
CREATE OR REPLACE FUNCTION Check returns void AS $$ DECLARE startDate=date; formateDate1=date; formatdate2=date;newDate=date; BEGIN startDate:= SELECT to_date(lastdate::date, 'MM-DD-YYYY') FROM setup; for i in 1..3 LOOP IF i = 1 THEN formateDate1 := select (startDate - INTERVAL '11 months'); formatdate2:= to_date(formatdate2::date,'YYYYMM'); insert into warehouse.memcnts1 (select distinct source, formatdate2 as yearmo, to_date(formateDate1, 'MM-DD-YYYY') where effdt <= formateDate1 and enddt >= formateDate1); ELSIF i = 2 THEN -- this is todays date --insert query here insert into warehouse.memcnts1 (select distinct source,formatdate2 as yearmo, to_date(formateDate1, 'MM-DD-YYYY') where effdt <= formateDate1 and enddt >= formateDate1); ELSIF i = 3 THEN formateDate1 := select (startDate + INTERVAL '1 months'); newDate=formateDate1; update dwset SET lastdate := newDate; -- wonder if this is right? formatdate2:=startDate; END IF; END LOOP; END $$ language 'plpgsql';