22
PL/SQL : INTRODUCTION

1 plsql introduction1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 1 plsql introduction1

PL/SQL : INTRODUCTION

Page 2: 1 plsql introduction1

PSG

Page 3: 1 plsql introduction1

DIFFERENCE BETWEEN PL/SQL AND SQL

PSG

Page 4: 1 plsql introduction1

Comparison of SQL*PLUS and PL/SQL

PSG

Page 5: 1 plsql introduction1

PL/SQL BLOCKS

PL/SQL blocks can be divided into two groups:

1. Named and

2. Anonymous.

PSG

Page 6: 1 plsql introduction1

PL/SQL BLOCK STRUCTURE

• PL/SQL blocks contain three sections1. Declare section2. Executable section and3. Exception-handling section.

PSG

Page 7: 1 plsql introduction1

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

DECLARATION SECTION

• ExampleDECLARE

v_first_name VARCHAR2(35) ;v_last_name VARCHAR2(35) ;v_counter NUMBER := 0 ;

PSG

Page 9: 1 plsql introduction1

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

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

HOW PL/SQL GETS EXECUTED

PSG

Page 12: 1 plsql introduction1

PL/SQL IN SQL*PLUS

PSG

Page 13: 1 plsql introduction1

SQL EXAMPLE

SELECT first_name, last_nameFROM student;

PSG

Page 14: 1 plsql introduction1

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

SQL> SET SERVEROUTPUT ON;

PSG

Page 16: 1 plsql introduction1

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

SUBSTITUTIONVARIABLES• Substitution variables are usually prefixed by

the ampersand(&) character or double ampersand (&&) character.

PSG

Page 18: 1 plsql introduction1

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

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

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

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

Substitution Variables

SET DEFINE characterorSET DEFINE ONorSET DEFINE OFF

PSG