-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_fonctions_Plpgsql.sql
193 lines (163 loc) · 4.7 KB
/
01_fonctions_Plpgsql.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
---------------------------------------------------------------------------------
-- Projet : plpgslq
-- Objet du script : manipulation et proposition des fonctions
-- Auteur : Jéros VIGAN
-- Date de creation : 19/12/2017
-- Date de modification : 18/07/2023
---------------------------------------------------------------------------------
create or replace function fetcher (n integer)
returns void as $$
declare
exp record;
begin
for exp in select nom_auteur
from t_auteur
order by prenom_auteur
limit n
loop
raise notice '%',exp.nom_auteur ;
end loop;
end;
$$ language plpgsql;
select fetcher(10);
----------------------------------------------------------------------------------------------------------------------------------------------
CREATE OR RePLACe FUNCTION total()
returns integer as $total$
declare
total integer;
begin
select count(*)into total from t_auteur;
return total;
end;
$total$ language plpgsql;
select total();
----------------------------------------------------------------------------------------------------------------------------------------------
CREATE OR RePLACe FUNCTION mimax(a integer,b integer,c integer, out max integer, out min integer)
as $mm$
begin
max=greatest(a, b, c);
min=least(a,b,c);
end;
$mm$ language plpgsql;
select mimax(2,5,6);
----------------------------------------------------------------------------------------------------------------------------------------------
CREATE OR RePLACe FUNCTION mimax(a integer,b integer,c integer, out max integer, out min integer)
as $mm$
begin
max=greatest(a, b, c);
min=least(a,b,c);
end;
$mm$ language plpgsql;
select mimax(2,5,6);
----------------------------------------------------------------------------------------------------------------------------------------------
/* condiction if */
do $$
declare
num1 integer=10;
num2 integer=10;
begin
if num1>num2 then
raise notice 'num1 est superieur à num2';
end if;
if num1<num2 then
raise notice 'num1 est inferieur à num2';
end if;
if num1=num2 then
raise notice 'num1 est egal au num2';
end if ;
end$$
----------------------------------------------------------------------------------------------------------------------------------------------
do $$
declare
num1 integer=20;
num2 integer=60;
begin
if num1>num2 then
raise notice 'num1 est superieur à num2';
else
raise notice 'num1 est inferieur à num2';
end if;
end$$
----------------------------------------------------------------------------------------------------------------------------------------------
do $$
declare
num1 integer=50;
num2 integer=20;
begin
if num1>num2 then
raise notice 'num1 est superieur à num2';
elseif num1<num2 then
raise notice 'num1 est inferieur à num2';
else
raise notice 'num1 est egal au num2';
end if ;
end$$
----------------------------------------------------------------------------------------------------------------------------------------------
/* boucle */
create or replace function fib (n integer)
returns integer as $$
declare
counter integer=0;
i integer=0;
j integer=1;
begin
if (n<1) then
return 0;
end if;
loop
exit when counter=n;
counter =counter+1;
select j, j+i into i,j;
end loop;
return i;
end;
$$ language plpgsql;
select fib(6);
----------------------------------------------------------------------------------------------------------------------------------------------
create or replace function fib (n integer)
returns integer as $$
declare
counter integer=0;
i integer=0;
j integer=1;
begin
if (n<1) then
return 0;
end if;
while counter<n loop counter =counter+1;
select j, j+i into i,j;
end loop;
return i;
end;
$$ language plpgsql;
select fib(10);
----------------------------------------------------------------------------------------------------------------------------------------------
CREATE OR RePLACe FUNCTION req( var integer)
returns table(ident integer , nam varchar)
as $info$
begin
return query select num_auteur, nom_auteur from t_auteur
where num_auteur=var;
end;
$info$ language plpgsql;
select req(2);
----------------------------------------------------------------------------------------------------------------------------------------------
/*triggres */
create or replace function log_changer()
returns trigger as
$BODY$
begin
if NEW.adress<>OLD.adress then
insert into emp_log values(old.id, old.adress,new.adress,old.name);
end inf;
return new;
$BODY$
language plpgsql;
create trigger loc_changer
before update
on company
for each row
execute procedure log_change();
select * from company;
update company set adress ='dehli' where id =4;
update company set adress ='dehli' where id =4;