PLSQL 2 5 PDF
PLSQL 2 5 PDF
PLSQL 2 5 PDF
PL/SQL
2-5
Writing PL/SQL Executable Statements
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 4
Writing PL/SQL Executable Statements
Assigning New Values to Variables
• Character and date literals must be enclosed in single
quotation marks.
v_name := 'Henderson';
v_start_date := '12-Dec-2005';
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 5
Writing PL/SQL Executable Statements
SQL Functions in PL/SQL
• You are already familiar with functions in SQL statements.
• For example:
SELECT LAST_DAY(SYSDATE)
FROM DUAL;
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 6
Writing PL/SQL Executable Statements
SQL Functions in PL/SQL
• Functions available in procedural statements:
– Single-row character
– Single-row number
– Date
– Data-type conversion
– Miscellaneous functions
• Not available in procedural statements:
– DECODE (CASE is used instead)
– Group functions (AVG, MIN, MAX etc. may
be used ONLY within a SQL statement)
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 7
Writing PL/SQL Executable Statements
SQL Functions in PL/SQL
SQL functions help you to manipulate data; they fall into
the following categories:
• Character
• Number
• Date
• Conversion
• Miscellaneous
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 8
Writing PL/SQL Executable Statements
Character Functions
• Valid character functions in PL/SQL include:
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 9
Writing PL/SQL Executable Statements
Examples of Character Functions
• Get the length of a string:
v_desc_size INTEGER(5);
v_prod_description VARCHAR2(70):='You can use this product
with your radios for higher frequency';
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 10
Writing PL/SQL Executable Statements
Number Functions
• Valid number functions in PL/SQL include:
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 11
Writing PL/SQL Executable Statements
Examples of Number Functions
• Get the sign of a number:
DECLARE
v_my_num BINARY_INTEGER := -56664;
BEGIN
DBMS_OUTPUT.PUT_LINE(SIGN(v_my_num));
END;
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 12
Writing PL/SQL Executable Statements
Date Functions
• Valid date functions in PL/SQL include:
ADD_MONTHS MONTHS_BETWEEN
CURRENT_DATE ROUND
CURRENT_TIMESTA
SYSDATE
MP
LAST_DAY TRUNC
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 14
Writing PL/SQL Executable Statements
Data-Type Conversion
• In any programming language, converting one data type
to another is a common requirement.
• PL/SQL can handle such conversions with scalar data
types.
• Data-type conversions can be of two types:
– Implicit conversions
– Explicit conversions
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 15
Writing PL/SQL Executable Statements
Implicit Conversions
• In implicit conversions, PL/SQL attempts to convert data
types dynamically if they are mixed in a statement.
• Implicit conversions can happen between many types in
PL/SQL, as illustrated by the following chart.
DATE LONG NUMBER PLS_INTEGER VARCHAR2
DATE N/A X X
LONG N/A X
NUMBER X N/A X X
PLS_INTEGER X X N/A X
VARCHAR2 X X X X N/A
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 16
Writing PL/SQL Executable Statements
Example of Implicit Conversion
• In this example, the variable v_sal_increase is of
type VARCHAR2.
• While calculating the total salary, PL/SQL first converts
v_sal_increase to NUMBER and then performs
the operation.
• The result of the operation is the NUMBER type.
DECLARE
v_salary NUMBER(6) := 6000;
v_sal_increase VARCHAR2(5) := '1000';
v_total_salary v_salary%TYPE;
BEGIN
v_total_salary := v_salary + v_sal_increase;
DBMS_OUTPUT.PUT_LINE(v_total_salary);
END;
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 17
Writing PL/SQL Executable Statements
Drawbacks of Implicit Conversions
At first glance, implicit conversions might seem useful;
however, there are several drawbacks:
• Implicit conversions can be slower.
• When you use implicit conversions, you lose control
over your program because you are making an
assumption about how Oracle handles the data.
• If Oracle changes the conversion rules, then your code
can be affected.
• Code that uses implicit conversion is harder to read and
understand.
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 18
Writing PL/SQL Executable Statements
Drawbacks of Implicit Conversions
Additional drawbacks:
• Implicit conversion rules depend upon the environment in
which you are running.
– For example, the date format varies depending on the
language setting and installation type.
– Code that uses implicit conversion might not run on a different
server or in a different language.
• It is strongly recommended that you AVOID allowing SQL or
PL/SQL to perform implicit conversions on your behalf.
• You should use conversion functions to guarantee that the
right kinds of conversions take place.
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 19
Writing PL/SQL Executable Statements
Drawbacks of Implicit Conversions
• It is the programmer's responsibility to ensure that
values can be converted.
• For instance, PL/SQL can convert the CHAR value '02-
Jun-1992' to a DATE value, but cannot convert the
CHAR value 'Yesterday' to a DATE value.
• Similarly, PL/SQL cannot convert a VARCHAR2 value
containing alphabetic characters to a NUMBER value.
Valid? Statement
Yes v_new_date DATE := '02-Jun-1992';
No v_new_date DATE := 'Yesterday';
Yes v_my_number NUMBER := '123';
No v_my_number NUMBER := 'abc';
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 20
Writing PL/SQL Executable Statements
Explicit Conversions
• Explicit conversions convert values from one data type
to another by using built-in functions.
• Examples of conversion functions include:
TO_NUMBER() ROWIDTONCHAR()
TO_CHAR() HEXTORAW()
TO_CLOB() RAWTOHEX()
CHARTOROWID() RAWTONHEX()
ROWIDTOCHAR() TO_DATE()
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 21
Writing PL/SQL Executable Statements
Examples of Explicit Conversions
• TO_CHAR
BEGIN
DBMS_OUTPUT.PUT_LINE(TO_CHAR(SYSDATE,'Month YYYY'));
END;
• TO_DATE
BEGIN
DBMS_OUTPUT.PUT_LINE(TO_DATE('April-1999','Month-YYYY'));
END;
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 22
Writing PL/SQL Executable Statements
Examples of Explicit Conversions
• TO_NUMBER
DECLARE
v_a VARCHAR2(10) := '-123456';
v_b VARCHAR2(10) := '+987654';
v_c PLS_INTEGER;
BEGIN
v_c := TO_NUMBER(v_a) + TO_NUMBER(v_b);
DBMS_OUTPUT.PUT_LINE(v_c);
END;
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 23
Writing PL/SQL Executable Statements
Data Type Conversion Examples
• Example #1
v_date_of_joining DATE := '02-Feb-2014';
• Example #2
v_date_of_joining DATE := 'February 02, 2014';
• Example #3
v_date_of_joining DATE := TO_DATE('February 02, 2014',
'Month DD, YYYY');
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 24
Writing PL/SQL Executable Statements
Operators in PL/SQL
The operations within an expression are performed in a
particular order depending on their precedence (priority).
• Logical
• Arithmetic
• Concatenation Same as
• Parentheses to control in SQL
the order of operations
• Exponential operator (**)
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 25
Writing PL/SQL Executable Statements
Operators in PL/SQL
The following table shows the default order of operations
from high priority to low priority:
Operator Operation
** Exponentiation
+, - Identity, negation
*, / Multiplication, division
AND Conjunction
OR Inclusion
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 26
Writing PL/SQL Executable Statements
Operators in PL/SQL Examples
• Increment the counter for a loop.
v_loop_count := v_loop_count + 1;
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 27
Writing PL/SQL Executable Statements
Terminology
Key terms used in this lesson included:
• Explicit conversion
• Implicit conversion
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 28
Writing PL/SQL Executable Statements
Summary
In this lesson, you should have learned how to:
• Construct accurate variable assignment statements in
PL/SQL
• Construct accurate statements using built-in SQL functions
in PL/SQL
• Differentiate between implicit and explicit conversions of
data types
• Describe when implicit conversions of data types take
placeList the drawbacks of implicit data type conversions
• Construct accurate statements using functions to explicitly
convert data types
• Construct statements using operators in PL/SQL
PLSQL S2L5 Copyright © 2018, Oracle and/or its affiliates. All rights reserved. 29
Writing PL/SQL Executable Statements