Sie sind auf Seite 1von 9

Oracle Academy

Introduction to Database Programming with PL/SQL


Code Samples for Section 2
/*******************************************
LESSON 01 - SLIDE 12
*******************************************/
DECLARE
v_myname VARCHAR2(20);
BEGIN
DBMS_OUTPUT.PUT_LINE('My name is: '||v_myname);
v_myname := 'John';
DBMS_OUTPUT.PUT_LINE('My name is: '||v_myname);
END;
/*******************************************
LESSON 01 - SLIDE 14
*******************************************/
DECLARE
v_myname VARCHAR2(20):= 'John';
BEGIN
v_myname := 'Steven';
DBMS_OUTPUT.PUT_LINE('My name is: '||v_myname);
END;
/*******************************************
LESSON 01 - SLIDE 15
*******************************************/
DECLARE
v_date VARCHAR2(30);
BEGIN
SELECT TO_CHAR(SYSDATE) INTO v_date FROM dual;
DBMS_OUTPUT.PUT_LINE(v_date);
END;

/*******************************************
LESSON 01 - SLIDE 16
*******************************************/
--function to return number of characters in string
--NOTE: This was created in the PL/SQL blocks lesson. You can either reference
--that function or execute DROP FUNCTION num_characters and recreate it
CREATE FUNCTION num_characters (p_string IN VARCHAR2) RETURN INTEGER AS
v_num_characters INTEGER;
BEGIN
SELECT length(p_string) into v_num_characters
FROM dual;
RETURN v_num_characters;
END;
--anonymous block: assign a variable to the function output
DECLARE
v_length_of_string INTEGER;
BEGIN
v_length_of_string := num_characters('Oracle Corporation');
DBMS_OUTPUT.PUT_LINE(v_length_of_string);

END;
/*******************************************
LESSON 02 - SLIDE 06
*******************************************/
CREATE PROCEDURE print_date IS
v_date VARCHAR2(30);
BEGIN
SELECT TO_CHAR(SYSDATE,'Mon DD, YYYY')
INTO v_date
FROM dual;
DBMS_OUTPUT.PUT_LINE(v_date);
END;
/*******************************************
LESSON 02 - SLIDE 11
*******************************************/
DECLARE
date DATE;
BEGIN
SELECT ADD_MONTHS(SYSDATE,3) INTO date
FROM dual;
END;
/*******************************************
LESSON 02 - SLIDE 18
*******************************************/
DECLARE
v_annual_sal NUMBER (9,2);
BEGIN
/* Compute the annual salary based on the monthly salary input from the user */
v_annual_sal := v_monthly_sal * 12;
END;
/**********************************************************
LESSON 03: Recognizing Data Types - No code samples in this lesson
**********************************************************/
/*******************************************
LESSON 04 - SLIDE 09
*******************************************/
DECLARE
country_id
CHAR(2);
BEGIN
SELECT country_id
INTO country_id
FROM countries
WHERE country_name = 'Canada';
END;
/*******************************************

LESSON 04 - SLIDE 11
*******************************************/
-- Create a table for the following anonymous block
CREATE TABLE myemps (
emp_name
VARCHAR2(6),
emp_salary
NUMBER(6,2));
-- Anonymous block references the table
DECLARE
v_emp_salary NUMBER(6,2);
BEGIN
SELECT emp_salary INTO v_emp_salary
FROM myemps WHERE emp_name = 'Smith';
END;

/*******************************************
LESSON 04 - SLIDE 15
*******************************************/
-- This block uses the table from Lesson 04 Slide 11
DECLARE
v_emp_salary myemps.emp_salary%TYPE;
BEGIN
SELECT emp_salary INTO v_emp_salary
FROM myemps WHERE emp_name = 'Smith';
END;

/*******************************************
LESSON 05 - SLIDE 04
*******************************************/
--Example 1
DECLARE
v_name
VARCHAR2(10);
v_start_date DATE;
BEGIN
v_name := 'Henderson' ;
v_start_date := '12-DEC-2005';
DBMS_OUTPUT.PUT_LINE(v_name);
DBMS_OUTPUT.PUT_LINE (v_start_date);
END;
--Example 2
DECLARE
v_quote VARCHAR2(250);
BEGIN
v_quote := 'The only thing that we can know is that we know nothing
and that is the highest flight of human reason.';
DBMS_OUTPUT.PUT_LINE(v_quote);
END;

--Example 3
DECLARE
v_my_integer
v_my_sci_not
BEGIN
v_my_integer
v_my_sci_not

PLS_INTEGER;
BINARY_INTEGER;
:= 100;
:= 2E5;

DBMS_OUTPUT.PUT_LINE (v_my_integer);
DBMS_OUTPUT.PUT_LINE (v_my_sci_not);
END;

/*******************************************
LESSON 05 - SLIDE 05
*******************************************/
--Example 1
SELECT country_name, LAST_DAY(date_of_independence)
FROM wf_countries
WHERE date_of_independence IS NOT NULL:
--Example 2
DECLARE
v_last_day DATE;
BEGIN
v_last_day := LAST_DAY(SYSDATE);
DBMS_OUTPUT.PUT_LINE(v_last_day);
END;
/*******************************************
LESSON 05 - SLIDE 08
*******************************************/
-- Example 1
DECLARE
v_desc_size
INTEGER(5);
v_prod_description VARCHAR2(70) :='You can use this product with your radios
for higher frequency';
BEGIN
-- get the length of the string in prod_description
v_desc_size:= LENGTH(v_prod_description);
DBMS_OUTPUT.PUT_LINE(v_desc_size);
END;
-- Example 2
DECLARE
v_country
VARCHAR2(50):='Canada';
v_capitol_name VARCHAR2(50);

BEGIN
SELECT capitol INTO v_capitol_name FROM wf_countries
WHERE country_name = v_country;
v_capitol_name:= UPPER(v_capitol_name);
DBMS_OUTPUT.PUT_LINE(v_capitol_name);
END;

/*******************************************
LESSON 05 - SLIDE 08 (continued)
*******************************************/
-- Example 3
DECLARE
v_first_name VARCHAR2(20);
v_last_name VARCHAR2(25);
v_emp_name VARCHAR2(45);
BEGIN
SELECT first_name, last_name INTO v_first_name, v_last_name
FROM employees
WHERE employee_id = 100;
v_emp_name:= v_first_name||' '||v_last_name;
DBMS_OUTPUT.PUT_LINE(v_emp_name);
END;
/*******************************************
LESSON 05 - SLIDE 10
*******************************************/
-- Example 1
DECLARE
v_my_num BINARY_INTEGER :=-56664;
BEGIN
DBMS_OUTPUT.PUT_LINE(SIGN(v_my_num));
END;
-- Example 2
DECLARE
v_median_age NUMBER(6,2);
BEGIN
SELECT median_age INTO v_median_age
FROM wf_countries WHERE country_id=27;
DBMS_OUTPUT.PUT_LINE(ROUND(v_median_age,0));
END;

/*******************************************
LESSON 05 - SLIDE 12

*******************************************/
-- Example 1
DECLARE
v_new_date DATE;
v_num_months NUMBER := 6;
BEGIN
v_new_date := ADD_MONTHS(SYSDATE,v_num_months);
DBMS_OUTPUT.PUT_LINE(v_new_date);
END;
-- Example 2
DECLARE
v_no_months PLS_INTEGER:=0;
BEGIN
v_no_months := MONTHS_BETWEEN('31-JAN-06','31-MAY-05');
DBMS_OUTPUT.PUT_LINE(v_no_months);
END;
/*******************************************
LESSON 05 - SLIDE 15
*******************************************/
DECLARE
v_salary
NUMBER(6):=6000;
v_sal_increase
VARCHAR2(5):='1000';
v_total_salary
v_salary%TYPE;
BEGIN
v_total_salary:= v_salary + v_sal_increase;
DBMS_OUTPUT.PUT_LINE(v_total_salary);
END;

/*******************************************
LESSON 05 - SLIDE 20
*******************************************/
-- Example 1
BEGIN
DBMS_OUTPUT.PUT_LINE(TO_CHAR(SYSDATE,'Month YYYY'));
END;
-- Example 2
BEGIN

DBMS_OUTPUT.PUT_LINE(TO_DATE('April-1999','Month-YYYY'));
END;
/*******************************************
LESSON 05 - SLIDE 21
*******************************************/
DECLARE
v_a VARCHAR2(10) := '-123456';
v_b VARCHAR2(10) := '+987654';
v_c PLS_INTEGER;
BEGIN
v_c := TO_NUMBER(v_a) + TO_NUMBER(v_b);
DBMS_OUTPUT.PUT_LINE(v_c);
END;
/*******************************************
LESSON 05 - SLIDE 22
*******************************************/
-- The first and third conversions will succeed.
-- The second conversion (commented out) will fail
DECLARE
v_date_of_joining DATE;
BEGIN
v_date_of_joining := '02-Feb-2000';
DBMS_OUTPUT.PUT_LINE(v_date_of_joining);
-- v_date_of_joining := 'February 02,2000';
-- DBMS_OUTPUT.PUT_LINE(v_date_of_joining);
v_date_of_joining := TO_DATE('February 02,2000','Month DD,YYYY');
DBMS_OUTPUT.PUT_LINE(v_date_of_joining);
END;

/********************************************************************
LESSON 06 SLIDE 06 *************************************************************
*******/
DECLARE
v_outer_variable VARCHAR2(20):='GLOBAL VARIABLE';
BEGIN
DECLARE
v_inner_variable VARCHAR2(20):='LOCAL VARIABLE';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_inner_variable);
DBMS_OUTPUT.PUT_LINE(v_outer_variable);
END;
DBMS_OUTPUT.PUT_LINE(v_outer_variable);
END;
/********************************************************************
LESSON 06 SLIDE 08 *************************************************************
*******/
DECLARE
v_father_name VARCHAR2(20):='Patrick';
v_date_of_birth DATE:='20-Apr-1972';
BEGIN
DECLARE
v_child_name VARCHAR2(20):='Mike';
BEGIN
DBMS_OUTPUT.PUT_LINE('Father''s Name: '||v_father_name);

DBMS_OUTPUT.PUT_LINE('Date of Birth: '||v_date_of_birth);


DBMS_OUTPUT.PUT_LINE('Child''s Name: '||v_child_name);
END;
DBMS_OUTPUT.PUT_LINE('Date of Birth: '||v_date_of_birth);
END;
/********************************************************************
LESSON 06 SLIDE 09 *************************************************************
*******/
DECLARE
v_outer_variable VARCHAR2(20):='GLOBAL VARIABLE';
BEGIN
DECLARE
v_inner_variable VARCHAR2(20):='LOCAL VARIABLE';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_inner_variable);
DBMS_OUTPUT.PUT_LINE(v_outer_variable);
END;
DBMS_OUTPUT.PUT_LINE(v_outer_variable);
END;
/********************************************************************
LESSON 06 SLIDE 10 *************************************************************
*******/
DECLARE
v_outer_variable VARCHAR2(20):='GLOBAL VARIABLE';
BEGIN
DECLARE
v_inner_variable VARCHAR2(20):='LOCAL VARIABLE';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_inner_variable);
DBMS_OUTPUT.PUT_LINE(v_outer_variable);
END;
DBMS_OUTPUT.PUT_LINE(v_outer_variable);
END;
/********************************************************************
LESSON 06 SLIDE 11 *************************************************************
*******/
DECLARE
v_father_name VARCHAR2(20):='Patrick';
v_date_of_birth DATE:='20-Apr-1972';
BEGIN
DECLARE
v_child_name
VARCHAR2(20):='Mike';
v_date_of_birth DATE:='12-Dec-2002';
BEGIN
DBMS_OUTPUT.PUT_LINE('Date of Birth' || v_date_of_birth);
END;
END;
/********************************************************************
LESSON 06 SLIDE 17 *************************************************************
*******/
DECLARE
v_father_name VARCHAR2(20):='Patrick';
v_date_of_birth DATE:='20-Apr-1972';
BEGIN

DECLARE
v_child_name
VARCHAR2(20):='Mike';
v_date_of_birth DATE:='12-Dec-2002';
BEGIN
DBMS_OUTPUT.PUT_LINE('Father''s Name: '||v_father_name);
DBMS_OUTPUT.PUT_LINE('Date of Birth: '||v_date_of_birth);
DBMS_OUTPUT.PUT_LINE('Child''s Name: '||v_child_name);
END;
DBMS_OUTPUT.PUT_LINE('Date of Birth: '||v_date_of_birth);
END;
/********************************************************************
LESSON 06 SLIDE 20 *************************************************************
*******/
<<outer>>
DECLARE
v_father_name VARCHAR2(20):='Patrick';
v_date_of_birth DATE:='20-Apr-1972';
BEGIN
DECLARE
v_child_name VARCHAR2(20):='Mike';
v_date_of_birth DATE:='12-Dec-2002';
BEGIN
DBMS_OUTPUT.PUT_LINE('Father''s Name: '||v_father_name);
DBMS_OUTPUT.PUT_LINE('Date of Birth: ' ||outer.v_date_of_birth);
DBMS_OUTPUT.PUT_LINE('Child''s Name: '||v_child_name);
DBMS_OUTPUT.PUT_LINE('Date of Birth: '||v_date_of_birth);
END;
END;
/********************************************************************
LESSON 07 SLIDE 06
********************************************************************/
DECLARE
v_annual_sal NUMBER (9,2);
BEGIN
-- Begin the executable sec
tion
/* Compute the annual salary based on the
monthly salary input from the user */
v_annual_sal := v_monthly_sal * 12;
END;
ock

-- This is the end of the bl

/********************************************************************
LESSON 07 SLIDE 09
********************************************************************/
DECLARE
v_deptno
NUMBER(4);
v_location_id NUMBER(4);
BEGIN
SELECT
department_id,
location_id
INTO
v_deptno,
v_location_id
FROM
departments
WHERE
department_name = 'Sales';
END;
Oracle Academy 3
Database Programming with PL/SQL
Copyright 2010, Oracle. All rights reserved.