Pl-sql blocks and block types and variablesdeclaring.pptx
abobakralwaylysocial
39 views
42 slides
Jan 26, 2024
Slide 1 of 42
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
About This Presentation
Pl/sql very help for you to start pl/sql language, it contains all the basics you need as a new learner.
Size: 344.22 KB
Language: en
Added: Jan 26, 2024
Slides: 42 pages
Slide Content
PL/SQL Block , Declaring variables ,Flow of Control and Exception Handling Part 1
About PL/SQL PL/SQL: Stands for “Procedural Language” extension to SQL. Is Oracle Corporation's standard data access language for relational databases Seamlessly integrates procedural constructs with SQL
About PL/SQL Provides a block structure for executable units of code. Maintenance of code is made easier with such a well- defined structure. Provides procedural constructs such as: Variables, constants, and data types Control structures such as conditional statements and loops Reusable program units that are written once and executed many times
Benefits of PL/SQL Benefits of PL/SQL Integration of procedural constructs with SQL Improved performance Exception handling
PL/SQL Block
PL/SQL Block Structure DECLARE (optional) - Variables, cursors, user-defined exceptions BEGIN (mandatory) - SQL statements - PL/SQL statements EXCEPTION (optional) - Actions to perform when exceptions occur END; (mandatory)
Block Types Anonymous Procedure Function Anonymous [Declare] Begin --Statements [Exception] End Procedure PROCEDUER name IS Begin --Statements [Exception] End Function FUNCTION name RETURN Begin -- Statements RETURN value [Exception] End
Examining an Anonymous Block SET SERVEROUTPUT ON; DECLARE v_hello VARCHAR2(20):= 'Hello World!'; BEGIN dbms_output.put_line ( v_hello ); END;
Enabling Output of a PL/SQL Block To enable output in SQL Developer, execute the following command before running the PL/SQL block : SET SERVEROUTPUT ON; Use a predefined Oracle package and its procedure in the anonymous block : DBMS_OUTPUT.PUT_LINE(); Ex: DBMS_OUTPUT.PUT_LINE('The First Name of the Employee is' || v_fname );
Declaring PL/SQL Variables
Use of Variables Variables can be used for : Temporary storage of data Manipulation of stored values Reusability
Requirements for Variable Names A variable name : Must start with a letter Can include letters or numbers Can include special characters (such as $,_, and #) Must contain no more than 30 characters Must not include reserved words
Guidelines for Declaring and Initializing PL/SQL Variables Follow consistent naming conventions. Use meaningful identifiers for variables . Initialize variables that are designated as NOT NULL and CONSTANT . Initialize variables with the assignment operator (:) or the DEFAULT keyword : V_myName VARCHAR2(20):= 'John ' ; V _myName VARCHAR2 (20) DEFAULT 'John '; 5.Declare one identifier per line for better readability and code maintenance. 6. Avoid using column names as identifiers. 7. Use the NOT NULL constraint when the variable must hold a value.
Declaring Variables DECLARE v_fname VARCHAR2(20); BEGIN SELECT first_name INTO v_fname FROM employees WHERE employee_id = 100; dbms_output.put_line ( v_fname ); END ; /
Declaring Variables DECLARE v_fname VARCHAR2(20 ); BEGIN dbms_output.put_line ( v_fname ); END; / --------------------------------------------------------------------------------------------------------------------- DECLARE v_fname VARCHAR2(20) NOT NULL :='Ahmed'; BEGIN dbms_output.put_line ( v_fname ); END; /
Declaring Variables DECLARE v_fname CONSTANT VARCHAR2(20) :='Ahmed'; BEGIN dbms_output.put_line ( v_fname ); END; / --------------------------------------------------------------------------------------------------------------------- DECLARE v_fname CONSTANT VARCHAR2(20) :='Ahmed'; BEGIN v_fname :='Ali'; dbms_output.put_line ( v_fname ); END ; /
%TYPE Attribute Is used to declare a variable according to: A database column definition Another declared variables prefixed with : The database table and column name The name of the declared variable
%TYPE Attribute DECLARE v_FirstName Employees.First_Name%Type ; BEGIN SELECT first_name INTO v_FirstName FROM Employees WHERE employee_id = 100; dbms_output.put_line ( v_FirstName ); END ;
%TYPE Attribute DECLARE V_FirstName Employees.First_Name%Type ; V_LastName V_FirstName%Type ; BEGIN SELECT last_name INTO V_LastName FROM Employees WHERE employee_id = 100; dbms_output.put_line ( V_LastName ); END;
Bind Variables Bind variables are : Created in the environment Also called host variables Created with the VARIABLE keyword * Used in SQL statements and PL/SQL blocks Accessed even after the PL/SQL block is executed Referenced with a preceding colon Values can be output using the PRINT command .
Using AUTOPRINT with Bind Variables Use the SET AUTOPRINT ON command to automatically display the bind variables used in a successful PL/SQL block.
Bind Variables SET AUTOPRINT ON ; VARIABLE b_result NUMBER BEGIN SELECT SALARY INTO : b_result FROM employees WHERE employee_id = 144; END; / PRINT b_result
IF Statement
Simple IF Statement IF condition THEN Statements End IF;
Simple IF Statement DECLARE v_MyNumber Number :=18; BEGIN IF v_MyNumber > 0 THEN dbms_output.put_line ('It is +'); End IF; END; /
IF THEN ELSE Statement IF condition THEN Statements ELSE Statements End IF;
IF THEN ELSE Statement DECLARE v_MyNumber Number :=-18 ; BEGIN IF v_MyNumber > 0 THEN dbms_output.put_line ('It is +'); ELSE dbms_output.put_line ('It is -'); End IF; END; /
IF THEN ELSE Statement IF condition THEN Statements ELSEIF condition THEN Statements ELSE Statements End IF;
IF THEN ELSE Statement DECLARE v_MyNumber NUMBER := 0; BEGIN IF v_MyNumber > 0 THEN dbms_output.put_line ('It is +'); ELSIF v_MyNumber < 0 THEN dbms_output.put_line ('It is -'); ELSE dbms_output.put_line ('It is Zero'); END IF; END;
Case Statement
Case Expression CASE WHEN expression1 THEN result1 WHEN expression2 THEN result2 ….. [ELSE result] END;
Case Statement DECLARE v_score NUMBER := 85; v_grade VARCHAR2(2); BEGIN CASE WHEN v_score >= 90 THEN v_grade := 'A'; WHEN v_score >= 80 THEN v_grade := 'B'; WHEN v_score >= 70 THEN v_grade := 'C'; WHEN v_score >= 60 THEN v_grade := 'D'; ELSE v_grade := 'F'; END CASE ; dbms_output.put_line ('Grade: ' || v_grade ); END; /
Iterative Control Loop Statements
Iterative Control: Loop Statements Loops repeat a statement (or a sequence of statements) multiple times . There are three loop types : Basic loop WHILE loop FOR loop
Basic Loops LOOP Statements Exit [When condition]; END LOOP;
Basic Loops DECLARE v_counter NUMBER := 1; BEGIN LOOP dbms_output.put_line ('Iteration: ' || v_counter ); v_counter := v_counter + 1; EXIT WHEN v_counter > 5; END LOOP; END; /
Basic Loops DECLARE v_countryid locations.country_id%TYPE := 'CA '; v_loc_id locations.location_id%TYPE ; v_counter NUMBER(2) := 1 ; v_new_city locations.city%TYPE := 'Montreal'; BEGIN SELECT MAX( location_id ) INTO v_loc_id FROM locations WHERE country_id = v_countryid ; LOOP INSERT INTO locations ( location_id , city, country_id ) VALUES(( v_loc_id + v_counter ), v_new_city , v_countryid ); v_counter := v_counter + 1 ; EXIT WHEN v_counter > 3 ; END LOOP ; END ; /
While Loop While Condition LOOP Statement1; Statement2; …… END LOOP;
While Loop DECLARE v_countryid locations.country_id%TYPE := 'CA '; v_loc_id locations.location_id%TYPE ; v_new_city locations.city%TYPE := 'Montreal '; v_counter NUMBER:= 1 ; BEGIN SELECT MAX( location_id ) INTO v_loc_id FROM locations WHERE country_id = v_countryid ; WHILE v_counter <= 3 LOOP INSERT INTO locations ( location_id , city, country_id ) VALUES(( v_loc_id + v_counter ), v_new_city , v_countryid ); v_counter := v_counter + 1; END LOOP ; END ; /