oracle slides02 fp2005 ver 1.0

115 434 0
oracle  slides02  fp2005  ver 1.0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

PL/SQL Oracle Day 2 2 I-2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 1.0 Objectives • To explain the need for a procedural language and the constructs of PL/SQL. • To explain Exception/Error handling in PL/SQL blocks such as Named and Un-named System Exceptions, User defined Exceptions 3 I-3 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 1.0 About PL/SQL • PL/SQL is the procedural extension to SQL with design features of programming languages. • Data manipulation and query statements of SQL are included within procedural units of code. 4 I-4 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 1.0 PL/SQL block PL/SQL engine Oracle server Procedural statement executor PL/SQL SQL SQL statement executor PL/SQL block PL/SQL Environment 5 I-5 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 1.0 Application Oracle server Shared library Integration Benefits of PL/SQL 1/4 6 I-6 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 1.0 Application Other DBMSs Application Oracle with PL/SQL SQL SQL SQL SQL SQL IF THEN SQL ELSE SQL END IF; SQL Improved performance Benefits of PL/SQL 2/4 7 I-7 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 1.0 DECLARE BEGIN END; EXCEPTION Benefits of PL/SQL 3/4 Modularize program development 8 I-8 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 1.0 PL/SQL is portable • You can declare variables • You can program with procedural language control structures. • PL/SQL can handle errors. Benefits of PL/SQL 4/4 9 I-9 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 1.0 DECLARE (Optional) Variables, cursors, user-defined exceptions BEGIN (Mandatory) – SQL statements – PL/SQL statements EXCEPTION (Optional) Actions to perform when errors occur END; (Mandatory) END; PL/SQL Block Structure 10 I-10 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 1.0 DECLARE var1 VARCHAR2(5); BEGIN SELECT column_name INTO var1 FROM t1; EXCEPTION WHEN exception THEN END; Executing Statements and PL/SQL Blocks [...]... I-23 Copyright © 2005, Infosys Technologies Ltd 23 ER/CORP/CRS/DB25/003 Version No 1.0 LOB Data Type Variables CLOB BLOB BFILE I-24 Copyright © 2005, Infosys Technologies Ltd 24 ER/CORP/CRS/DB25/003 Version No 1.0 Bind Variables Bind variable Server I-25 Copyright © 2005, Infosys Technologies Ltd 25 ER/CORP/CRS/DB25/003 Version No 1.0 Using Bind Variables To reference a bind variable in PL/SQL, you must... SQL*Plus with SET SERVEROUTPUT ON SET SERVEROUTPUT ON DEFINE p_annsal = 120000 DECLARE v_salary NUMBER(9,2) := &p_annsal; BEGIN v_salary := v_salary/12; DBMS_OUTPUT.PUT_LINE (‘Monthly salary is ‘ ||TO_CHAR(v_salary)); END; / I-28 Copyright © 2005, Infosys Technologies Ltd 28 ER/CORP/CRS/DB25/003 Version No 1.0 PL/SQL Block Syntax and Guidelines • • I-29 Statements can continue over several lines Lexical... v_name||CHR(10)|| v_address||CHR(10)||v_state|| CHR(10)||v_pin; •Convert the employee name to lowercase v_name := LOWER(v_name); I-34 Copyright © 2005, Infosys Technologies Ltd 34 ER/CORP/CRS/DB25/003 Version No 1.0 Data Type Conversion •Convert data to comparable data types •Mixed data types can result in an error and affect performance •Conversion functions: –TO_CHAR –TO_DATE –TO_NUMBER DECLARE v_bdate... ER/CORP/CRS/DB25/003 Version No 1.0 SQL Functions in PL/SQL •Available in procedural statements: –Single-row number –Single-row character –Data type conversion –Date –Timestamp –GREATEST and LEAST –Miscellaneous functions •Not available in procedural statements: –DECODE –Group functions I-33 Copyright © 2005, Infosys Technologies Ltd Similar to SQL 33 ER/CORP/CRS/DB25/003 Version No 1.0 SQL Functions... Technologies Ltd 26 ER/CORP/CRS/DB25/003 Version No 1.0 Referencing Non-PL/SQL Variables Store the annual salary into a SQL*Plus host variable :monthly_salary := v_salary / 12; •Reference non-PL/SQL variables as host variables •Prefix the references with a colon (:) I-27 Copyright © 2005, Infosys Technologies Ltd 27 ER/CORP/CRS/DB25/003 Version No 1.0 DBMS_OUTPUT.PUT_LINE •An Oracle- supplied packaged procedure... BEGIN I-35 Copyright © 2005, Infosys Technologies Ltd 35 ER/CORP/CRS/DB25/003 Version No 1.0 Data Type Conversion This statement produces a compilation error if the variable v_date is declared as a DATE data type v_bdate := ‘September 15, 2000'; I-36 Copyright © 2005, Infosys Technologies Ltd 36 ER/CORP/CRS/DB25/003 Version No 1.0 ... name IS END; END; Copyright © 2005, Infosys Technologies Ltd 11 ER/CORP/CRS/DB25/003 Version No 1.0 Use of Variables Variables can be used for: •Temporary storage of data •Manipulation of stored values •Reusability •Ease of maintenance I-12 Copyright © 2005, Infosys Technologies Ltd 12 ER/CORP/CRS/DB25/003 Version No 1.0 Handling Variables in PL/SQL Declare and initialize variables in the declaration... output variables I-13 Copyright © 2005, Infosys Technologies Ltd 13 ER/CORP/CRS/DB25/003 Version No 1.0 Types of Variables •PL/SQL variables: –Scalar –Composite –Reference –LOB (large objects) •Non-PL/SQL variables: Bind and host variables I-14 Copyright © 2005, Infosys Technologies Ltd 14 ER/CORP/CRS/DB25/003 Version No 1.0 Declaring PL/SQL Variables Syntax: identifier [CONSTANT] datatype [NOT NULL] [:=... ER/CORP/CRS/DB25/003 Version No 1.0 Guidelines for Declaring PL/SQL Variables •Follow naming conventions •Initialize variables designated as NOT NULL and CONSTANT •Declare one identifier per line •Initialize identifiers by using the assignment operator (:=) or the DEFAULT reserved word identifier := expression; I-16 Copyright © 2005, Infosys Technologies Ltd 16 ER/CORP/CRS/DB25/003 Version No 1.0 Naming Rules... PL/SQL identifiers: e.g.: v_empno 17 ER/CORP/CRS/DB25/003 Version No 1.0 Variable Initialization and Keywords •Assignment operator (:=) •DEFAULT keyword •NOT NULL constraint Syntax: identifier := expression; Examples: v_joindate := '01-JAN-2004'; v_name := ‘Swetalina'; I-18 Copyright © 2005, Infosys Technologies Ltd 18 ER/CORP/CRS/DB25/003 Version No 1.0 Scalar Data Types •CHAR [(maximum_length)] •VARCHAR2 . Block Structure 10 I - 10 Copyright © 200 5, Infosys Technologies Ltd ER/CORP/CRS/DB25 /00 3 Version No. 1. 0 DECLARE var1 VARCHAR2(5); BEGIN SELECT column_name INTO var1 FROM t1; EXCEPTION WHEN. the block. Naming Rules 18 I -18 Copyright © 200 5, Infosys Technologies Ltd ER/CORP/CRS/DB25 /00 3 Version No. 1. 0 identifier := expression; v_joindate := ' 01 - JAN- 200 4'; v_name := ‘Swetalina'; • Assignment. VARCHAR2 (13 ) := ‘Berhampur’; c_incentive CONSTANT NUMBER := 10 0; Syntax: Examples: Declaring PL/SQL Variables 16 I -16 Copyright © 200 5, Infosys Technologies Ltd ER/CORP/CRS/DB25 /00 3 Version No. 1. 0 identifier

Ngày đăng: 18/04/2014, 10:25

Mục lục

  • PL/SQL Oracle Day 2

  • Slide 2

  • Slide 3

  • Slide 4

  • Slide 5

  • Slide 6

  • Slide 7

  • Slide 8

  • Slide 9

  • Slide 10

  • Slide 11

  • Slide 12

  • Slide 13

  • Slide 14

  • Slide 15

  • Slide 16

  • Slide 17

  • Slide 18

  • Slide 19

  • Slide 20

Tài liệu cùng người dùng

Tài liệu liên quan