Download presentation
Presentation is loading. Please wait.
1
Fundamentals of PL/SQL part 1 (Basics)
2
Defining variables and constants
The most common datatypes in PL/SQL are in four families: string, number, date, and logical (Boolean). Strings are "free form" data. A string can contain any valid character in the character set of a language. While there are several variations of strings, the datatype you will mostly use is VARCHAR2. The general format for a VARCHAR2 declaration is: variable_name VARCHAR2(n); The declaration of a NUMBER looks like this: variable_name NUMBER [ ( precision [, scale ] ) ];
3
Examples The following examples demonstrate the different ways you can declare variables of type NUMBER: The bean_counter variable can hold values with up to ten digits of precision, three of which are to the right of the decimal point. If you assign to bean_counter, it is rounded to If you assign to the variable, the operation will return an error because there are more digits than allowed for in the precision: bean_counter NUMBER (10,3); The any_number variable can span the full range of supported values, because the default precision and scale are unspecified: any_number NUMBER;
4
Rules Here are some rules to keep in mind when declaring numbers:
If the scale is positive, then the scale determines the point at which rounding occurs to the right of the decimal point. You can use a negative scale, in which case the scale determines the point at which rounding occurs to the left of the decimal point. If the scale is zero, then rounding occurs to the nearest whole number. If the scale is not specified, then no rounding occurs. The absolute magnitude range of legal numbers is fairly enormous: from to 10125 NOTE: You can also declare numbers that are whole numbers (they have no fractional components) as having the INTEGER datatype. An Oracle integer may have up to 38 digits.
5
Constants The complete syntax template for declaring a variable is:
variable_name DATATYPE [ CONSTANT ] [ := | DEFAULT initial_value ]; CONSTANT If this keyword is present, prevents the program from changing the value of the variable. Must be accompanied by an initial value. A convenience that the compiler provides to help you protect yourself from silly errors. := | DEFAULT Designates the presence of an optional clause that can initialize the variable to a non-null value (see the following discussion). You can use either the assignment operator, :=, or the keyword DEFAULT to set apart the initial value.
6
Examples of variables and Constants
Normal integer initialized to the highest possible value without resorting to scientific notation (there are 38 nines): max_grains_of_sand INTEGER := ; Number variable whose default value is the result of an arithmetic operator: national_debt FLOAT DEFAULT 10**10; A quantity unlikely to change, and therefore made a constant, which requires an initial value: earth_circumference_miles CONSTANT NUMBER := ;
7
Types of PL/SQL Operators
Operator Category Notation Meaning Assignment := Store the value Arithmetic + - / * ** Addition Subtraction Division Multiplication Exponentiation Logical AND OR NOT Conjunction Disjunction Negation
8
Types of PL/SQL Operators
Operator Category Notation Meaning Comparison (of non-nulls) = != < > <= >= IN BETWEEN Equality Inequality Less than Greater than Less than or equal Greater than or equal Equality disjunction Range test
9
Types of PL/SQL Operators
Operator Category Notation Meaning Comparison (of nulls) IS NULL IS NOT NULL Nullity test Non-nullity test String LIKE || Wildcard matching Concatenation
10
First PL/SQL Procedure
Example 1: BEGIN DBMS_OUTPUT.PUT_LINE('hello, world'); END; / Example 2: Declare Str1 varchar2(20):=‘Hello, world’; Begin Dbms_output.put_line(Str1); End NOTE: These are called as anonymous block—that is, a block with no name. Its only executable statement is a call to the procedure PUT_LINE, supplied in Oracle's built-in package named DBMS_OUTPUT. This built-in stored procedure can accept a string that can get printed to the screen when you run it.
11
Types of Blocks In PL/SQL, there are only three types of blocks:
Anonymous blocks Procedures Functions
12
Named blocks: procedures and functions
A named program that executes some predefined statements and then returns control to whatever called it. After creating a procedure, you can invoke it by name from other programs. Function Similar to a procedure, except that it returns a value to the program that calls it. The data returned by a function is always of a specific, predefined datatype.
13
Conditional Statements
Syntax IF Statement If <some condition> …… End If; IF-THEN-ELSE STATEMENT if condition then ……. else End if;
14
Conditional Statements
Syntax IF-THEN-ELSIF statement if <some condition> then ……. Elsif<some condition> Then …. Else …… End if; IF-THEN-ELSE STATEMENT IF condition THEN else
15
Conditional Statements
Syntax Case Statement CASE selector WHEN 'value1' THEN S1; WHEN 'value2' THEN S2; WHEN 'value3' THEN S3; ELSE Sn; -- default case END CASE; Nested IF-THEN-ELSE I If( <some condition>) Then If(<some condition>) End if Else ……
16
Summary How to define variables and constants Types of PL/SQL Operators Types of Blocks Conditional Statements
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.