5 Eylül 2016 Pazartesi

PostgreSql'de Trigger ile Update İşlemleri

PostgreSql'de Trigger Yazmak:

CREATE TRIGGER check_update
AFTER UPDATE
ON denemetable
FOR EACH ROW
EXECUTE PROCEDURE check_account_update();

Burada "denemetable" adındaki tabloyu update ettiğimizde trigger, "check_account_update()" adlı fonksiyonu tetikler.
After Update : "denemetable" adlı tabloda güncelleme yapıldıktan 'sonra' "check_account_update()" adlı fonksiyon tetiklenir.
Before Update : "denemetable" adlı tabloda güncelleme yapılmadan 'önce' "check_account_update()" adlı fonksiyon tetiklenir.
After Insert :  "denemetable" adlı tabloya kayıt ekleme yapıldıktan 'sonra' "check_account_update()" adlı fonksiyon tetiklenir.
Before Insert :  "denemetable" adlı tabloya kayıt ekleme yapılmadan 'önce' "check_account_update()" adlı fonksiyon tetiklenir.
After Delete :  "denemetable" adlı tablodan kayıt silme işlemi yapıldıktan 'sonra' "check_account_update()" adlı fonksiyon tetiklenir.
Before Delete :  "denemetable" adlı tablodan kayıt silme işlemi yapılmadan 'önce' "check_account_update()" adlı fonksiyon tetiklenir.

Aynı Anda Birden Fazla Update Sorgusunu Çalıştıran "check_account_update()" Trigger Fonksiyonu:

CREATE OR REPLACE FUNCTION fonks() RETURNS TRIGGER AS $update$
DECLARE BEGIN

update STSCSTRUCTARGET set vlperup=(vlpoints*100/4) where (cdrangeitem=203 or cdrangeitem=200 or cdrangeitem=201 or cdrangeitem=202 or cdrangeitem=204);

Update STSCSTRUCTARGET set cdrangeitem=204 where vlperup<40 and (cdrangeitem=203 or cdrangeitem=200 or cdrangeitem=201 or cdrangeitem=202 or cdrangeitem=204);

Update STSCSTRUCTARGET set cdrangeitem=203 where vlperup>=40 and vlperup<70 and (cdrangeitem=203 or cdrangeitem=200 or cdrangeitem=201 or cdrangeitem=202 or cdrangeitem=204);

Update STSCSTRUCTARGET set cdrangeitem=202 where vlperup>=70 and vlperup<90 and (cdrangeitem=203 or cdrangeitem=200 or cdrangeitem=201 or cdrangeitem=202 or cdrangeitem=204);

Update STSCSTRUCTARGET set cdrangeitem=201 where vlperup>=90 and vlperup<115 and (cdrangeitem=203 or cdrangeitem=200 or cdrangeitem=201 or cdrangeitem=202 or cdrangeitem=204);

Update STSCSTRUCTARGET set cdrangeitem=200 where vlperup>=115 and (cdrangeitem=203 or cdrangeitem=200 or cdrangeitem=201 or cdrangeitem=202 or cdrangeitem=204);

update STSCSTRUCTARGET set VLPERUPACCUM=(vlpointsaccum*100/4) where (cdrangeitemaccum=203 or cdrangeitemaccum=200 or cdrangeitemaccum=201 or cdrangeitemaccum=202 or cdrangeitemaccum=204);

Update STSCSTRUCTARGET set cdrangeitemaccum=204 where VLPERUPACCUM<40 and (cdrangeitemaccum=203 or cdrangeitemaccum=200 or cdrangeitemaccum=201 or cdrangeitemaccum=202 or cdrangeitemaccum=204);

Update STSCSTRUCTARGET set cdrangeitemaccum=203 where VLPERUPACCUM>=40 and VLPERUPACCUM<70 and (cdrangeitemaccum=203 or cdrangeitemaccum=200 or cdrangeitemaccum=201 or cdrangeitemaccum=202 or cdrangeitemaccum=204);

Update STSCSTRUCTARGET set cdrangeitemaccum=202 where VLPERUPACCUM>=70 and VLPERUPACCUM<90 and (cdrangeitemaccum=203 or cdrangeitemaccum=200 or cdrangeitemaccum=201 or cdrangeitemaccum=202 or cdrangeitemaccum=204);

Update STSCSTRUCTARGET set cdrangeitemaccum=201 where VLPERUPACCUM>=90 and VLPERUPACCUM<115 and (cdrangeitemaccum=203 or cdrangeitemaccum=200 or cdrangeitemaccum=201 or cdrangeitemaccum=202 or cdrangeitemaccum=204);

Update STSCSTRUCTARGET set cdrangeitemaccum=200 where VLPERUPACCUM>=115 and (cdrangeitemaccum=203 or cdrangeitemaccum=200 or cdrangeitemaccum=201 or cdrangeitemaccum=202 or cdrangeitemaccum=204);
RETURN NULL;


END;
$update$ LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;


Hiç yorum yok:

Yorum Gönder