psani funkce v pgsql

Karel Zak zakkr na zf.jcu.cz
Čtvrtek Listopad 21 09:28:07 CET 2002


On Wed, Nov 20, 2002 at 11:12:59PM +0100, Jiri Chaloupka wrote:
> Pokud panove dovolite, budu jeste chvili pokracovat s ptanim ...
> 
> takze jsem se posti jinym smerem.
> mam funkci a trigger:
> 
> create function default_phoneid_insells() returns opaque as '
> -- Funkce slouzi k prevodu linku na id z tabulky 
> --   pred pripadnym smazanim telefonu z databaze

 To je podivne definovana integrita, kdyz musite neco prevadet. Proc
 nepouzivate rovnou to id nebo jako primarni klic primo ten telefon
 a nasledne nejaky FOREIGN KEY.

>   BEGIN
> 
>     -- provedu vlastni prevod
>     update sells set phoneid = 0 where phoneid = OLD.id;
> 
>     return NEW;

 Co pro operaci DELETE obsahuje NEW? ...IMHO alespon v C psane funkci nic.

>   END;
> ' language 'plpgsql';
> 
> CREATE TRIGGER t_sells_changephid
>   BEFORE DELETE ON phones
>   FOR EACH ROW EXECUTE PROCEDURE default_phoneid_insells();

 A trigger function must return either NULL or a record/row value having
exactly the structure of the table the trigger was fired for. Triggers fired
BEFORE may return NULL to signal the trigger manager to skip the rest of the
                                                    ^^^^^^^^^^^^^^^^^^^^
operation for this row (ie, subsequent triggers are not fired, and the
INSERT/UPDATE/DELETE does not occur for this row). If a non-NULL value is
returned then the operation proceeds with that row value. Note that returning a
row value different from the original value of NEW alters the row that will be
inserted or updated. It is possible to replace single values directly in NEW
and return that, or to build a complete new record/row to return.

The return value of a trigger fired AFTER is ignored; it may as well always
return a NULL value. But an AFTER trigger can still abort the operation by
raising an error. 


 Pohal bych si s tim BEFORE/AFTER a RETURNem.

> funkce, mel se vykonat i inicializacni prikaz (delete) ...

 Ano i ne, viz dokumentace :-) 

    Karel

-- 
 Karel Zak  <zakkr na zf.jcu.cz>
 http://home.zf.jcu.cz/~zakkr/


Další informace o konferenci Databases