Вы пропустили точку с запятой внутри оператора UPDATE
. Изменение в COUNTER ||';';
.
---stored procedure
CREATE OR REPLACE PROCEDURE CDW.redshift_sample_sp ()
AS $$
DECLARE
rowcount INT ;
rwnm INT;
printval INT ;
rec RECORD;
days_supplied INT;
sp_id INT;
lh_patient_id1 INT;
ship_dt DATE;
next_ship_dt1 DATE;
anticipated_dt DATE;
days_balance INT;
days_diff INT;
counter BIGINT;
BEGIN
-----------------Insert Into begins---------------------
EXECUTE ' CREATE TEMP TABLE tmp_variables AS (select * from tmp_variables_new);';
RAISE INFO 'CREATED TEMP TABLE';
rwnm = (SELECT MAX(rownum) FROM tmp_variables);
days_balance = 0;
counter = 1;
RAISE INFO 'UPDATED COUNTER';
FOR rec IN SELECT * FROM tmp_variables WHERE ROWNUM<=RWNM
LOOP
RAISE INFO 'INSIDE FOR LOOP';
days_supplied=(SELECT days_supplied1 FROM tmp_variables WHERE rownum = counter);
RAISE INFO 'days_supplied = %', days_supplied;
sp_id=(SELECT sp_id1 FROM tmp_variables WHERE rownum = counter);
RAISE INFO 'sp_id = %', sp_id;
lh_patient_id1=(SELECT lh_patient_id from tmp_variables WHERE rownum = counter);
RAISE INFO 'lh_patient_id1 = %', lh_patient_id1;
ship_dt=(SELECT ship_date FROM tmp_variables WHERE rownum = counter);
RAISE INFO 'ship_dt = %', ship_dt;
next_ship_dt1= (SELECT next_ship_dt FROM tmp_variables WHERE rownum = counter);
RAISE INFO 'next_ship_dt1 = %', next_ship_dt1;
anticipated_dt = DATEADD(d, days_supplied + days_balance, ship_dt)::DATE;
RAISE INFO 'days_balance = %', days_balance;
RAISE INFO 'Anticipated_dt = %', anticipated_dt;
IF (anticipated_dt > next_ship_dt1) THEN
days_balance = ABS(DATEDIFF(d, anticipated_dt, next_ship_dt1));
RAISE INFO 'days_balance NOW = %', days_balance;
ELSE
days_balance = 0;
RAISE INFO 'IF LOOP ENDS';
END IF;
EXECUTE 'UPDATE tmp_variables '
||' SET next_anticipated_ship_dt = '''||anticipated_dt::DATE
||''' WHERE rownum = ' || COUNTER ||';';
RAISE INFO 'UPDATE STATEMENT ENDS';
COUNTER=COUNTER+1;
RAISE INFO 'UPDATED COUNTER = %', COUNTER;
END LOOP;
END;
$$ LANGUAGE plpgsql;
CALL CDW.redshift_sample_sp ();