mercredi 22 mars 2017

Nested IF THEN ELSE in sql statement throws syntax error

I am trying the following query in SQL (Postgres) but am getting a syntax error. What am I missing here:

FOR rec in () LOOP IF rec.status = 'success' THEN IF (condition) and (condition) THEN (do_stuff); ELSE IF (condition) THEN (do_stuff); ELSE IF (condition) THEN (do_stuff); ELSE (do_stuff); END IF; ELSE IF rec.status = 'failed' THEN IF (condition) THEN (do_stuff); ELSE IF (condition) THEN (do_stuff); ELSE IF (condition) THEN (do_stuff); ELSE (do_stuff); END IF; END IF; END LOOP;

The following error occurs at the inner 'END IF' clause. ERROR: syntax error at or near "ELSE"

EDIT: Full block of code:

FOR rec in (select transaction_id as tid,created_at from transaction_recharge_view where wallet_id = '${wids[$i]}' order by created_at asc) LOOP IF rec.status = 'success' THEN IF (select transaction_id from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1) ~ '^MRC' and (select status from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1) = 'success' THEN update recharges set updated_wallet_balance = (select updated_wallet_balance from transaction_recharge_view where transaction_id = rec.tid) + (select amount from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1) where recharge_id = (select transaction_id from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1); ELSE IF (select transaction_id from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1) ~ '^MRC' and (select status from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1) = 'failed' THEN update recharges set updated_wallet_balance = (select updated_wallet_balance from transaction_recharge_view where transaction_id = rec.tid) where recharge_id = (select transaction_id from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1); ELSE IF (select transaction_type from transactions where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1) = 'credit' THEN update transactions set updated_wallet_balance = (select updated_wallet_balance from transaction_recharge_view where transaction_id = rec.tid) + (select amount from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at limit 1) where transaction_id = (select transaction_id from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1); ELSE update transactions set updated_wallet_balance = (select updated_wallet_balance from transaction_recharge_view where transaction_id = rec.tid) - (select amount from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at limit 1) where transaction_id = (select transaction_id from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1); END IF; ELSE IF rec.status = 'failed' THEN IF (select transaction_id from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1) ~ '^MRC' and (select status from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1) = 'success' THEN update recharges set updated_wallet_balance = (select updated_wallet_balance from transaction_recharge_view where transaction_id = rec.tid) + (select amount from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1) where recharge_id = (select transaction_id from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1); ELSE IF (select transaction_id from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1) ~ '^MRC' and (select status from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1) = 'failed' THEN update recharges set updated_wallet_balance = (select updated_wallet_balance from transaction_recharge_view where transaction_id = rec.tid) where recharge_id = (select transaction_id from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1); ELSE IF (select transaction_type from transactions where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1) = 'credit' THEN update transactions set updated_wallet_balance = (select updated_wallet_balance from transaction_recharge_view where transaction_id = rec.tid) + (select amount from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at limit 1) where transaction_id = (select transaction_id from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1); ELSE update transactions set updated_wallet_balance = (select updated_wallet_balance from transaction_recharge_view where transaction_id = rec.tid) - (select amount from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at limit 1) where transaction_id = (select transaction_id from transaction_recharge_view where wallet_id = '${wids[$i]}' and created_at > rec.created_at order by created_at asc limit 1); END IF; END IF; END LOOP;

Aucun commentaire:

Enregistrer un commentaire