<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Forum PostgreSQL - Trigger update'ujacy pole typu DATE]]></title>
	<link rel="self" href="http://forum.postgresql.org.pl/extern.php?action=feed&amp;tid=231&amp;type=atom"/>
	<updated>2009-06-16T11:20:46Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.postgresql.org.pl/viewtopic.php?id=231</id>
		<entry>
			<title type="html"><![CDATA[Odp: Trigger update'ujacy pole typu DATE]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=991#p991"/>
			<content type="html"><![CDATA[Wydajnościowo pewnie szybciej bedzie wołać beposrednio update'a.
Co do trybu opaque to zostal zastąpiony typem trigger.

pz]]></content>
			<author>
				<name><![CDATA[rski]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=26</uri>
			</author>
			<updated>2009-06-16T11:20:46Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=991#p991</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: Trigger update'ujacy pole typu DATE]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=990#p990"/>
			<content type="html"><![CDATA[Sorki, że tak długo nie napisałem. 
Podane wyżej rozwiązanie DZIAŁA. :):)

Bardzo dziękuję za pomoc.

całość funkcji wygląda teraz tak:

[code]create function splata_trigger() returns opaque AS' 
declare
BEGIN 
 if(old.ilosc_pozyczki = new.splata_pozyczki ) and (old.ilosc_pozyczki != old.splata_pozyczki) then
   update pozyczki set data_splaty = (select current_date) WHERE splata_pozyczki = NEW.splata_pozyczki;
  return NEW;
 else
  return NULL;
 end if;
 
END;
' language 'plpgsql';[/code]

Aha Postgres przy 'kompilacji' zmienia typ zwracany przez tą funkcję z opaque na trigger co nie jest żadnym problemem;)

Zastanawiam się czy lepiej nie napisać funkcji do update'u, która przy okazji sprawdzałaby ten warunek spłaty i działaby zamiast tego triggera:)
POZDRAWIAM i jeszcze raz DZIĘKI!]]></content>
			<author>
				<name><![CDATA[krzysko]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=925</uri>
			</author>
			<updated>2009-06-16T06:19:41Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=990#p990</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: Trigger update'ujacy pole typu DATE]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=989#p989"/>
			<content type="html"><![CDATA[Ok. Sprawdzę to i dam znać co z tego wyszło:)]]></content>
			<author>
				<name><![CDATA[krzysko]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=925</uri>
			</author>
			<updated>2009-06-15T05:46:35Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=989#p989</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: Trigger update'ujacy pole typu DATE]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=978#p978"/>
			<content type="html"><![CDATA[Mógłbyś dodać do warunku w if'ie sprawdzenie czy
[code]
old.ilosc_pozyczki <> old.splata_pozyczki
[/code]
(tylko nie pamiętam czy operator 'różne od' to <> czy != :).)
To powinno zadziałać. Chyba że nie zrozumiałem do końca logiki działania tego triggera.]]></content>
			<author>
				<name><![CDATA[rski]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=26</uri>
			</author>
			<updated>2009-06-11T18:03:16Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=978#p978</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: Trigger update'ujacy pole typu DATE]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=977#p977"/>
			<content type="html"><![CDATA[Słuszna uwaga :) a czy masz jakis pomysł jak to rozwiązać?]]></content>
			<author>
				<name><![CDATA[krzysko]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=925</uri>
			</author>
			<updated>2009-06-11T17:29:45Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=977#p977</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: Trigger update'ujacy pole typu DATE]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=976#p976"/>
			<content type="html"><![CDATA[Wydaje mi sie ze sie zapetla. Jak zrobisz update rowny ilosc_pozyczki to trigger robi znowu update, ktory znowu uruchamia trigger  a w nim warunek 
[code]
old.ilosc_pozyczki = new.splata_pozyczki
[/code]
jest spelniony i tak w kolko - petla.
Czy sie mylę?]]></content>
			<author>
				<name><![CDATA[rski]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=26</uri>
			</author>
			<updated>2009-06-11T13:02:34Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=976#p976</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: Trigger update'ujacy pole typu DATE]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=974#p974"/>
			<content type="html"><![CDATA[Sorki, że nie napisałem, a więc tak:
- jeśli robię update mniejszy niż ilosc_pozyczki to jest ok wiersz jest updateowany
- jeśli robię update równy ilosc_pozyczki taki jak ten:
[code]
UPDATE pozyczki SET splata_pozyczki = 400 where id_pozyczki = 1;
[/code]
to zaczyna chwilowo mulić i wyrzuca sporo takich samych komunikatów jak ten:

[code]wyrażenie SQL "update pozyczki set data_splaty = (select current_date)"
PL/pgSQL function "splata_trigger" line 7 at SQL statement[/code]]]></content>
			<author>
				<name><![CDATA[krzysko]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=925</uri>
			</author>
			<updated>2009-06-11T12:32:31Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=974#p974</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: Trigger update'ujacy pole typu DATE]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=965#p965"/>
			<content type="html"><![CDATA[Co znaczy nie działa? Jakiś komunikat błędu czy robi nie to co trzeba?]]></content>
			<author>
				<name><![CDATA[rski]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=26</uri>
			</author>
			<updated>2009-06-10T13:29:18Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=965#p965</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Trigger update'ujacy pole typu DATE]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=964#p964"/>
			<content type="html"><![CDATA[Witam wszystkich serdecznie.
W myśl: "Żadne pytanie nie jest zbyt proste dla tego działu." proszę o pomoc w poniższym problemie:
Chciałbym stworzyć wyzwalacz, który po spełnieniu pewnego warunku zaktualizuje wiersz wstawiając nową datę. 

Mam następująca tabelę POZYCZKI:
[code]
 Column                 |     Type         |                               Modifiers                              
-----------------------+----------------+---------------------------------------------------------------------
 id_pozyczki            | integer          | not null PRIMARY KEY
 id_kontrahent         | integer          | not null
 data_pozyczki        | date              | not null
 ilosc_pozyczki        | numeric(6,2)   | not null
 splata_pozyczki      | numeric(6,2)  | default 0
 kierunek_pozyczki  | character(1)   | 
 data_splaty             | date              | [/code]

wypełniam ją insertem:

[code]insert into pozyczki (id_kontrahent, data_pozyczki, ilosc_pozyczki, kierunek_pozyczki) values (1, 2009-06-10, 400, 1);[/code]

następnie robie update:
[code]update pozyczki set splata_pozczyki = 400 where id_pozyczki = 1;[/code]

Chciałbym, żeby po każdym update'cie było sprawdzanie czy pola ilosc_pozyczki i splata_pozyczki są równe i jeśli tak to automatycznie wypełnia się pole data_splaty aktualną datą.
poniżej wkleje moje "próby":

[code]-- trigger uruchamiajacy  funkcje...
create trigger trig_splata 
after update on kasa_pozyczki
for each row 
execute procedure splata_trigger();[/code]

[code]-- funkcja..
create function splata_trigger() returns opaque AS' 
declare
--  aktualna_data date:= current_date;
BEGIN 
 if(old.ilosc_pozyczki = new.splata_pozyczki ) then
  --  NEW.data_splaty = now();
   update kasa_pozyczki set data_splaty = (select current_date);-- WHERE splata_pozyczki = NEW.splata_pozyczki;
  return NEW;
 else
  return NULL;
 end if;
 
END;
' language 'plpgsql';[/code]

I niestety to nie działa :/ pl/pgsql dopiero zaczynam proszę o wyrozumiałość;)
Bardzo proszę o pomoc. Z góry dziękuję]]></content>
			<author>
				<name><![CDATA[krzysko]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=925</uri>
			</author>
			<updated>2009-06-10T12:09:14Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=964#p964</id>
		</entry>
</feed>
