Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.

Similar presentations


Presentation on theme: "Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems."— Presentation transcript:

1 Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems

2 Objectives After completing this lesson, you should be able to:  Write PL/SQL blocks  Recognize the significance of the executable section.  Declare PL/SQL scalar variables of numeric, character, and date datatypes explicitly or using the %TYPE attribute  Use the DBMS_OUTPUT.PUT_LINE() procedure to display output from PL/SQL blocks

3 Why PL/SQL? PL/SQL lets you use all the SQL data manipulation, cursor control, and transaction control commands, as well as all the SQL functions, operators, and pseudo columns. So, you can manipulate Oracle data flexibly and safely. Also, PL/SQL fully supports SQL datatypes. This reduces the need to convert data passed between your applications and the database. PL/SQL also supports dynamic SQL, an advanced programming technique that makes your applications more flexible and versatile. Your programs can build and process SQL data definition, data control, and session control statements "on the fly" at run time.

4 A Basic PL/SQL Block Structure DECLARE /* this section is optional when you have no variables to declare */ BEGIN -- this section is mandatory and must -- contain at least one executable -- statement END; / Don’t forget to terminate the block with ; and / on the next line.

5 PL/SQL Block Syntax and Guidelines Statements can continue over several lines. Because PL/SQL is an extension of SQL, the general syntax rules that apply to SQL also apply to PL/SQL language.

6 Declaring PL/SQL Variables DECLARE v_my_name VARCHAR2(30) := ‘Jun’; v_my_dob DATE := ‘19-MAY-1981’; v_my_age NUMBER(3,0); BEGIN -- block continues -- next page You must specify length for VARCHAR2 variables. Use the v_ prefix to identify a PL/SQL variable. You need not specify precision and scale for NUMBER.

7 Declaring PL/SQL Variables DECLARE v_my_name VARCHAR2(30) := ‘Jun’; v_my_dob DATE := ‘19-MAY-1981’; v_my_age NUMBER(3,0); BEGIN v_my_age := (SYSDATE – v_my_dob) /365; END; The assignment operator in PL/SQL is :=.

8

9 The DBMS_OUTPUT.PUT_LINE () Procedure SET SERVEROUTPUT ON DECLARE v_my_name VARCHAR2(30) := ‘Jun’; v_my_dob DATE := ‘19-MAY-1981’; v_my_age NUMBER(3,0); BEGIN v_my_age := (SYSDATE – v_my_dob) /365; DBMS_OUTPUT.PUT_LINE(v_my_name || ‘ is ’ || v_my_age); END;  Used to display output from a PL/SQL block  Accepts only one expression as argument  Enabled with SET SERVEROUTPUT ON command

10

11 The %TYPE Attribute Used to “copy” an existing variable’s datatype when declaring a subsequent variable Can also “copy” a table column’s datatype Makes maintaining bigger programs easier

12 The %TYPE Attribute SET SERVEROUTPUT ON DECLARE v_my_name VARCHAR2 (30) := ‘Jun’; v_my_dob DATE := ‘19-MAY-1981’; v_my_age NUMBER(3,0); v_your_name v_my_name%TYPE := ‘You’; --Specify your DOB in the declaration below v_your_dob v_my_dob %TYPE := ‘’; v_your_age v_my_age%TYPE := 0; BEGIN -- block continues -- next page

13 The %TYPE Attribute v_my_age := (SYSDATE –v_my_dob) /365; DBMS_OUTPUT.PUT_LINE(v_my_name|| ‘ is ’ || v_my_age); v_your_age := (SYSDATE – v_your_dob) / 365; DBMS_OUTPUT.PUT_LINE(v_your_name|| ‘ is ’ || v_your_age); END;

14

15 Exercises 1. Alter the script on slide 7 so that it: Accepts the user’s birthday as a date datatype input Assigns the birthday value to the DATE variable v_your_dob Calculates the user’s age and outputs the sentence “your_name is your_age” as previously done.

16 Exercises 2. Alter your solution for exercise #1 so that it also: Calculates the number of days until your next birthday Outputs the message e.g. “Your 29 th birthday is 148 days away.”

17 Exercises 3. Write a script that: Accepts 2 numbers from the user Performs the addition operation on both numbers Displays the result of the operation e.g. 2 + 3 = 5 Does the same for the subtraction, multiplication, division, and exponentiation operations


Download ppt "Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems."

Similar presentations


Ads by Google