1 plsql introduction1
DESCRIPTION
TRANSCRIPT
![Page 1: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/1.jpg)
PL/SQL : INTRODUCTION
![Page 2: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/2.jpg)
PSG
![Page 3: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/3.jpg)
DIFFERENCE BETWEEN PL/SQL AND SQL
PSG
![Page 4: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/4.jpg)
Comparison of SQL*PLUS and PL/SQL
PSG
![Page 5: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/5.jpg)
PL/SQL BLOCKS
PL/SQL blocks can be divided into two groups:
1. Named and
2. Anonymous.
PSG
![Page 6: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/6.jpg)
PL/SQL BLOCK STRUCTURE
• PL/SQL blocks contain three sections1. Declare section2. Executable section and3. Exception-handling section.
PSG
![Page 7: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/7.jpg)
PL/SQL BLOCK STRUCTURE
• PL/SQL block has the following structure:
DECLARE Declaration statements
BEGINExecutable statements
EXCETIONException-handling statements
END ;
PSG
![Page 8: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/8.jpg)
DECLARATION SECTION
• ExampleDECLARE
v_first_name VARCHAR2(35) ;v_last_name VARCHAR2(35) ;v_counter NUMBER := 0 ;
PSG
![Page 9: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/9.jpg)
EXECUTABLE SECTION
BEGINSELECT first_name, last_name
INTO v_first_name, v_last_nameFROM studentWHERE student_id = 123 ;
DBMS_OUTPUT.PUT_LINE(‘Student name :’ || v_first_name ||‘ ’|| v_last_name);
END;
PSG
![Page 10: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/10.jpg)
EXCEPTION-HANDLING SECTION
EXCEPTIONWHEN NO_DATA_FOUND THENDBMS_OUTPUT.PUT_LINE
(‘ There is no student with student id 123 ’);END;
PSG
![Page 11: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/11.jpg)
HOW PL/SQL GETS EXECUTED
PSG
![Page 12: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/12.jpg)
PL/SQL IN SQL*PLUS
PSG
![Page 13: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/13.jpg)
SQL EXAMPLE
SELECT first_name, last_nameFROM student;
PSG
![Page 14: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/14.jpg)
PL/SQL EXAMPLEDECLARE
v_first_name VARCHAR2(35);v_last_name VARCHAR2(35);
BEGINSELECT first_name, last_nameINTO v_first_name, v_last_nameFROM studentWHERE student_id = 123;DBMS_OUTPUT.PUT_LINE('Student name: '||v_first_name||' '||v_last_name);
EXCEPTIONWHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('There is no student with student id 123');
END;./
PSG
![Page 15: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/15.jpg)
SQL> SET SERVEROUTPUT ON;
PSG
![Page 16: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/16.jpg)
Second, use DBMS_OUTPUT.PUT_LINE in your executable section to display any message you want to the screen.
Syntax for displaying a message:DBMS_OUTPUT.PUT_LINE(<string>);in which PUT_LINE is the procedure to generate
the output on the screen, and DBMS_OUTPUT is the package to which the PUT_LINE belongs.
DBMS_OUTPUT_PUT_LINE(‘My age is ‘ || num_age);
PSG
![Page 17: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/17.jpg)
SUBSTITUTIONVARIABLES• Substitution variables are usually prefixed by
the ampersand(&) character or double ampersand (&&) character.
PSG
![Page 18: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/18.jpg)
EXAMPLEDECLARE
v_student_id NUMBER := &sv_student_id;v_first_name VARCHAR2(35);v_last_name VARCHAR2(35);
BEGINSELECT first_name, last_nameINTO v_first_name, v_last_nameFROM studentWHERE student_id = v_student_id;DBMS_OUTPUT.PUT_LINE
('Student name: '||v_first_name||' '||v_last_name);EXCEPTION
WHEN NO_DATA_FOUND THENDBMS_OUTPUT.PUT_LINE('There is no such student');
END;
PSG
![Page 19: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/19.jpg)
EXAMPLE
• When this example is executed, the user is asked to provide a value for the student ID.
• The example shown above uses a single ampersand for the substitution variable.
• When a single ampersand is used throughout the PL/SQL block, the user is asked to provide a value for each occurrence of the substitution variable.
PSG
![Page 20: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/20.jpg)
EXAMPLE
BEGINDBMS_OUTPUT.PUT_LINE('Today is '||'&&sv_day');DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ &sv_day');
END;
PSG
![Page 21: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/21.jpg)
OUTPUT
Enter value for sv_day: Mondayold 2: DBMS_OUTPUT.PUT_LINE('Today is ‘||’ &&sv_day');new 2: DBMS_OUTPUT.PUT_LINE('Today is ‘||’ Monday');old 3: DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ &sv_day');new 3: DBMS_OUTPUT.PUT_LINE('Tomorrow will be ‘||’ Monday');
Today is MondayTomorrow will be MondayPL/SQL procedure successfully completed.
PSG
![Page 22: 1 plsql introduction1](https://reader033.vdocuments.mx/reader033/viewer/2022061122/546f9edab4af9f210b8b45fd/html5/thumbnails/22.jpg)
Substitution Variables
SET DEFINE characterorSET DEFINE ONorSET DEFINE OFF
PSG