En iyi yanıtlayıcılar
trigger fonksiyonu ile ilgili

Soru
-
Merhaba. Dun sorduğum soru bir az karmaşık idi o yuzden yeniden sormak istedim. postgrsqlde tablo yapım bu şekilde.
CREATE TABLE product ( id serial PRIMARY KEY NOT NULL, barcode varchar(50) NOT NULL, productname varchar(50) NOT NULL, eded_say int NOT NULL, durum boolean NOT NULL default false, FOREIGN KEY(categoryid) REFERENCES categories(id) on delete cascade on update cascade ); CREATE TABLE buy ( id serial PRIMARY KEY NOT NULL, productid int NOT NULL, miqdar int NOT NULL, birim varchar(20) NOT NULL, alis numeric(6,2) NOT NULL, durum boolean NOT NULL default false, FOREIGN KEY(productid) REFERENCES product(id) on delete cascade on update cascade ); CREATE TABLE stok ( id serial PRIMARY KEY NOT NULL, productid int UNIQUE NOT NULL, miqdar int NOT NULL, FOREIGN KEY(productid) REFERENCES product(id) on delete cascade on update cascade
);CREATE TABLE sale ( productid int NOT NULL, miqdar int NOT NULL, birim varchar(20) NOT NULL, satis numeric(6,2) NOT NULL, durum boolean NOT NULL default false, FOREIGN KEY(productid) REFERENCES product(id) on delete cascade on update cascade );
Farz edelim ki, bir eczane proqrami. Eczanede ilaçlar hem kutu ile hem de adetle satıla ve ya alina biler. Onun için ben hem kutu hemde adetle satılan bilen ilaçların kutusunun içinde kaç adet olursa onu önceden product tablosuna ekliyorum (eded_say kolonu) Aynı zamanda da öyle ilaçları (hem kutu hemde adetle satıla bilen) kontrol etmek için product tablosunda durum isimli kolon yaratdım. ( kolon tipi bool). Eczanede olan ilaçların miktarını ve id numarasını tutmak için stok isimli tablo yaratdım. Tabloda ilaçların miktarını toplam adet olarak saklamak istiyorum ki, sonra o ilaçdan kaç kutu kaç adet kaldığını bulum. Bu şekilde:
var liste = from k in ctx.stoks select new { k.id, k.product.eded_say, k.product.productname, Kutu = k.product.durum == true ? 0 : k.miqdar / k.product.eded_say, Adet = k.product.durum == true ? k.miqdar : k.miqdar % k.product.eded_say };
Umarım bu sorğuda ne anlatmak istiyorum biliniyor.
Bu ekleme isleminde id-i 1 olan hem kutu ile hemde adetle satıla bilir.
INSERT INTO public.product( id, productname, eded_sayi, durum) VALUES (1, "Remantadin", 30,true);
Bu ekleme isleminde id-i 2 olan yalnız adetle satıla bilir.
INSERT INTO public.product( id, productname, eded_sayi, durum) VALUES (1, "Nurofen", 1,false);
INSERT INTO public.buy( id, productid, miqdar, birim, durum) VALUES (1, 1, 10, 'kutu',true); INSERT INTO public.buy( id, productid, miqdar, birim, durum) VALUES (1, 2, 20, 'adet',false);
Stok tablosu sonucda bu şekilde olmalıdır.
İd Productİd Miktar
1 1 300
2 2 20
buy tablosuna veri girilende trigger ile stok tablosuna miktar ekleniyor ve ya guncelleniyor. Benim istediyim buy tablosundakı durum kolonundakı deger true-se o zaman miktar buy tablosundan gelen deger * product tablosundakı eded_sayi olsun. if (buy.durum == true)
(buy.miqdar += (buy.miqdar * product.eded_sayi))
else buy.miqdar += buy.miqdar
c# tarafda bu şeklide. Ben bu işlemi trigger funksiyoni ile yapmak istiyorum. Aşağıdakı fonksiyonda göründüyü gibi tekce stok.miqdar + EXCULED.miqdar gibidir. Ama benim istediyim kontrol etsin buy tablosundak durum true-se stok.miqdar + (product.eded_sayi * EXCULED.miqdar) yok false -e stok.miqdar + EXCULED.miqdar
CREATE OR REPLACE FUNCTION process_buy_insert() RETURNS TRIGGER AS $process_buy_insert$ BEGIN IF (TG_OP = 'INSERT') THEN INSERT INTO stok (productId, miqdar) SELECT NEW.productId, NEW.miqdar ON CONFLICT (productid) DO UPDATE Set miqdar = stok.miqdar + EXCLUDED.miqdar; END IF; RETURN NULL; END; $process_buy_insert$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION process_sale_insert() RETURNS TRIGGER AS $process_sale_insert$ BEGIN IF (TG_OP = 'INSERT') THEN INSERT INTO stok (productId, miqdar) SELECT NEW.productId, NEW.miqdar ON CONFLICT (productid) DO UPDATE Set miqdar = stok.miqdar - EXCLUDED.miqdar; END IF; IF (TG_OP = 'UPDATE') THEN Update stok Set miqdar = stok.miqdar + (OLD.miqdar-New.miqdar) where productId = NEW.productId; END IF; IF (TG_OP = 'DELETE') THEN Update stok Set miqdar = stok.miqdar + OLD.miqdar where productId = OLD.productId; END IF; RETURN NULL; END; $process_sale_insert$ LANGUAGE plpgsql;
Bilmem nasıl anlata bildimmi ne yapmak istediyimi? Aynı zamanda da sale tablosuda öyle.
- Düzenleyen vusal.nurehmedov 16 Temmuz 2019 Salı 08:46
Yanıtlar
-
Nihat,
Ayni cevabi her iki soruna da ekliyorum.
Bu sekilde stok tutmanı ben şahsen mantıklı bulmuyorum ama sen bilirsin:
CREATE OR REPLACE FUNCTION process_buy_insert() RETURNS TRIGGER AS $process_buy_insert$ declare carpan integer; BEGIN select case when p.durum then p.eded_say else 1 end from product p where p.Id = NEW.productId into carpan; IF (TG_OP = 'INSERT') THEN INSERT INTO stok (productId, miqdar) select NEW.productId, NEW.miqdar*carpan ON CONFLICT (productid) DO UPDATE Set miqdar = stok.miqdar + EXCLUDED.miqdar; END IF; RETURN NULL; END; $process_buy_insert$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION process_sale_insert() RETURNS TRIGGER AS $process_sale_insert$ declare carpan integer; BEGIN select case when p.durum then p.eded_say else 1 end from product p where p.Id = NEW.productId into carpan; IF (TG_OP = 'INSERT') THEN INSERT INTO stok (productId, miqdar) SELECT NEW.productId, NEW.miqdar*carpan ON CONFLICT (productid) DO UPDATE Set miqdar = stok.miqdar - EXCLUDED.miqdar; END IF; IF (TG_OP = 'UPDATE') THEN Update stok Set miqdar = stok.miqdar + ((OLD.miqdar-New.miqdar)*carpan) where productId = NEW.productId; END IF; IF (TG_OP = 'DELETE') THEN select case when p.durum then p.eded_say else 1 end from product p where p.Id = OLD.productId into carpan; Update stok Set miqdar = stok.miqdar + (OLD.miqdar*carpan) where productId = OLD.productId; END IF; RETURN NULL; END; $process_sale_insert$ LANGUAGE plpgsql;
How to create a Minimal, Reproducible Example
The way to Go.
World's most advanced open source (object-) relational Database.
Flutter (for mobile, for web & desktop.- Yanıt Olarak İşaretleyen vusal.nurehmedov 17 Temmuz 2019 Çarşamba 08:07
Tüm Yanıtlar
-
-
Nihat,
Ayni cevabi her iki soruna da ekliyorum.
Bu sekilde stok tutmanı ben şahsen mantıklı bulmuyorum ama sen bilirsin:
CREATE OR REPLACE FUNCTION process_buy_insert() RETURNS TRIGGER AS $process_buy_insert$ declare carpan integer; BEGIN select case when p.durum then p.eded_say else 1 end from product p where p.Id = NEW.productId into carpan; IF (TG_OP = 'INSERT') THEN INSERT INTO stok (productId, miqdar) select NEW.productId, NEW.miqdar*carpan ON CONFLICT (productid) DO UPDATE Set miqdar = stok.miqdar + EXCLUDED.miqdar; END IF; RETURN NULL; END; $process_buy_insert$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION process_sale_insert() RETURNS TRIGGER AS $process_sale_insert$ declare carpan integer; BEGIN select case when p.durum then p.eded_say else 1 end from product p where p.Id = NEW.productId into carpan; IF (TG_OP = 'INSERT') THEN INSERT INTO stok (productId, miqdar) SELECT NEW.productId, NEW.miqdar*carpan ON CONFLICT (productid) DO UPDATE Set miqdar = stok.miqdar - EXCLUDED.miqdar; END IF; IF (TG_OP = 'UPDATE') THEN Update stok Set miqdar = stok.miqdar + ((OLD.miqdar-New.miqdar)*carpan) where productId = NEW.productId; END IF; IF (TG_OP = 'DELETE') THEN select case when p.durum then p.eded_say else 1 end from product p where p.Id = OLD.productId into carpan; Update stok Set miqdar = stok.miqdar + (OLD.miqdar*carpan) where productId = OLD.productId; END IF; RETURN NULL; END; $process_sale_insert$ LANGUAGE plpgsql;
How to create a Minimal, Reproducible Example
The way to Go.
World's most advanced open source (object-) relational Database.
Flutter (for mobile, for web & desktop.- Yanıt Olarak İşaretleyen vusal.nurehmedov 17 Temmuz 2019 Çarşamba 08:07