PostgreSQL

Скачать в pdf «PostgreSQL»
18.4 Triggers


Rules allow SQL queries to be executed when a table is accessed (see Section
13.6). Triggers offer an alternative way to perform actions on INSERT, UPDATE, or DELETE. They are ideal for checking or modifying a column value before it is added to the database.


Triggers and rules are implemented differently, however. Triggers call server-side functions for each modified row, whereas rules rewrite user queries or add queries. The former are ideal for checking or modifying a row before it is added to the database. The latter are ideal when the action affects other tables.


CREATE FUNCTION spread(text)


test=>


test->


test->


test’>


test’>


test’>


test’>


test’>


test’>


test’>


test’>


test’>


test’>


test’>


test’>


test’>


test’>


test’>


test’>


test->


create


test=>



m a j


(1 tow)


RETURNS text


AS ‘declare


str text; ret text;


1 integer; len integer;


begin


str := upper($1);


ret :=»»;    — start with zero length


1 := 1;


len := length(str);


WHILE 1 <= len LOOP


ret := ret || substr(str, 1, 1) || » »;


1 := 1 + 1;


END LOOP;


RETURN ret;


END;’


LANGUAGE ‘plpgsql’;


SELECT spread(‘Major Financial Report’); spread


OR FINANCIAL REPORT


test=


= >


CREATE FUNCTION getstatecode(text)


test


->


RETURNS text

Скачать в pdf «PostgreSQL»