SlideShare a Scribd company logo
1 of 58
• PL/pgSQL Variables
• PL/pgSQL Constants
• PostgreSQL data types
Controlling the program flow, conditional statements, loops
P L / P G S Q L VA R I A B L E S
• A PL/pgSQL variable is a meaningful name for a memory location. A
variable holds a value that can be changed through the block or function. A
variable is always associated with a particular data type.
• You must declare it in the declaration section of the PL/pgSQL block before
using.
variable_name data_type[:=expression];
Counter integer := 0;
variable_name data_type[:=expression];
• In this syntax:
• First, specify the name of the variable. It is a good practice to assign a
meaningful name to a variable. For example, instead of naming a
variable i you should use index or counter.
• Second, associate a specific data type with the variable. The data type
can be any valid PostgreSQL data type such
as INTEGER, NUMERIC, VARCHAR and CHAR.
• Third, optionally assign a default value to a variable. If you don’t, the
initial value of the variable is initialized to NULL.
• Notice that PostgreSQL evaluates the default values and set them for
the variables when the block is entered. Exa.
DO $$
DECLARE
cnt NUMERIC:=1;
first_name VARCHAR(50):='John';
last_name VARCHAR(50):='Doe';
payment NUMERIC(11,2):=20.5;
BEGIN
RAISE NOTICE'% % % has been paid % USD’, cnt,first_name,last_name,
payment;
END; $$
Note that you can use either := or = assignment operator to initialize and assign a value to a variable.
P L / P G S Q L C O N S TA N T S
• The values of constants cannot be changed once they are initialized.
• The following are reasons to use constants.
• First, the constants make the code more readable e.g., imagine that we have a
formula as follows:
• selling_price=net_price+net_price*0.1;
• What does 0.1 means? It can be interpreted to anything. But, when we use the
following formula, we know the meaning of the calculation of the selling price that
equals to net price plus value-added tax (VAT).
• selling_price=net_price+net_price*VAT;
• Second, the constants reduce the maintenance efforts. constants example
D E C L A R I N G C O N S TA N T S Y N TA X
To declare a constant in PL/pgSQL, you use the following syntax:
• constant_name CONSTANT data_type:=expression;
• PI CONSTANT float =3.14;
Let’s examine each component of the syntax in more detail:
• First, specify the constant name. By convention, it is in the uppercase form
such as VAT and DISCOUNT.
• Second, put the CONSTANT keyword and specify the data type that the
constant is associated with.
• Third, initialize a value for the constant.
P L / P G S Q L S E L E C T I N T O
• We can use PL/pgSQL SELECT INTO statement to fetch data of a single row from a
table into variables.
SELECT
select_list
INTO
variable_list
FROM
table_name
WHERE
condition;
The number of columns in the variable_list must be the same as the number of variables in
the select_list. In addition, their corresponding data type must be compatible.
DO $$
DECLARE
actorf_name varchar(10);
actorl_name varchar(10);
BEGIN
select actfname,actlname
into actorf_name,actorl_name
from actor
where actid=102;
RAISE NOTICE 'The actors details: %%', actorf_name,actorl_name;
END; $$
Copying data types
• PL/pgSQL enables you to define a variable with a data type that
references to the data type of a column of a table or the data type
of another variable using %type:
• The %type provides the data type of a table column or another
variable.
variable_name table_name.column_name%TYPE;
Fname student.std_Fname%TYPE;
variable_name variable%TYPE;
Lname Fneme%TYPE;
Assigning aliases to variables
• PostgreSQL allows you to define an alias for any
variable as follows:
• new_name ALIAS FOR old_name;
• user_name ALIAS FOR f_name;
P L / P G S Q L R O W T Y P E S ( % R O W T Y P E )
• To store the whole row of a result set returned by the ’select into’ statement,
you use the row-type variable or row variable.
• You can declare a variable that has the same datatype as the datatype of the
row in a table by using the following syntax:
row_variable table_name%ROWTYPE;
row_variable view_name%ROWTYPE;
do $$
declare
selected_actor actor%rowtype;
begin
select *
from actor
into selected_actor
where actor_id = 10;
raise notice 'The actor name is % %',
selected_actor.first_name,
selected_actor.last_name;
end; $$
P L / P G S Q L R E C O R D T Y P E S
• PostgreSQL uses record type variables which simply act as placeholders for
rows of a result set.
• To declare a record variable, you use a variable name followed by
the record keyword like this:
variable_name record;
• Unlike a row-type variable, a record variable does not have a predefined
structure. The structure of a record variable is determined when
the select or for statement assigns an actual row to it.
1) Using record with the select into statement
2) Using record variables in the for loop statement
T Y P E S
• PostgreSQL supports the following data types:
• Boolean
• Character types such as char, varchar, and text.
• Numeric types such as integer and floating-point number.
• Temporal types such as date, time, timestamp, and interval
• UUID for storing Universally Unique Identifiers
• Array for storing array strings, numbers, etc.
• JSON stores JSON data
• hstore stores key-value pair
• Special types such as network address and geometric data.
N U M E R I C ( P R E C I S I O N , S C A L E )
• In this syntax, the precision is the total number of digits and the scale is the
number of digits in the fraction part. For example, the number 1234.567 has the
precision 7 and scale 3.
• The NUMERIC type can hold a value up to 131,072 digits before the decimal
point 16,383 digits after the decimal point.
• The scale of the NUMERIC type can be zero or positive. The following shows the
syntax of NUMERIC type with scale zero:
• NUMERIC(precision)
• If you omit both precision and scale, you can store any precision and
scale up to the limit of the precision and scale mentioned above.
NUMERIC
If precision is not required, you should not use the NUMERIC type because
calculations on NUMERIC values are typically slower than integers, floats,
and double precisions.
B O O L E A N
• A Boolean data type can hold one of three possible values: true, false
or null.
• You use boolean or bool keyword to declare a column with the Boolean
data type.
When you insert data into a Boolean column, PostgreSQL converts it to
a Boolean value
• 1, yes, y, t, true values are converted to true
• 0, no, false, f values are converted to false.
• When you select data from a Boolean column, PostgreSQL converts
the values back e.g., t to true, f to false and space to null.
C H A R A C T E R
• PostgreSQL provides three character data types: CHAR(n), VARCHAR(n),
and TEXT
• CHAR(n) is the fixed-length character with space padded. If you insert a
string that is shorter than the length of the column, PostgreSQL pads
spaces. If you insert a string that is longer than the length of the column,
PostgreSQL will issue an error.
• VARCHAR(n) is the variable-length character
string. With VARCHAR(n), you can store up to n characters. PostgreSQL
does not pad spaces when the stored string is shorter than the length of the
column.
• TEXT is the variable-length character string. Theoretically, text data is a
character string with unlimited length.
P L / P G S Q L A S S E R T S TAT E M E N T
• The assert statement is a useful shorthand for inserting debugging checks into PL/pgSQL
code.
• The following illustrates the syntax of the assert statement:
assert condition [, message];
1) condition
• The condition is a Boolean expression that is expected to always return true.
• If the condition evaluates to true, the assert statement does nothing.
• In case the condition evaluates to false or null, PostgreSQL raises
an assert_failure exception.
P L / P G S Q L A S S E R T S TAT E M E N T
2) message
• The message is optional.
• If you don’t pass the message, PostgreSQL uses the “assertion failed”
message by default. In case you pass the message to the assert statement,
it will use it to replace the default message.
• Note that you should use the assert statement solely for detecting bugs, not
for reporting.
• To report a message or an error, you use the raise statement instead.
3 / 9 / 2 0 2 3 28
3 / 9 / 2 0 2 3 29
do $$
declare
film_count integer;
begin
select count(*)
into film_count
from film;
assert film_count > 0, 'Film not found, check the
film table';
end$$;
R E P O R T I N G M E S S A G E S
• To raise a message, you use the raise statement as follows:
raise level format;
1. Level
PostgreSQL provides the following levels:
• debug
• log
• notice
• info
• warning
• exception
3 / 9 / 2 0 2 3 30
2. Format
• The format is a string that specifies the message. The format uses percentage ( %)
placeholders that will be substituted by the arguments.
do $$
begin
raise info 'information message %', now();
raise log 'log message %', now();
raise debug 'debug message %', now();
raise warning 'warning message %', now();
raise notice 'notice message %', now();
end $$;
3 / 9 / 2 0 2 3 31
Raising errors
• To raise an error, you use the exception level after the raise statement. Note
that raise statement uses the exception level by default.
do $$
declare
email varchar(255) := 'info@postgresqltutorial.com';
begin
-- check email for duplicate
-- ...
-- report duplicate email
raise exception 'duplicate email: %', email
using hint = 'check the email again';
end $$;
3 / 9 / 2 0 2 3 32
1.3 Controlling the program flow,
conditional statements, loops
3/9/2023 33
PL/pgSQL IF Statement
• The IF statement is used to execute a command conditionally. PL/pgSQL
provides you with three forms of the IF statements.
The simplest form of PL/pgSQL IF statement
• The IF statement executes statements if a conditionis true. If the condition
evaluates to false, the control is passed to the next statement after
the END IF part. The following illustrates the simplest form of
the IF statement:
If condition THEN
statement;
END IF;
The condition is a boolean expression that evaluates to true or f
alse.
DO $$
DECLARE a integer := 10;
b integer := 20;
BEGIN
IF a > b THEN
RAISE NOTICE 'a is greater than b';
END IF;
IF a < b THEN
RAISE NOTICE 'a is less than b';
END IF;
IF a = b THEN
RAISE NOTICE 'a is equal to b';
END IF;
END $$;
PL/pgSQL IF THEN ELSE statement
• The IF THEN ELSE statement executes a command when the
condition is true and it executes an alternative command when
the condition is false. The following illustrates the syntax of
the IF THEN ELSE statement:
IF condition THEN
statements;
ELSE
alternative-statements;
END IF;
DO $$
DECLARE
a integer:=10;
b integer:=20;
BEGIN
IF a>b THEN
RAISE NOTICE 'a is greater than b';
ELSE
RAISE NOTICE 'a is not greater than b';
END IF;
END $$;
PL/pgSQL IF THEN ELSIF THEN ELSE statement
IFcondition-1THEN
if-statement;
ELSIFcondition-2THEN
elsif-statement-2
...
ELSIFcondition-nTHEN
elsif-statement-n;
ELSE
else-statement;
END IF;
DO $$
DECLARE
a integer := 10;
b integer := 10;
BEGIN
IF a > b THEN
RAISE NOTICE 'a is greater than b';
ELSIF a < b THEN
RAISE NOTICE 'a is less than b';
ELSE
RAISE NOTICE 'a is equal to b';
END IF;
END $$;
PL/pgSQL CASE Statement
• Besides the IF statement, PostgreSQL provides you
with CASE statements that allow you to execute a block of code
conditionally.
• There are two forms of the CASE statement:
• simple
• searched CASE statements.
Simple CASE
CASE search-expression
WHEN expression [, expression [ ... ]] THEN statements [
WHEN expression [, expression [ ... ]] THEN statements ... ]
[ ELSE statements ]
END CASE;
3/9/2023 44
CREATE FUNCTION case1 (val integer)
RETURNS text
LANGUAGE PLPGSQL
AS
$$
BEGIN
CASE val
WHEN 1 THEN
RETURN ‘ one’;
WHEN 2 THEN
RETURN ' two';
WHEN 3 THEN
RETURN ‘ three';
ELSE
RETURN 'other value than 1,2,3';
END CASE;
END; $$;
3/9/2023 45
select get_price_segment(121)
46
Searched CASE
• The searched form of CASE provides conditional execution
based on truth of Boolean expressions.
• Each WHEN clause's boolean-expression is evaluated in turn,
until one is found that yields true.
• Then the corresponding statements are executed, and then
control passes to the next statement after END CASE.
search_ex
a
CASE
WHEN x BETWEEN 0 AND 10 THEN
msg := 'value is between zero and ten';
WHEN x BETWEEN 11 AND 20 THEN
msg := 'value is between eleven and twenty';
END CASE;
PL/pgSQL LOOP statement
Sometimes, you need to execute a block of statements
repeatedly until a condition becomes true.
To do this, you use the PL/pgSQL LOOP statement.
[<<label>>]
LOOP
Statements;
EXIT [<<label>>] WHEN condition;
END LOOP;
3/9/2023 50
CREATE OR REPLACE FUNCTION fibonacci(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,i+j INTO i,j;
END LOOP;
RETURN i;
END; $$ LANGUAGE plpgsql;
3 / 9 / 2 0 2 3 51
P L / P G S Q L W H I L E L O O P
• The WHILE loop statement executes a block of statements until a
condition evaluates to false.
• In the WHILE loop statement, PostgreSQL evaluates the condition before
executing the block of statements.
• If the condition is true, the block of statements is executed until it is
evaluated to false.
• The following flowchart illustrates the WHILE loop statement.
The following is the syntax of the WHILE loop statement.
[<<label>>]
WHILE condition LOOP
statements;
END LOOP;
CREATE OR REPLACE FUNCTION fibonacci(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,i+j INTO i, j;
END LOOP;
RETURN i;
END; $$
F O R L O O P
The following code uses the for loop statement to iterate over largest 10 employee id:
do
$$
declare
f record;
begin
for f in select employee_id, full_name
from employees
order by employee_id desc, full_name
limit 10
loop
raise notice '% % ', f.employee_id, f.full_name;
end loop;
end;
$$;
do $$
declare
sort_type smallint := 1;
rec_count int := 10;
rec record;
query text;
begin
query := 'select full_name, employee_id from employees
';
if sort_type = 1 then
query := query || 'order by employee_id desc ';
elsif sort_type = 2 then
query := query || 'order by length(full_name) desc ';
else
raise 'invalid sort type %s', sort_type;
end if;
query := query || ' limit $1';
for rec in execute query using rec_count
loop
raise notice '% %', rec.employee_id, rec.full_name;
end loop;
end;

More Related Content

What's hot

Troubleshooting Tips and Tricks for Database 19c - Sangam 2019
Troubleshooting Tips and Tricks for Database 19c - Sangam 2019Troubleshooting Tips and Tricks for Database 19c - Sangam 2019
Troubleshooting Tips and Tricks for Database 19c - Sangam 2019Sandesh Rao
 
Hadoop hive presentation
Hadoop hive presentationHadoop hive presentation
Hadoop hive presentationArvind Kumar
 
Apache Spark Introduction
Apache Spark IntroductionApache Spark Introduction
Apache Spark Introductionsudhakara st
 
OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...
OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...
OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...Christopher Frohoff
 
Cloud Native PostgreSQL
Cloud Native PostgreSQLCloud Native PostgreSQL
Cloud Native PostgreSQLEDB
 
Introduction to PySpark
Introduction to PySparkIntroduction to PySpark
Introduction to PySparkRussell Jurney
 
Spark introduction and architecture
Spark introduction and architectureSpark introduction and architecture
Spark introduction and architectureSohil Jain
 
Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...
Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...
Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...Edureka!
 
SQLcl overview - A new Command Line Interface for Oracle Database
SQLcl overview - A new Command Line Interface for Oracle DatabaseSQLcl overview - A new Command Line Interface for Oracle Database
SQLcl overview - A new Command Line Interface for Oracle DatabaseJeff Smith
 
Introduction to spark
Introduction to sparkIntroduction to spark
Introduction to sparkHome
 
Apache Spark overview
Apache Spark overviewApache Spark overview
Apache Spark overviewDataArt
 
Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...
Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...
Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...Simplilearn
 
Oracle Exadata Exam Dump
Oracle Exadata Exam DumpOracle Exadata Exam Dump
Oracle Exadata Exam DumpPooja C
 
Histograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLHistograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLSergey Petrunya
 

What's hot (20)

Troubleshooting Tips and Tricks for Database 19c - Sangam 2019
Troubleshooting Tips and Tricks for Database 19c - Sangam 2019Troubleshooting Tips and Tricks for Database 19c - Sangam 2019
Troubleshooting Tips and Tricks for Database 19c - Sangam 2019
 
Hadoop hive presentation
Hadoop hive presentationHadoop hive presentation
Hadoop hive presentation
 
Hive(ppt)
Hive(ppt)Hive(ppt)
Hive(ppt)
 
Apache Spark Introduction
Apache Spark IntroductionApache Spark Introduction
Apache Spark Introduction
 
OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...
OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...
OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...
 
SQOOP PPT
SQOOP PPTSQOOP PPT
SQOOP PPT
 
Cloud Native PostgreSQL
Cloud Native PostgreSQLCloud Native PostgreSQL
Cloud Native PostgreSQL
 
Sequelize
SequelizeSequelize
Sequelize
 
Introduction to PySpark
Introduction to PySparkIntroduction to PySpark
Introduction to PySpark
 
Spark introduction and architecture
Spark introduction and architectureSpark introduction and architecture
Spark introduction and architecture
 
Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...
Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...
Apache Sqoop Tutorial | Sqoop: Import & Export Data From MySQL To HDFS | Hado...
 
SQLcl overview - A new Command Line Interface for Oracle Database
SQLcl overview - A new Command Line Interface for Oracle DatabaseSQLcl overview - A new Command Line Interface for Oracle Database
SQLcl overview - A new Command Line Interface for Oracle Database
 
Introduction to spark
Introduction to sparkIntroduction to spark
Introduction to spark
 
Apache Spark overview
Apache Spark overviewApache Spark overview
Apache Spark overview
 
Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...
Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...
Hive Tutorial | Hive Architecture | Hive Tutorial For Beginners | Hive In Had...
 
Apache spark
Apache sparkApache spark
Apache spark
 
Spark
SparkSpark
Spark
 
Oracle Exadata Exam Dump
Oracle Exadata Exam DumpOracle Exadata Exam Dump
Oracle Exadata Exam Dump
 
Postgresql
PostgresqlPostgresql
Postgresql
 
Histograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLHistograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQL
 

Similar to PLPgSqL- Datatypes, Language structure.pptx

L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdfL2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdfMMRF2
 
Open Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and typesDaman Toor
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data TypesTareq Hasan
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical ExpressionsUnit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressionsdubon07
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3Vince Vo
 
Variables and calculations_chpt_4
Variables and calculations_chpt_4Variables and calculations_chpt_4
Variables and calculations_chpt_4cmontanez
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersANUSUYA S
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfYRABHI
 

Similar to PLPgSqL- Datatypes, Language structure.pptx (20)

Java chapter 2
Java chapter 2Java chapter 2
Java chapter 2
 
lec 2.pptx
lec 2.pptxlec 2.pptx
lec 2.pptx
 
Pl sql
Pl sqlPl sql
Pl sql
 
Pl sql
Pl sqlPl sql
Pl sql
 
Pl sql
Pl sqlPl sql
Pl sql
 
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdfL2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
 
Open Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul Language PL/SQL
Open Gurukul Language PL/SQL
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
 
Data structure
Data structureData structure
Data structure
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 
Data Handling
Data HandlingData Handling
Data Handling
 
C++ data types
C++ data typesC++ data types
C++ data types
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
PL_SQL - II.pptx
PL_SQL - II.pptxPL_SQL - II.pptx
PL_SQL - II.pptx
 
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical ExpressionsUnit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
Unit 4 - Basic ABAP statements, ABAP Structures and ABAP Logical Expressions
 
JAVA LESSON-01.pptx
JAVA LESSON-01.pptxJAVA LESSON-01.pptx
JAVA LESSON-01.pptx
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
 
Variables and calculations_chpt_4
Variables and calculations_chpt_4Variables and calculations_chpt_4
Variables and calculations_chpt_4
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about PointersC++ - UNIT_-_IV.pptx which contains details about Pointers
C++ - UNIT_-_IV.pptx which contains details about Pointers
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdf
 

Recently uploaded

What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 

Recently uploaded (20)

What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 

PLPgSqL- Datatypes, Language structure.pptx

  • 1. • PL/pgSQL Variables • PL/pgSQL Constants • PostgreSQL data types Controlling the program flow, conditional statements, loops
  • 2. P L / P G S Q L VA R I A B L E S • A PL/pgSQL variable is a meaningful name for a memory location. A variable holds a value that can be changed through the block or function. A variable is always associated with a particular data type. • You must declare it in the declaration section of the PL/pgSQL block before using. variable_name data_type[:=expression]; Counter integer := 0;
  • 3. variable_name data_type[:=expression]; • In this syntax: • First, specify the name of the variable. It is a good practice to assign a meaningful name to a variable. For example, instead of naming a variable i you should use index or counter. • Second, associate a specific data type with the variable. The data type can be any valid PostgreSQL data type such as INTEGER, NUMERIC, VARCHAR and CHAR. • Third, optionally assign a default value to a variable. If you don’t, the initial value of the variable is initialized to NULL. • Notice that PostgreSQL evaluates the default values and set them for the variables when the block is entered. Exa.
  • 4. DO $$ DECLARE cnt NUMERIC:=1; first_name VARCHAR(50):='John'; last_name VARCHAR(50):='Doe'; payment NUMERIC(11,2):=20.5; BEGIN RAISE NOTICE'% % % has been paid % USD’, cnt,first_name,last_name, payment; END; $$ Note that you can use either := or = assignment operator to initialize and assign a value to a variable.
  • 5.
  • 6. P L / P G S Q L C O N S TA N T S • The values of constants cannot be changed once they are initialized. • The following are reasons to use constants. • First, the constants make the code more readable e.g., imagine that we have a formula as follows: • selling_price=net_price+net_price*0.1; • What does 0.1 means? It can be interpreted to anything. But, when we use the following formula, we know the meaning of the calculation of the selling price that equals to net price plus value-added tax (VAT). • selling_price=net_price+net_price*VAT; • Second, the constants reduce the maintenance efforts. constants example
  • 7. D E C L A R I N G C O N S TA N T S Y N TA X To declare a constant in PL/pgSQL, you use the following syntax: • constant_name CONSTANT data_type:=expression; • PI CONSTANT float =3.14; Let’s examine each component of the syntax in more detail: • First, specify the constant name. By convention, it is in the uppercase form such as VAT and DISCOUNT. • Second, put the CONSTANT keyword and specify the data type that the constant is associated with. • Third, initialize a value for the constant.
  • 8. P L / P G S Q L S E L E C T I N T O • We can use PL/pgSQL SELECT INTO statement to fetch data of a single row from a table into variables. SELECT select_list INTO variable_list FROM table_name WHERE condition; The number of columns in the variable_list must be the same as the number of variables in the select_list. In addition, their corresponding data type must be compatible.
  • 9. DO $$ DECLARE actorf_name varchar(10); actorl_name varchar(10); BEGIN select actfname,actlname into actorf_name,actorl_name from actor where actid=102; RAISE NOTICE 'The actors details: %%', actorf_name,actorl_name; END; $$
  • 10. Copying data types • PL/pgSQL enables you to define a variable with a data type that references to the data type of a column of a table or the data type of another variable using %type: • The %type provides the data type of a table column or another variable. variable_name table_name.column_name%TYPE; Fname student.std_Fname%TYPE; variable_name variable%TYPE; Lname Fneme%TYPE;
  • 11. Assigning aliases to variables • PostgreSQL allows you to define an alias for any variable as follows: • new_name ALIAS FOR old_name; • user_name ALIAS FOR f_name;
  • 12. P L / P G S Q L R O W T Y P E S ( % R O W T Y P E ) • To store the whole row of a result set returned by the ’select into’ statement, you use the row-type variable or row variable. • You can declare a variable that has the same datatype as the datatype of the row in a table by using the following syntax: row_variable table_name%ROWTYPE; row_variable view_name%ROWTYPE;
  • 13. do $$ declare selected_actor actor%rowtype; begin select * from actor into selected_actor where actor_id = 10; raise notice 'The actor name is % %', selected_actor.first_name, selected_actor.last_name; end; $$
  • 14. P L / P G S Q L R E C O R D T Y P E S • PostgreSQL uses record type variables which simply act as placeholders for rows of a result set. • To declare a record variable, you use a variable name followed by the record keyword like this: variable_name record; • Unlike a row-type variable, a record variable does not have a predefined structure. The structure of a record variable is determined when the select or for statement assigns an actual row to it.
  • 15. 1) Using record with the select into statement
  • 16. 2) Using record variables in the for loop statement
  • 17. T Y P E S • PostgreSQL supports the following data types: • Boolean • Character types such as char, varchar, and text. • Numeric types such as integer and floating-point number. • Temporal types such as date, time, timestamp, and interval • UUID for storing Universally Unique Identifiers • Array for storing array strings, numbers, etc. • JSON stores JSON data • hstore stores key-value pair • Special types such as network address and geometric data.
  • 18.
  • 19.
  • 20. N U M E R I C ( P R E C I S I O N , S C A L E ) • In this syntax, the precision is the total number of digits and the scale is the number of digits in the fraction part. For example, the number 1234.567 has the precision 7 and scale 3. • The NUMERIC type can hold a value up to 131,072 digits before the decimal point 16,383 digits after the decimal point. • The scale of the NUMERIC type can be zero or positive. The following shows the syntax of NUMERIC type with scale zero: • NUMERIC(precision)
  • 21. • If you omit both precision and scale, you can store any precision and scale up to the limit of the precision and scale mentioned above. NUMERIC If precision is not required, you should not use the NUMERIC type because calculations on NUMERIC values are typically slower than integers, floats, and double precisions.
  • 22. B O O L E A N • A Boolean data type can hold one of three possible values: true, false or null. • You use boolean or bool keyword to declare a column with the Boolean data type. When you insert data into a Boolean column, PostgreSQL converts it to a Boolean value • 1, yes, y, t, true values are converted to true • 0, no, false, f values are converted to false. • When you select data from a Boolean column, PostgreSQL converts the values back e.g., t to true, f to false and space to null.
  • 23.
  • 24. C H A R A C T E R • PostgreSQL provides three character data types: CHAR(n), VARCHAR(n), and TEXT • CHAR(n) is the fixed-length character with space padded. If you insert a string that is shorter than the length of the column, PostgreSQL pads spaces. If you insert a string that is longer than the length of the column, PostgreSQL will issue an error. • VARCHAR(n) is the variable-length character string. With VARCHAR(n), you can store up to n characters. PostgreSQL does not pad spaces when the stored string is shorter than the length of the column. • TEXT is the variable-length character string. Theoretically, text data is a character string with unlimited length.
  • 25. P L / P G S Q L A S S E R T S TAT E M E N T • The assert statement is a useful shorthand for inserting debugging checks into PL/pgSQL code. • The following illustrates the syntax of the assert statement: assert condition [, message]; 1) condition • The condition is a Boolean expression that is expected to always return true. • If the condition evaluates to true, the assert statement does nothing. • In case the condition evaluates to false or null, PostgreSQL raises an assert_failure exception.
  • 26. P L / P G S Q L A S S E R T S TAT E M E N T 2) message • The message is optional. • If you don’t pass the message, PostgreSQL uses the “assertion failed” message by default. In case you pass the message to the assert statement, it will use it to replace the default message. • Note that you should use the assert statement solely for detecting bugs, not for reporting. • To report a message or an error, you use the raise statement instead. 3 / 9 / 2 0 2 3 28
  • 27. 3 / 9 / 2 0 2 3 29 do $$ declare film_count integer; begin select count(*) into film_count from film; assert film_count > 0, 'Film not found, check the film table'; end$$;
  • 28. R E P O R T I N G M E S S A G E S • To raise a message, you use the raise statement as follows: raise level format; 1. Level PostgreSQL provides the following levels: • debug • log • notice • info • warning • exception 3 / 9 / 2 0 2 3 30
  • 29. 2. Format • The format is a string that specifies the message. The format uses percentage ( %) placeholders that will be substituted by the arguments. do $$ begin raise info 'information message %', now(); raise log 'log message %', now(); raise debug 'debug message %', now(); raise warning 'warning message %', now(); raise notice 'notice message %', now(); end $$; 3 / 9 / 2 0 2 3 31
  • 30. Raising errors • To raise an error, you use the exception level after the raise statement. Note that raise statement uses the exception level by default. do $$ declare email varchar(255) := 'info@postgresqltutorial.com'; begin -- check email for duplicate -- ... -- report duplicate email raise exception 'duplicate email: %', email using hint = 'check the email again'; end $$; 3 / 9 / 2 0 2 3 32
  • 31. 1.3 Controlling the program flow, conditional statements, loops 3/9/2023 33
  • 32. PL/pgSQL IF Statement • The IF statement is used to execute a command conditionally. PL/pgSQL provides you with three forms of the IF statements. The simplest form of PL/pgSQL IF statement • The IF statement executes statements if a conditionis true. If the condition evaluates to false, the control is passed to the next statement after the END IF part. The following illustrates the simplest form of the IF statement: If condition THEN statement; END IF; The condition is a boolean expression that evaluates to true or f alse.
  • 33.
  • 34. DO $$ DECLARE a integer := 10; b integer := 20; BEGIN IF a > b THEN RAISE NOTICE 'a is greater than b'; END IF; IF a < b THEN RAISE NOTICE 'a is less than b'; END IF; IF a = b THEN RAISE NOTICE 'a is equal to b'; END IF; END $$;
  • 35. PL/pgSQL IF THEN ELSE statement • The IF THEN ELSE statement executes a command when the condition is true and it executes an alternative command when the condition is false. The following illustrates the syntax of the IF THEN ELSE statement: IF condition THEN statements; ELSE alternative-statements; END IF;
  • 36.
  • 37. DO $$ DECLARE a integer:=10; b integer:=20; BEGIN IF a>b THEN RAISE NOTICE 'a is greater than b'; ELSE RAISE NOTICE 'a is not greater than b'; END IF; END $$;
  • 38. PL/pgSQL IF THEN ELSIF THEN ELSE statement IFcondition-1THEN if-statement; ELSIFcondition-2THEN elsif-statement-2 ... ELSIFcondition-nTHEN elsif-statement-n; ELSE else-statement; END IF;
  • 39.
  • 40. DO $$ DECLARE a integer := 10; b integer := 10; BEGIN IF a > b THEN RAISE NOTICE 'a is greater than b'; ELSIF a < b THEN RAISE NOTICE 'a is less than b'; ELSE RAISE NOTICE 'a is equal to b'; END IF; END $$;
  • 41. PL/pgSQL CASE Statement • Besides the IF statement, PostgreSQL provides you with CASE statements that allow you to execute a block of code conditionally. • There are two forms of the CASE statement: • simple • searched CASE statements.
  • 42. Simple CASE CASE search-expression WHEN expression [, expression [ ... ]] THEN statements [ WHEN expression [, expression [ ... ]] THEN statements ... ] [ ELSE statements ] END CASE; 3/9/2023 44
  • 43. CREATE FUNCTION case1 (val integer) RETURNS text LANGUAGE PLPGSQL AS $$ BEGIN CASE val WHEN 1 THEN RETURN ‘ one’; WHEN 2 THEN RETURN ' two'; WHEN 3 THEN RETURN ‘ three'; ELSE RETURN 'other value than 1,2,3'; END CASE; END; $$; 3/9/2023 45
  • 45. Searched CASE • The searched form of CASE provides conditional execution based on truth of Boolean expressions. • Each WHEN clause's boolean-expression is evaluated in turn, until one is found that yields true. • Then the corresponding statements are executed, and then control passes to the next statement after END CASE.
  • 47. CASE WHEN x BETWEEN 0 AND 10 THEN msg := 'value is between zero and ten'; WHEN x BETWEEN 11 AND 20 THEN msg := 'value is between eleven and twenty'; END CASE;
  • 48. PL/pgSQL LOOP statement Sometimes, you need to execute a block of statements repeatedly until a condition becomes true. To do this, you use the PL/pgSQL LOOP statement. [<<label>>] LOOP Statements; EXIT [<<label>>] WHEN condition; END LOOP; 3/9/2023 50
  • 49. CREATE OR REPLACE FUNCTION fibonacci(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,i+j INTO i,j; END LOOP; RETURN i; END; $$ LANGUAGE plpgsql; 3 / 9 / 2 0 2 3 51
  • 50. P L / P G S Q L W H I L E L O O P • The WHILE loop statement executes a block of statements until a condition evaluates to false. • In the WHILE loop statement, PostgreSQL evaluates the condition before executing the block of statements. • If the condition is true, the block of statements is executed until it is evaluated to false. • The following flowchart illustrates the WHILE loop statement.
  • 51.
  • 52. The following is the syntax of the WHILE loop statement. [<<label>>] WHILE condition LOOP statements; END LOOP;
  • 53. CREATE OR REPLACE FUNCTION fibonacci(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,i+j INTO i, j; END LOOP; RETURN i; END; $$
  • 54. F O R L O O P
  • 55.
  • 56. The following code uses the for loop statement to iterate over largest 10 employee id: do $$ declare f record; begin for f in select employee_id, full_name from employees order by employee_id desc, full_name limit 10 loop raise notice '% % ', f.employee_id, f.full_name; end loop; end; $$;
  • 57.
  • 58. do $$ declare sort_type smallint := 1; rec_count int := 10; rec record; query text; begin query := 'select full_name, employee_id from employees '; if sort_type = 1 then query := query || 'order by employee_id desc '; elsif sort_type = 2 then query := query || 'order by length(full_name) desc '; else raise 'invalid sort type %s', sort_type; end if; query := query || ' limit $1'; for rec in execute query using rec_count loop raise notice '% %', rec.employee_id, rec.full_name; end loop; end;

Editor's Notes

  1. 1.1 Introduction to PLSQL 1.2 PL/PgSqL: Datatypes, Language structure 1.3 Controlling the program flow, conditional statements, loops 1.4 Stored Procedures 1.5 Stored Functions 1.6 Handling Errors and Exceptions 1.7 Cursors 1.8 Triggers
  2. Notice that the DO statement does not belong to the block. It is used to execute an anonymous block.
  3. The PostgreSQL SELECT INTO statement creates a new table and inserts data returned from a query into the table. If the SELECT statement returns more than one row, Oracle will raise the TOO_MANY_ROWS exception. If the SELECT statement does not return any row, Oracle will raise the NO_DATA_FOUND exception.
  4. Third, you can refer the type of variables to data type of function arguments to create polymorphic functions since the type of internal variables can change from one call to the next.
  5. -- select actor with id 10
  6. In Postgres 10 or later consider an IDENTITY column instead of a serial
  7. Notice that PostgreSQL introduced the assert statement since version 9.5. Check your PostgreSQL version before using it.
  8. Enable / Disable Assertions PostgreSQL provides the plpgsql.check_asserts configuration parameter to enable or disable assertion testing. If you set this parameter to off, the assert statement will do nothing.
  9. Notice that not all messages are reported back to the client. PostgreSQL only reports the info, warning, and notice level messages back to the client. This is controlled by client_min_messages and log_min_messages configuration parameters.
  10. The statement is a statement that will be executed if the condition is true. It can be any valid statement, even another IF statement. An IF code  statement is placed inside another IF statement is called a nested-IF statement. The following flowchart illustrates the simple IF statement
  11. In this example, we declare two variables a and b. In the body of the block, we compare the value of a and b using the comparison operator >, < and = in the boolean expressions of the IF statements and print out the corresponding message. Because a is less than b, we receive the following output when we execute the block: NOTICE:  a is less than b
  12. Because the expression a > b is false, the statement in the ELSE clause is executed. We get the following output: NOTICE:  a is not greater than b
  13. The IF and IF THEN ELSE statements allow you to have just one condition to evaluate. However, with this form of the IF statement, you may have multiple conditions to evaluate. The then condition is true, the corresponding statement in that branch is executed. For example, if the condition-1, condition-2, etc., is true then the corresponding statement: if-statement, elseif-statement-2, etc., will execute. In case one condition is true, PostgreSQL will stop evaluating the underneath conditions. If all conditions are false, the statements in the last ELSE branch are executed. The following flowchart illustrates the IF ELSIF ELSE statement.
  14. In this example, because a is equal to b, the statement in the ELSE branch is executed. 1 NOTICE:  aisequaltob In this tutorial, you have learned three forms of the IF statement to execute a statement based on a certain condition.
  15. Notice that you should not confuse about the CASE statement and CASE expression. The CASE expression evaluates to a value, while the CASE statement executes statements based on condition.
  16. select case1(1);
  17. This form of CASE is entirely equivalent to IF-THEN-ELSIF, except for the rule that reaching an omitted ELSE clause results in an error rather than doing nothing.
  18. The LOOP statement is also referred to as an unconditional loop statement because it executes the statements until the condition in the EXIT statement evaluates to true. Note that the condition specified after the WHEN keyword in the EXIT statement is a Boolean expression that evaluates to true or false. Loop statements can be nested. A LOOP statement is placed inside another LOOP statement is known as a nested loop. In this case, you need to the loop label to specify explicitly which loop you want to terminate in the EXIT statement.
  19. Fibonacci sequence The Fibonacci function accepts an integer and returns the nth Fibonacci number. By definition, Fibonacci numbers are the sequence of integers starting with 0 and 1, and each subsequent number is the sum of the previous two numbers, for example, 1, 1, 2 (1+1), 3 (2+1), 5 (3 +2), 8 (5+3), … In the declaration section, the counter variable is initialized to zero (0). Inside the loop, when counter equals n, the loop exits. The statement: 1 SELECTj,i+jINTOi,   j; swaps i and j at the same time without using a temporary variable.
  20. $1 is a reference to the first argument of the function.