Oracle Application Server 10g Forms and Reports Services 10g (9.0.4)
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to...
-
Upload
primrose-mcbride -
Category
Documents
-
view
240 -
download
0
Transcript of Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to...
Guide to Oracle 10g
ITBIS373 Database Development
Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data
Guide to Oracle 10g 2
Lesson A Objectives
After completing this lesson, you should be able to:
Describe the fundamentals of the PL/SQL programming language
Write and execute PL/SQL programs in SQL*Plus
Execute PL/SQL data type conversion functions
Guide to Oracle 10g 3
Display output through PL/SQL programs Manipulate character strings in PL/SQL
programs Debug PL/SQL programs
Guide to Oracle 10g 4
Fundamentals of PL/SQL
Full-featured programming language Execute using Oracle 10g utilities
SQL*Plus Forms Builder
Interpreted language Semicolon ends each command Reserved words
Guide to Oracle 10g 5
A procedural programming language is a programming language that uses detailed, sequential instructions to process data.
A PL/SQL program combines SQL queries with procedural commands for task such as manipulating variable values, evaluating IF/THEN decision control structures, and creating loop structure that repeat instructions multiple times until the loop reaches an exit condition.
Guide to Oracle 10g 6
PL/SQL interface directly with the Oracle 10g database and is available within Oracle development environments.
Another advantage of using PL/SQL to create programs that interface with Oracle 10g database is that database developers can store PL/SQL programs directly in the database, which makes the programs valuable to all database users.
Guide to Oracle 10g 7
PL/SQL Variables and Data Types
Variable names must follow the Oracle naming standard
Strongly typed language Explicitly declare each variable including data
type before using variable Variable declaration syntax:
variable_name data_type_declaration; Default value always NULL
Guide to Oracle 10g 8
Scalar Variables
Reference single value Data types correspond to Oracle 10g
database data types VARCHAR2 CHAR DATE NUMBER
Guide to Oracle 10g 9
Guide to Oracle 10g 10
General Scalar Data TypesPL/SQL has general scalar data types that do not correspond to database data types.
Guide to Oracle 10g 11
Composite Variables
Data object made up of multiple individual data elements
Data structure contains multiple scalar variables Types:
RECORD, which specifies a structure that contains multiple scalar values, and is similar to a table record.
TABLE, which specifies a tabular structure with multiple columns and rows.
VARRAY, which specifies a variable-sized array. A variable-sized array is a tabular structure that can expand or contract based on the data values it contains.
Guide to Oracle 10g 12
Reference Variables
Directly reference specific database column or row and assume data type of associated column or row.
Guide to Oracle 10g 13
LOB Data type
PL/SQL LOB data types declare variables that reference binary data objects, such as image or sound. The LOB data type can store either binary or character data up to four gigabytes in size. The LOB values in PL/SQL programs must be manipulated using special set of programs, called the DBMS_LOB package.
Guide to Oracle 10g 14
PL/SQL Program Blocks
Declaration section Optional
Execution section Required
Exception section Optional
Comment statements Enclosed within /* and */
Guide to Oracle 10g 15
The structure of a PL/SQL program block is
Guide to Oracle 10g 16
PL/SQL Arithmetic Operators in Describing Order of Precedence
Guide to Oracle 10g 17
Assignment Statements
Operator :=
Syntax variable_name := value;Ex: Current_s_first_name := ‘Tammy’;
Guide to Oracle 10g 18
Recall that when you declare a new PL/SQL variable, the default value is NULL. You can place an assignment statement within a variable declaration to assign an initial value to a new variable.
Ex:cuurent_student_Id NUMBER :=100;
Or
cuurent_student_Id NUMBER DEFAULT 100;
Guide to Oracle 10g 19
If you use a NULL value in an assignment statement that perform an arithmetic operation, the resulting value is always NULL. DECLARE
variable1 NUMBER;
variable2 NUMBER :=0;
BEGIN
variable2:= variable1+1;
END;
Guide to Oracle 10g 20
Executing a PL/SQL Program in SQL*Plus
Create and execute PL/SQL program blocks Within variety of Oracle 10g development
environmentsAdd PL/SQL programs to your Forms BuilderReport Builder
Execute PL/SQL programs in SQL*plus.
Guide to Oracle 10g 21
Displaying PL/SQL Program Output in SQL*Plus
PL/SQL output buffer Memory area on database server Stores program’s output values before they are
displayed to user Should increase size
SET SERVEROUTPUT ON SIZE buffer_size Default buffer size 2000 bytes. SET SERVEROUTPUT ON SIZE 4000
Guide to Oracle 10g 22
Displaying PL/SQL Program Output in SQL*Plus (continued)
Display program output DBMS_OUTPUT.PUT_LINE('display_text'); Display maximum of 255 characters of text data
Guide to Oracle 10g 23
Writing a PL/SQL Program
Write PL/SQL program in Notepad or another text editor
Copy and paste program commands into SQL*Plus
Press Enter after last program command Type front slash ( / ) Then press Enter again
Guide to Oracle 10g 24
Guide to Oracle 10g 25
Writing a PL/SQL Program (continued)
Good programming practice Place DECLARE, BEGIN, and END commands
flush with left edge of text editor window Indent commands within each section
Guide to Oracle 10g 26
PL/SQL Program Commands
Guide to Oracle 10g 27
PL/SQL Data Conversion Functions
Implicit data conversions Interpreter automatically converts value from one
data type to another If PL/SQL interpreter unable to implicitly convert
value error occurs Explicit data conversions
Convert variables to different data types Using data conversion functions
Guide to Oracle 10g 28
PL/SQL Data Conversion Functions of PL/SQL
Guide to Oracle 10g 29
Manipulating Character Strings with PL/SQL
String Character data value Consists of one or more characters
Concatenating Joining two separate strings
Parse Separate single string consisting of two data
items separated by commas or spaces
Guide to Oracle 10g 30
Concatenating Character Strings
Operator ||
Syntax: new_string := string1 || string2;
Guide to Oracle 10g 31
Removing Blank Leading and Trailing Spaces from Strings
LTRIM function Remove blank leading spaces string := LTRIM(string_variable_name);
RTRIM function Remove blank trailing spaces string := RTRIM(string_variable_name);
Guide to Oracle 10g 32
Finding the Length of Character Strings
LENGTH function syntax string_length := LENGTH(string_variable_name);
Ex: You use the following command to declare an integer variable named code_length, and assign to it the string length:
Code-length as NUMBER(3) := LENGTH(bldg_code);
Guide to Oracle 10g 33
Character String Case Functions
Modify case of character strings Functions and syntax:
string := UPPER(string_variable_name);
string := LOWER(string_variable_name);
string := INITCAP(string_variable_name);
Guide to Oracle 10g 34
Guide to Oracle 10g 35
Parsing Character Strings
INSTR function Searches string for specific substring Syntax:
start_position := INSTR(original_string, substring);
SUBSTR function Extracts specific number of characters from
character string Starting at given point
Guide to Oracle 10g 36
Parsing Character Strings (continued)
SUBSTR function (continued) Syntax:
extracted_string := SUBSTR(string_variable, starting_point, number_of_characters);
Use INSTR to find delimiter
Guide to Oracle 10g 37
Guide to Oracle 10g 38
Debugging PL/SQL Programs
Syntax error Occurs when command does not follow
guidelines of programming language Generate compiler or interpreter error messages
Logic error Does not stop program from running Results in incorrect result
Guide to Oracle 10g 39
Program with a Syntax Error
Guide to Oracle 10g 40
Program with a Logic Error
Guide to Oracle 10g 41
Finding Syntax Errors
Often involve: Misspelling reserved word Omitting required character in command Using built-in function improperly
Interpreter Flags line number and character location of
syntax errors May actually be on preceding line
Displays error code and message
Guide to Oracle 10g 42
Finding Syntax Errors (continued)
Comment out program lines To find error
Cascading errors One syntax error can generate many more errors
Guide to Oracle 10g 43
Finding Logic Errors
Caused by: Not using proper order of operations in arithmetic
functions Passing incorrect parameter values to built-in
functions Creating loops that do not terminate properly Using data values that are out of range or not of
right data type
Guide to Oracle 10g 44
Finding Logic Errors (continued)
Debugger Program that enables software developers to
pause program execution and examine current variable values
SQL*Plus environment does not provide PL/SQL debugger
Use DBMS_OUTPUT to print variable values
Guide to Oracle 10g 45