Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create function_trigger.md #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions function_trigger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
Title: Mise à jour automatique
Favicon: logo.svg
Sibling: yes
...

[TOC]

## Mise à jour automatique de champs

Création de la fonction trigger de mise à jour du champs longueur
```sql
- longueur_geom()
CREATE FUNCTION z_formation.longueur_geom() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
BEGIN
NEW.longueur = ST_Length(NEW.geom);

RETURN NEW;
END;
$$;
```

Utilisation du trigger de mise à jour du champs longueur avant chaque insert
```sql
-- voie trigger_insert
CREATE TRIGGER trigger_insertz_formation_chemin_longueur_geom
BEFORE INSERT ON z_formation.chemin
FOR EACH ROW
EXECUTE PROCEDURE z_formation.longueur_geom();
```

Utilisation du trigger de mise à jour du champs longueur avant chaque update si le champs geom ou longueur ont été modifié
```sql
-- voie trigger_update
CREATE TRIGGER trigger_update_z_formation_chemin_longueur_geom
BEFORE UPDATE ON z_formation.chemin
FOR EACH ROW
WHEN (OLD.geom IS DISTINCT FROM NEW.geom OR OLD.longueur IS DISTINCT FROM NEW.longueur)
EXECUTE PROCEDURE z_formation.longueur_geom();
```