Download presentation
Presentation is loading. Please wait.
Published byMyrtle Richard Modified over 9 years ago
1
Introduction to PL/SQL As usual, use speaker notes for additional information!
2
DECLARE (optional) Declarative section - allows for variables, types, cursors, user defined-exceptions etc BEGIN (required) Executable section - PL/SQL procedural statements and SQL statements EXCEPTION (optional) Exception section - error and exception handling END; (required) PL/SQL PL/SQL is a procedural language that can be used with Oracle. It allows an approach to data handling the includes stepping through each record one by one. It is written in a block structure where each block performs a logical task. Blocks can be nested. The structure of a block is:
3
SQL> EDIT try_this SQL> @ try_this PL/SQL procedure successfully completed. PL/SQL DECLARE v_msg VARCHAR2(25); BEGIN v_msg := 'This PL/SQL block works!'; END; / In the editor, I entered the code shown and saved under the name try_this. The assignment symbol is := It is used to assign a value on the right to a variable on the left.
4
PL/SQL SQL> edit try_this SQL> @ try_this The output is: This PL/SQL block works! PL/SQL procedure successfully completed. SET SERVEROUTPUT ON DECLARE v_msg VARCHAR2(25); BEGIN v_msg := 'This PL/SQL block works!'; DBMS_OUTPUT.PUT_LINE('The output is: ' || v_msg); END; / SET SERVEROUTPUT OFF In this example, I added the code to display the contents of the variable on the screen. Note that the code displays the literal The output is: concatenated with the contents of v_msg. SQL> START try_this The output is: This PL/SQL block works! PL/SQL procedure successfully completed. START is an alternative to @ when executing the procedure.
5
PL/SQL SQL> VARIABLE v_msg VARCHAR2(25); SQL> edit try_this1 SQL> @ try_this1 PL/SQL procedure successfully completed. SQL> print v_msg V_MSG -------------------------------- This PL/SQL still works! BEGIN :v_msg := 'This PL/SQL still works!'; END; / In this example, I declared a variable called v_msg at the SQL prompt. Then I edited a procedure called try_this1. In the procedure I assigned a message to the variable I had declared at the prompt. Since the variable was declared externally (outside the procedure) the variable in the procedure must have a colon in front of it. After running the procedure successfully, I entered the print command at the SQL prompt. Since the variable was declared at the SQL prompt, I can use the print statement to print its contents. Note colon :v_msg
6
SQL> SELECT * FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 45000 2000 PL/SQL SQL> edit pay_one SQL> @ pay_one Stephen York CM 42000 PL/SQL procedure successfully completed. SET SERVEROUTPUT ON DECLARE v_name VARCHAR2(20); v_jobcode CHAR(2); v_salary NUMBER(9,2); BEGIN SELECT name, jobcode, salary INTO v_name, v_jobcode, v_salary FROM first_pay WHERE name = 'Stephen York'; DBMS_OUTPUT.PUT_LINE (v_name || ' ' || v_jobcode || ' ' || v_salary); END; / SET SERVEROUTPUT OFF The procedure that was created in the editor is shown in the red box below. The output that was created - formatted and set up by the DBMS_OUTPUT.PUT_LINE statement. This is pay_one. Note that the SELECT statement must be written to only retrieve one record.
7
SQL> DESC first_pay; Name Null? Type ------------------------------- -------- ---- PAY_ID VARCHAR2(4) NAME VARCHAR2(20) JOBCODE CHAR(2) STARTDATE DATE SALARY NUMBER(9,2) BONUS NUMBER(5) PL/SQL SQL> edit pay_two SQL> @ pay_two Stephen York CM 42000 PL/SQL procedure successfully completed. SET SERVEROUTPUT ON DECLARE v_name first_pay.name%TYPE; v_jobcode first_pay.jobcode%TYPE; v_salary first_pay.salary%TYPE; BEGIN SELECT name, jobcode, salary INTO v_name, v_jobcode, v_salary FROM first_pay WHERE name = 'Stephen York'; DBMS_OUTPUT.PUT_LINE (v_name || ' ' || v_jobcode || ' ' || v_salary); END; / SET SERVEROUTPUT OFF This is pay_two. Instead of hard coding in the type I am saying go to the table first_pay and get the type of the name column, then the jobcode column and then the salary column. This makes the code work even if the type changes in the first_pay table. The output shows because of the DBMS_OUTPUT line.
8
SET SERVEROUTPUT ON DECLARE v_pay_id first_pay.pay_id%TYPE; v_name first_pay.name%TYPE; v_salary first_pay.salary%TYPE; v_input_id first_pay.pay_id%TYPE; BEGIN v_input_id := &user_input_id; SELECT pay_id, name, salary INTO v_pay_id, v_name, v_salary FROM first_pay WHERE pay_id = v_input_id; DBMS_OUTPUT.PUT_LINE (v_pay_id || ' ' || v_name || ' ' || v_salary); END; / SET SERVEROUTPUT OFF PL/SQL The %TYPE gets the type from the table and column named in the clause. The first three variables will hold the data retrieved from the table. They are filled with the INTO statement. The variable v_input_id is established to store the user input that will be entered when the code is executed. SQL> @ pay_in_one Enter value for user_input_id: 3333 old 7: v_input_id := &user_input_id; new 7: v_input_id := 3333; 3333 Susan Ash 25000 The & causes the prompt for user input when the procedure is executed. The prompt will ask for user_input_id. The WHERE clause compares the pay_id column on the table with the user input which was stored in v_input_id. When a match is found the data is transferred to the variables specified in the INTO clause. The DBMS_OUTPUT line takes the information from the variables and displays it on the screen.
9
PL/SQL PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 45000 2000 SQL> EDIT pay_in_one SQL> @ pay_in_one Enter value for user_input_id: 3333 old 7: v_input_id := &user_input_id; new 7: v_input_id := 3333; 3333 Susan Ash 25000 PL/SQL procedure successfully completed. SET SERVEROUTPUT ON DECLARE v_pay_id first_pay.pay_id%TYPE; v_name first_pay.name%TYPE; v_salary first_pay.salary%TYPE; v_input_id first_pay.pay_id%TYPE; BEGIN v_input_id := &user_input_id; SELECT pay_id, name, salary INTO v_pay_id, v_name, v_salary FROM first_pay WHERE pay_id = v_input_id; DBMS_OUTPUT.PUT_LINE (v_pay_id || ' ' || v_name || ' ' || v_salary); END; / SET SERVEROUTPUT OFF This is the code of pay_in_one as entered in the notepad editor. The user entered 3333 when prompted and the retrieved information was outputted.
10
PL/SQL SQL> @ pay_in_onea Enter value for user_input_pay_id: 4444 old 9: WHERE pay_id = &user_input_pay_id; new 9: WHERE pay_id = 4444; 4444 Stephen York 42000 PL/SQL procedure successfully completed. SQL> EDIT pay_in_onea SET SERVEROUTPUT ON DECLARE v_pay_id first_pay.pay_id%TYPE; v_name first_pay.name%TYPE; v_salary first_pay.salary%TYPE; BEGIN SELECT pay_id, name, salary INTO v_pay_id, v_name, v_salary FROM first_pay WHERE pay_id = &user_input_pay_id; DBMS_OUTPUT.PUT_LINE (v_pay_id || ' ' || v_name || ' ' || v_salary); END; / SET SERVEROUTPUT OFF The user input is requested in the WHERE clause of the select. A match is located in the table and the information is displayed.
11
SET SERVEROUTPUT ON ACCEPT user_input_id PROMPT 'Enter the payroll id#: ' DECLARE v_pay_id first_pay.pay_id%TYPE; v_name first_pay.name%TYPE; v_salary first_pay.salary%TYPE; BEGIN SELECT pay_id, name, salary INTO v_pay_id, v_name, v_salary FROM first_pay WHERE pay_id = &user_input_id; DBMS_OUTPUT.PUT_LINE (v_pay_id || ' ' || v_name || ' ' || v_salary); END; / SET SERVEROUTPUT OFF SQL> EDIT pay_in_two PL/SQL SQL> @ pay_in_two Enter the payroll id#: 6666 old 9: WHERE pay_id = &user_input_id; new 9: WHERE pay_id = 6666; 6666 Joanne Brown 48000 PL/SQL procedure successfully completed.
12
Nested blocks in PL/SQL SQL> edit nest1 SET SERVEROUTPUT ON DECLARE v_general VARCHAR2(30) := '***General contains DOG***'; BEGIN DBMS_OUTPUT.PUT_LINE('In outer begin - value v_general: ' || v_general); DECLARE v_inner_only VARCHAR2(30) :='***inner_only contains CAT***'; BEGIN DBMS_OUTPUT.PUT_LINE('In inner begin - value v_general: ' || v_general); DBMS_OUTPUT.PUT_LINE('In inner begin - value v_inner_only: ' || v_inner_only); END; DBMS_OUTPUT.PUT_LINE('AFTER END on inner'); DBMS_OUTPUT.PUT_LINE('In outer begin - value v_general: ' || v_general); END; / SET SERVEROUTPUT OFF Code in nest1. Outer block in red. Inner block in orange. SQL> @ nest1 In outer begin - value v_general: ***General contains DOG*** In inner begin - value v_general: ***General contains DOG*** In inner begin - value v_inner_only: ***inner_only contains CAT*** AFTER END on inner In outer begin - value v_general: ***General contains DOG***
13
SET SERVEROUTPUT ON DECLARE v_general VARCHAR2(40) := '***General contains DOG***'; BEGIN DBMS_OUTPUT.PUT_LINE('In outer begin - value v_general: ' || v_general); DECLARE v_inner_only VARCHAR2(40) :='***inner_only contains CAT***'; BEGIN DBMS_OUTPUT.PUT_LINE('In inner begin - value v_general: ' || v_general); DBMS_OUTPUT.PUT_LINE('In inner begin - value v_inner_only: ' || v_inner_only); v_general := '***Changed general in inner - HORSE***'; DBMS_OUTPUT.PUT_LINE('In inner begin - value v_general: ' || v_general); END; DBMS_OUTPUT.PUT_LINE('AFTER END on inner'); DBMS_OUTPUT.PUT_LINE('In outer begin - value v_general: ' || v_general); END; / SET SERVEROUTPUT OFF Nested blocks SQL> edit nest2 SQL> @nest2 In outer begin - value v_general: ***General contains DOG*** In inner begin - value v_general: ***General contains DOG*** In inner begin - value v_inner_only: ***inner_only contains CAT*** In inner begin - value v_general: ***Changed general in inner - HORSE*** AFTER END on inner In outer begin - value v_general: ***Changed general in inner - HORSE*** Note the change to v_general in the inner block (shown shadowed).
14
SET SERVEROUTPUT ON DECLARE v_general VARCHAR2(40) := '***General contains DOG***'; BEGIN DBMS_OUTPUT.PUT_LINE('In outer begin - value v_general: ' || v_general); DECLARE v_inner_only VARCHAR2(40) :='***inner_only contains CAT***'; BEGIN DBMS_OUTPUT.PUT_LINE('In inner begin - value v_general: ' || v_general); DBMS_OUTPUT.PUT_LINE('In inner begin - value v_inner_only: ' || v_inner_only); v_general := '***Changed general in inner - HORSE***'; DBMS_OUTPUT.PUT_LINE('In inner begin - value v_general: ' || v_general); END; DBMS_OUTPUT.PUT_LINE('AFTER END on inner'); DBMS_OUTPUT.PUT_LINE('In outer begin - value v_general: ' || v_general); DBMS_OUTPUT.PUT_LINE('In outer begin - value v_inner_only: ' || v_inner_only); END; / SET SERVEROUTPUT OFF Nested blocks SQL> edit nest3 SQL> @nest3 DECLARE * ERROR at line 1: ORA-06550: line 15, column 70: PLS-00201: identifier 'V_INNER_ONLY' must be declared ORA-06550: line 15, column 5: PL/SQL: Statement ignored The attempt to use v_inner_only which was defined in the inner block caused the error (see shadowed lines).
15
SET SERVEROUTPUT ON DECLARE v_general VARCHAR2(40) := '***General contains DOG***'; v_passed_to VARCHAR2(40); BEGIN DBMS_OUTPUT.PUT_LINE('In outer begin - value v_general: ' || v_general); DECLARE v_inner_only VARCHAR2(40) :='***inner_only contains CAT***'; BEGIN DBMS_OUTPUT.PUT_LINE('In inner begin - value v_general: ' || v_general); DBMS_OUTPUT.PUT_LINE('In inner begin - value v_inner_only: ' || v_inner_only); v_general := '***Changed general in inner - HORSE***'; DBMS_OUTPUT.PUT_LINE('In inner begin - value v_general: ' || v_general); v_passed_to := v_inner_only; DBMS_OUTPUT.PUT_LINE('In inner begin - value v_passed_to: ' || v_passed_to); END; DBMS_OUTPUT.PUT_LINE('AFTER END on inner'); DBMS_OUTPUT.PUT_LINE('In outer begin - value v_general: ' || v_general); DBMS_OUTPUT.PUT_LINE('In outer begin - value v_passed_to: ' || v_passed_to); END; / SET SERVEROUTPUT OFF SQL> @ nest4 In outer begin - value v_general: ***General contains DOG*** In inner begin - value v_general: ***General contains DOG*** In inner begin - value v_inner_only: ***inner_only contains CAT*** In inner begin - value v_general: ***Changed general in inner - HORSE*** In inner begin - value v_passed_to: ***inner_only contains CAT*** AFTER END on inner In outer begin - value v_general: ***Changed general in inner - HORSE*** In outer begin - value v_passed_to: ***inner_only contains CAT*** PL/SQL procedure successfully completed.
16
Comments SET SERVEROUTPUT ON DECLARE v_general VARCHAR2(40) := '***General contains DOG***'; v_passed_to VARCHAR2(40); --This defined a field to receive data BEGIN DBMS_OUTPUT.PUT_LINE('In outer begin - value v_general: ' || v_general); DECLARE v_inner_only VARCHAR2(40) :='***inner_only contains CAT***'; BEGIN DBMS_OUTPUT.PUT_LINE('In inner begin - value v_general: ' || v_general); DBMS_OUTPUT.PUT_LINE('In inner begin - value v_inner_only: ' || v_inner_only); v_general := '***Changed general in inner - HORSE***'; DBMS_OUTPUT.PUT_LINE('In inner begin - value v_general: ' || v_general); v_passed_to := v_inner_only; /* In the inner block, I am passing the contents of v_inner_only which was defined in the inner block to v_passed to which was defined in the outer block. The information in v_passed_to which now contains the information from v_inner_only can now be displayed in the outer block. */ DBMS_OUTPUT.PUT_LINE('In inner begin - value v_passed_to: ' || v_passed_to); END; DBMS_OUTPUT.PUT_LINE('AFTER END on inner'); DBMS_OUTPUT.PUT_LINE('In outer begin - value v_general: ' || v_general); DBMS_OUTPUT.PUT_LINE('In outer begin - value v_passed_to: ' || v_passed_to); END; / SET SERVEROUTPUT OFF Single line comments can be used on the line that contains a command. They start with -- Multiple line comments are surrounded with /*….*/ as shown below.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.