-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPL_Postgresql.sql
More file actions
96 lines (67 loc) · 3.13 KB
/
PL_Postgresql.sql
File metadata and controls
96 lines (67 loc) · 3.13 KB
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
--PostgreSQL PL/pgSQL
/*PL/pgSQL procedural language adds many procedural elements, e.g., control structures, loops, and complex computations, to extend standard SQL.
It allows you to develop complex functions and stored procedures in PostgreSQL that may not be possible using plain SQL.*/
/*PL/pgSQL procedural language adds many procedural elements, e.g., control structures, loops, and complex computations, to extend standard SQL.
It allows you to develop complex functions and stored procedures in PostgreSQL that may not be possible using plain SQL.*/
--PL/pgSQL Block Structure
--PL/pgSQL is a block-structured language, therefore, a PL/pgSQL function or stored procedure is organized into blocks.
--The following illustrates the syntax of a complete block in PL/pgSQL:
[ <<label>> ]
[ declare
declarations ]
begin
statements;
...
end [ label ];
/* Let’s examine the block structure in more detail:
Each block has two sections: declaration and body. The declaration section is optional while the body section is required.
A block is ended with a semicolon (;) after the END keyword.
A block may have an optional label located at the beginning and at the end.
You use the block label when you want to specify it in the EXIT statement of the block body or when you want to qualify the names of variables declared in the block.
The declaration section is where you declare all variables used within the body section. Each statement in the declaration section is terminated with a semicolon (;).
The body section is where you place the code. Each statement in the body section is also terminated with a semicolon (;). */
--PL/pgSQL block structure example
--The following example illustrates a very simple block. It is called an anonymous block.
do $$
<<first_block>>
declare
film_count integer := 0;
begin
-- get the number of films
select count(*)
into film_count
from film;
-- display a message
raise notice 'The number of films is %', film_count;
end first_block $$;
--Notice that the DO statement does not belong to the block. It is used to execute an anonymous block. PostgreSQL introduced the DO statement since version 9.0.
--The anonymous block has to be surrounded in single quotes like this:
'<<first_block>>
declare
film_count integer := 0;
begin
-- get the number of films
select count(*)
into film_count
from film;
-- display a message
raise notice ''The number of films is %'', film_count;
end first_block';
--we can use the dollar-quoted string constant syntax to make it more readable.
--The following example illustrates how to declare and initialize variables:
do $$
declare
counter integer := 1;
first_name varchar(50) := 'John';
last_name varchar(50) := 'Doe';
payment numeric(11,2) := 20.5;
begin
raise notice '% % % has been paid % USD',
counter,
first_name,
last_name,
payment;
end $$;
/* PL/pgSQL Subblocks
PL/pgSQL allows you to place a block inside the body of another block.
The block nested inside another block is called a subblock. The block that contains the subblock is referred to as an outer block.*/