plsql11
plsql11
www.starthack.com
5. ISO and ANSI has published standards for SQL implementation Join effort by ISO and ANSI has lead to standard version of SQL is called SQL/86(SQL1), SQL/92(SQL2)
www.starthack.com
INTRODUCTION TO PL/SQL
1. PL/SQL stands for Procedural Language/Structured Query Language, and it is
an extension of the SQL Language 2. It is superset of the Structured Query Language specialized for use in the oracle database. 3. Because it is procedural language, it eliminates many restriction of the SQL language
4. With the use of SQL user can only perform basic operations such as selecting the information from some prefabricated tables, inserting information into those tables, updating the information stored into tables and also used to delete information from these tables
5. PL/SQL extends SQL by adding control structure found in the other procedural languages 6. PL/SQL is the combination of SQLs languages ease of data manipulation and the procedural languages ease of programming.
www.starthack.com
7. With PL/SQL, we can use SQL statements to manipulate ORACLE data and the flow of control statements to process the data. Moreover, we can declare constants and variables, define subprograms (procedures and functions). Thus, PL/SQL combines the data manipulating power of SQL with the data processing power of procedural languages
8. While PL/SQL is just like any other programming language, it has syntax and rules that determine how programming statements work together. It is important for you to realize that PL/SQL is not a stand-alone programming language
9. PL/SQL is a part of the Oracle RDBMS, and it can reside in two environments, the client and the server 10. As a result, it is very easy to move PL/SQL modules between server-side and client-side applications 11. When the PL/SQL engine is located on the server, the whole PL/SQL block is passed to the PL/SQL engine on the Oracle server.
www.starthack.com
When the PL/SQL engine is located on the client, as it is in the Oracle Developer Tools, the PL/SQL processing is done on the client side. All SQL statements that are embedded within the PL/SQL block are sent to the Oracle server for further processing. When PL/SQL block contains no SQL statement, the entire block is executed on the client side.
SQL VS PL/SQL
SQL
SQL does not have any procedural capabilities SQL statements are passed to oracle engine(server) one at a time..Therefore each time for each statement a call is made to the server resources and these resources are opened and closed every time Due to this, Generating network traffic resulting in slow processing.
PL/SQL
ORACLE has provides all procedural capabilities in PL/SQL to support data filtration In PL/SQL it sends the bundle of SQL statements to the oracle server in the form of BLOCK and hence calling the server resources only once for that block even if that block is having more than one SQL statements After processing all the statements in a block ORACLE server closer the resources results in faster execution of SQL statements in a PL/SQL block. In PL/SQL, we can program the block of statements to handle the errors in such a way that if any of the statement fails to execute then we can display user friendly appropriate message.
In SQL, there is no provision for handling errors and exception, which means that if any SQL statements fails to execute, then oracle gives its own error message and error code many not be user friendly
SQL does not support PL/SQL statements We cannot store the intermediate results of a query in variable
PL/SQL supports SQL statements in its block PL/SQL supports declaring the variables so as to store intermediate results for later use
ADVANTAGES OF PL/SQL
Supports the declaration and manipulation of object types and collection Allowing the calling of external function and procedure Contains new libraries of built-in packages.A package is a file that group functions, cursors, stored procedures and variables in one place
TRIGGERS: Trigger is a PL/SQL program that is stored in the database and executed immediately before or after the INSERT, UPDATE, and DELETE command CURSORS: Oracle uses workspaces to executes the SQL commands. Through PL/SQL it is possible to name the workspace and access its information PL/SQL allow us to use all the SQL data manipulation commands, transection control commands, SQL functions and operators, thus allowing us to manipulates data values in a table more flexibly and effectively
DECLARATION SECTION
The declaration section is the first section of the PL/SQL block.. It contains definitions of PL/SQL identifiers such as variables, constants, cursors and so on..
Example
DECLARE v_first_name VARCHAR2(35) ; v_last_name VARCHAR2(35) ; v_counter NUMBER := 0 ;
EXECUTABLE SECTION
The executable section is the next section of the PL/SQL block. This section contains executable statements that allow you to manipulate the variables that have been declared in the declaration section. BEGIN SELECT first_name, last_name INTO v_first_name, v_last_name FROM student WHERE student_id = 123 ; DBMS_OUTPUT.PUT_LINE (Student name : || v_first_name || || v_last_name); END;
EXCEPTION-HANDLING SECTION
The exception-handling section is the last section of the PL/SQL block. This section contains statements that are executed when a runtime error occurs within a block. Runtime errors occur while the program is running and cannot be detected by the PL/SQL compiler.
EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE ( There is no student with student id 123 ); END;
EXAMPLE
Set serveroutput on DECLARE v_first_name VARCHAR2(35); v_last_name VARCHAR2(35); BEGIN SELECT first_name, last_name INTO v_first_name, v_last_name FROM student WHERE student_id = 123; DBMS_OUTPUT.PUT_LINE ('Student name: '||v_first_name||' '||v_last_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE ('There is no student with student id 123'); END ;
Identifiers
Literals
Comments
Expression and Comparisons
EXPRESSION OPERATORS :: Used to create assignment, range and string concatenation expression.
Example :Assignment Operator :: A:=2 Range Operator :: 1..4 Concatenate :: || ( dav||college)
Conditional Control :- It allows testing the truth of a condition and executing section of program depending upon the condition that may be true or false..
Iterative Control :- It allows executing a section of program repeatedly as long as a specified condition remain true.. Sequential Control :- It allows ordering the sequence of processing section of program..
CONDITIONAL CONTROL
IF THEN Statement IF THEN ELSE Statement
IF THEN Statement :: Syntax IF condition THEN Sequence_of_statements; END IF; Example :IF a>b THEN dbms_output.put_lines( a is greater); END IF;
IF THEN ELSE :: Syntax IF condition THEN Sequence_of_statements1; ELSE; Sequence_of_statements2; END IF;
TRUE
IF Condition
FALSE
THEN action
ELSE action
EXAMPLE
To illustrate of IF-ELSE construct PL/SQL block to find greate of two numbers. User will enter any two numbers and IF statement will check for the greater among the entered number.
SOLUTION :-
BEGIN
IF A > B THEN DBMS_OUTPUT.PUT_LINE(A IS GREATER); ELSE DBMS_OUTPUT.PUT_LINE(B IS GREATER); END IF; END;
IF THEN ELSEIF :: The third form of IF statement uses the keyword ELSIF (not ELSEIF) to introduce additional Condition, as follows
SYNTAX IF condition1 THEN Sequence_of_statements1; ELSIF condition2 THEN Sequence_of_statements2; ELSE Sequence_of_statements3; END IF;