Upload
daniela-lutic
View
217
Download
0
Embed Size (px)
Citation preview
8/12/2019 Ora Instructiuni PL SQL
1/6
-1
Copyright Oracle Corporation, 1998. All rights reserved.
1717
Writing Executable StatementsWriting Executable Statements
17-2 Copyright Oracle Corporation, 1998. All rights reserved.
ObjectivesObjectivesAfter completing this lesson, you should beable to do the following:
Recognize the significance of theexecutable section
Write statements within the executablesection
Describe the rules of nested blocks
Execute and test a PL/SQL block
Use coding conventions
After completing this lesson, you should beAfter completing this lesson, you should beable to do the following:able to do the following:
Recognize the significance of theexecutable section
Write statements within the executablesection
Describe the rules of nested blocks
Execute and test a PL/SQL block
Use coding conventions
17-3 Copyright Oracle Corporation, 1998. All rights reserved.
PL/SQL Block Syntax and
Guidelines
PL/SQL Block Syntax and
Guidelines
Statements can continue over several lines.
Lexical units can be separated by spaces:
Delimiters Identifiers
Literals
Comments
Statements can continue over several lines.
Lexical units can be separated by spaces:
Delimiters
Identifiers
Literals
Comments
17-4 Copyright Oracle Corporation, 1998. All rights reserved.
PL/SQL Block Syntax and
Guidelines
PL/SQL Block Syntax and
Guidelines
Identifiers
Can contain up to 30 characters
Cannot contain reserved words unlessenclosed in double quotation marks
Must begin with an alphabetic character
Should not have the same name as adatabase table column name
IdentifiersIdentifiers
Can contain up to 30 characters
Cannot contain reserved words unlessenclosed in double quotation marks
Must begin with an alphabetic character
Should not have the same name as adatabase table column name
8/12/2019 Ora Instructiuni PL SQL
2/6
-2
17-5 Copyright Oracle Corporation, 1998. All rights reserved.
PL/SQL Block Syntax andGuidelinesPL/SQL Block Syntax andGuidelines
Literals
Character and date literals must beenclosed in single quotation marks.
Numbers can be simple values or scientific
notation.
LiteralsLiterals
Character and date literals must beenclosed in single quotation marks.
Numbers can be simple values or scientific
notation.
v_ename := 'Henderson';
v_ename := 'Henderson';
17-6 Copyright Oracle Corporation, 1998. All rights reserved.
Commenting CodeCommenting Code Prefix single-line comments with two dashes
(- -).
Place multi-line comments between thesymbols /* and */.
Example
Prefix single-line comments with two dashes(- -).
Place multi-line comments between thesymbols /* and */.
ExampleExample
...v_sal NUMBER (9,2);
BEGIN/* Compute the annual salary based on themonthly salary input from the user */
v_sal := v_sal * 12;END; -- This is the end of the transaction
...v_sal NUMBER (9,2);
BEGIN/* Compute the annual salary based on themonthly salary input from the user */
v_sal := v_sal * 12;END; -- This is the end of the transaction
17-7 Copyright Oracle Corporation, 1998. All rights reserved.
SQL Functions in PL/SQLSQL Functions in PL/SQL
Available:
Single-row number
Single-row character
Datatype conversion
Date
Not available:
GREATEST
LEAST
DECODE
Group functions
Available:
Single-row number
Single-row character
Datatype conversion
Date
Not available:
GREATEST
LEAST
DECODE
Group functions
Same as in SQL
}}17-8 Copyright Oracle Corporation, 1998. All rights reserved.
PL/SQL FunctionsPL/SQL Functions
Examples
Build the mailing list for a company.
Convert the employee name to lowercase.
ExamplesExamples
Build the mailing list for a company.
Convert the employee name to lowercase.
v_mailing_address := v_name||CHR(10)||
v_address||CHR(10)||v_state||CHR(10)||v_zip;
v_mailing_address := v_name||CHR(10)||
v_address||CHR(10)||v_state||
CHR(10)||v_zip;
v_ename := LOWER(v_ename);
v_ename := LOWER(v_ename);
8/12/2019 Ora Instructiuni PL SQL
3/6
-3
17-9 Copyright Oracle Corporation, 1998. All rights reserved.
Datatype ConversionDatatype Conversion
Convert data to comparable datatypes.
Mixed datatypes can result in an error andaffect performance.
Conversion functions:
TO_CHAR
TO_DATE
TO_NUMBER
Convert data to comparable datatypes.
Mixed datatypes can result in an error andaffect performance.
Conversion functions:
TO_CHAR
TO_DATE
TO_NUMBER
BEGIN
SELECT TO_CHAR(hiredate,
'MON. DD, YYYY')
FROM emp;
END;
BEGIN
SELECT TO_CHAR(hiredate,
'MON. DD, YYYY')FROM emp;
END;
17-10 Copyright Oracle Corporation, 1998. All rights reserved.
Datatype ConversionDatatype Conversion
This statement produces a compile error.This statement produces a compile error.This statement produces a compile error.
v_comment := USER||': '||SYSDATE;
v_comment := USER||': '||SYSDATE;
v_comment := USER||': '||TO_CHAR(SYSDATE);
v_comment := USER||': '||TO_CHAR(SYSDATE);
To correct the error, the TO_CHARconversion function is used.
To correct the error, the TO_CHARconversion function is used.
17-11 Copyright Oracle Corporation, 1998. All rights reserved.
Nested Blocks and Variable ScopeNested Blocks and Variable Scope
Statements can be nested wherever anexecutable statement is allowed.
A nested block becomes a statement.
An exception section can contain nestedblocks.
The scope of an object is the region of theprogram that can refer to the object.
Statements can be nested wherever anexecutable statement is allowed.
A nested block becomes a statement.
An exception section can contain nestedblocks.
The scope of an object is the region of theprogram that can refer to the object.
17-12 Copyright Oracle Corporation, 1998. All rights reserved.
Nested Blocks and Variable ScopeNested Blocks and Variable Scope
An identifier is visible in the regions in whichyou can reference the unqualified identifier:
A block can look up to the enclosing block.
A block cannot look down to enclosedblocks.
An identifier is visible in the regions in whichAn identifier is visible in the regions in which
you can reference the unqualified identifier:you can reference the unqualified identifier:
A block can look up to the enclosing block.
A block cannot look down to enclosedblocks.
8/12/2019 Ora Instructiuni PL SQL
4/6
-4
17-13 Copyright Oracle Corporation, 1998. All rights reserved.
Nested Blocks and Variable ScopeNested Blocks and Variable Scope
...x BINARY_INTEGER;
BEGIN...
DECLAREy NUMBER;
BEGIN...
END;...
END;
...x BINARY_INTEGER;
BEGIN...
DECLAREy NUMBER;
BEGIN...
END;...
END;
Scope of x
Scope of y
ExampleExample
17-14 Copyright Oracle Corporation, 1998. All rights reserved.
Operators in PL/SQLOperators in PL/SQL
Logical
Arithmetic
Concatenation
Parentheses to controlorder of operations
Exponential operator (**)
Logical
Arithmetic
Concatenation
Parentheses to controlorder of operations
Exponential operator (**)
Same as in
SQL
}}
17-15 Copyright Oracle Corporation, 1998. All rights reserved.
Examples
Increment the index for a loop.
Set the value of a Boolean flag.
Validate an employee number if it containsa value.
ExamplesExamples
Increment the index for a loop.
Set the value of a Boolean flag.
Validate an employee number if it containsa value.
Operators in PL/SQLOperators in PL/SQL
v_count := v_count + 1;v_count := v_count + 1;
v_equal := (v_n1 = v_n2);v_equal := (v_n1 = v_n2);
v_valid := (v_empno IS NOT NULL);v_valid := (v_empno IS NOT NULL);
17-16 Copyright Oracle Corporation, 1998. All rights reserved.
Using Bind VariablesUsing Bind Variables
To reference a bind variable in PL/SQL, you
must prefix its name with a colon (:).
Example
To reference a bind variable in PL/SQL, youTo reference a bind variable in PL/SQL, you
must prefix its name with a colon (:).must prefix its name with a colon (:).
ExampleExampleDECLARE
v_sal emp.sal%TYPE;BEGIN
SELECT salINTO v_salFROM empWHERE empno = 7369;:salary := v_sal;
END;
DECLAREv_sal emp.sal%TYPE;
BEGINSELECT salINTO v_salFROM empWHERE empno = 7369;:salary := v_sal;
END;
8/12/2019 Ora Instructiuni PL SQL
5/6
-5
17-17 Copyright Oracle Corporation, 1998. All rights reserved.
Programming GuidelinesProgramming Guidelines
Make code maintenance easier by:
Documenting code with comments
Developing a case convention for the code
Developing naming conventions foridentifiers and other objects
Enhancing readability by indenting
Make code maintenance easier by:Make code maintenance easier by:
Documenting code with comments
Developing a case convention for the code
Developing naming conventions foridentifiers and other objects
Enhancing readability by indenting
17-18 Copyright Oracle Corporation, 1998. All rights reserved.
Code Naming ConventionsCode Naming Conventions
Avoid ambiguity:
The names of local variables and formalparameters take precedence over thenames of database tables.
The names of columns take precedence
over the names of local variables.
Avoid ambiguity:Avoid ambiguity:
The names of local variables and formalparameters take precedence over thenames of database tables.
The names of columns take precedenceover the names of local variables.
17-19 Copyright Oracle Corporation, 1998. All rights reserved.
Indenting CodeIndenting Code
For clarity, indent each level of code.
Example
For clarity, indent each level of code.For clarity, indent each level of code.
ExampleExample
BEGIN
IF x=0 THENy=1;
END IF;
END;
BEGIN
IF x=0 THEN
y=1;
END IF;
END;
DECLARE
v_detpno NUMBER(2);
v_location VARCHAR2(13);
BEGINSELECT deptno,
location
INTO v_deptno,v_location
FROM deptWHERE dname = 'SALES';
...END;
DECLARE
v_detpno NUMBER(2);
v_location VARCHAR2(13);
BEGIN
SELECT deptno,
location
INTO v_deptno,v_location
FROM deptWHERE dname = 'SALES';
...END;
17-20 Copyright Oracle Corporation, 1998. All rights reserved.
Determine Variable ScopeDetermine Variable Scope
Class ExerciseClass ExerciseClass Exercise
...DECLAREV_SAL NUMBER(7,2) := 60000;V_COMM NUMBER(7,2) := V_SAL / .20;
V_MESSAGE VARCHAR2(255) := ' eligible for commission';BEGIN ...
DECLAREV_SAL NUMBER(7,2) := 50000;V_COMM NUMBER(7,2) := 0;V_TOTAL_COMP NUMBER(7,2) := V_SAL + V_COMM;
BEGIN ...V_MESSAGE := 'CLERK not'||V_MESSAGE;END;
V_MESSAGE := 'SALESMAN'||V_MESSAGE;
END;
...DECLAREV_SAL NUMBER(7,2) := 60000;V_COMM NUMBER(7,2) := V_SAL / .20;
V_MESSAGE VARCHAR2(255) := ' eligible for commission';BEGIN ...
DECLAREV_SAL NUMBER(7,2) := 50000;V_COMM NUMBER(7,2) := 0;V_TOTAL_COMP NUMBER(7,2) := V_SAL + V_COMM;BEGIN ...
V_MESSAGE := 'CLERK not'||V_MESSAGE;END;
V_MESSAGE := 'SALESMAN'||V_MESSAGE;END;
8/12/2019 Ora Instructiuni PL SQL
6/6
-6
17-21 Copyright Oracle Corporation, 1998. All rights reserved.
SummarySummary PL/SQL block structure:
Nesting blocks and scoping rules
PL/SQL programming:
Functions
Datatype conversions
Operators Bind variables
Conventions and guidelines
PL/SQL block structure:
Nesting blocks and scoping rules
PL/SQL programming:
Functions
Datatype conversions
Operators Bind variables
Conventions and guidelines
DECLAREDECLARE
BEGINBEGIN
EXCEPTIONEXCEPTION
END;END;
17-22 Copyright Oracle Corporation, 1998. All rights reserved.
Practice OverviewPractice Overview
Reviewing scoping and nesting rules
Developing and testing PL/SQL blocks
Reviewing scoping and nesting rules
Developing and testing PL/SQL blocks