Oracle: PLSQL Introduction

3,548 views

Published on

Oracle: PLSQL Introduction

Published in: Technology
1 Comment
5 Likes
Statistics
Notes
  • For Siebel CRM Online Training Register at http://www.todaycourses.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
3,548
On SlideShare
0
From Embeds
0
Number of Embeds
111
Actions
Shares
0
Downloads
0
Comments
1
Likes
5
Embeds 0
No embeds

No notes for slide

Oracle: PLSQL Introduction

  1. 1. 1<br />Using ORACLE®<br />Introduction to PL/SQL<br />
  2. 2. DATA DEFINATION LANGUAGE<br />Oracle Engine<br />PL/SQL ENGINE:<br />2<br />PL/SQL stands for Procedural Extension to Structured Query Language.<br />PL /SQL support is provided in Oracle from versions > 6;<br />PL/SQL is used to provide users with the ability to use variables, loops , function etc in the database.<br />SQL can be combined easily into the PL/SQL code .Since the PL/SQL program is processed by the PL/SQL Statement Executor ,it reduces the load on Oracle thus making it perform <br />faster.<br />
  3. 3. 3<br />Anonymous Block<br />PL/SQL uses a block structure hence making it easier to write and maintain code.<br />PL/SQL provides us with the following:<br />Variables and constants.<br />Loops and conditional constructs.<br />Functions and Procedure that enable code reusability.<br />Exception handling .<br />The Anonymous Block ,the basic PL/SQL code is as follows:<br /> <<label>><br /> [ DECLARE ]<br /> TYPE / item / FUNCTION / PROCEDURE declarations or variables.<br /> BEGIN <br /> Statements (both SQL and PL/SQL)<br /> [ EXCEPTION ]<br /> EXCEPTION handlers to handle the exception<br /> END label; <br />
  4. 4. 4<br />Anonymous Block<br />Let us write an example Anonymous block.<br />DECLARE<br />name varchar2[20];<br />BEGIN<br />name := ‘Larry’;<br />DBMS_OUTPUT.PUT_LINE(‘The name is : ‘ || name);<br />END<br />The above anonymous block print the name ‘Larry'. Key points to be highlighted here are:<br />The variable name must follow rules similar to that of declaring Tables in SQL and the data type with size be mentioned.<br />The := operator is the assignment operator in PL/SQL.<br />The DBMS_OUTPUT.PUT_LINE prints text enclosed in ( ) .Here we use a ‘||’ concatenation operator to join the string and variable.<br />
  5. 5. 5<br />Variables<br />The various data types in PL/SQL are :<br />LOB refers to large object. It could be of the type BINARY LARGE OBJECT (BLOB) or CHARACTER LARGE OBJECT (CLOB) to store image or audio in binary format (BLOB) or a text file (CLOB) up to the size of 4 GIGABYTES.<br />BFILE refers to an external file to be stored in a database.<br /> A variable must be declared in the “DECLARE” block as:<br /> VARIABLE_NAME DATATYPE[SIZE];<br /> A variable may be initialized when declared (otherwise set to NULL)<br /> VARIABLE_NAME DATATYPE[SIZE]:= VALUE;<br /> We may also define a variable to not hold null values.<br /> VARIABLE_NAME DATATYPE[SIZE] NOT NULL := VALUE;<br />
  6. 6. 6<br />%TYPE and %ROWTYPE <br />We can declare a variable to have a data type of any column or another variable using the <br />%TYPE attribute which is prefixed with the table and column or variable name.<br />SYNTAX:<br /> VARIABLE _NAME TABLE_NAME.COLUMN_NAME[VARIABLE_NAME]%TYPE ;<br />EXAMPLE:<br /> name InfoTable.name%TYPE;<br />The %ROWTYPE attribute is used to declare a variable to hold an entire row which is a collection of multiple columns of a table.<br />SYNTAX:<br /> VARIABLE _NAME TABLE_NAME %ROWTYPE<br />EXAMPLE:<br /> name InfoTable%ROWTYPE;<br />
  7. 7. 7<br />BIND/HOST VARIABLES<br />Bind variables are the variables that are created in the host environment hence also called as host variable. The features of a bind variable are:<br />Unlike normal variables the memory used by a bind variable is not cleared when the PL/SQL block execution is complete.<br />Can be accessed outside the block by other PL/SQL and SQL code.<br />Declared using a VARIABLE keyword and above the “DECLARE” block.<br />While usage they are referred wit a prefixed : symbol<br />SYNTAX:<br />VARIABLE variable_name DATATYPE[SIZE];<br />EXAMPLE:<br />VARIABLE eage NUMBER;<br /> BEGIN<br /> SELECT age INTO :eage FROM InfoTable WHERE name = ‘bill’;<br /> END<br /> Only character array variables need to be given a size.<br />
  8. 8. 8<br />Substitution Variables<br />We can either hard code the values of variable or accept the values from the user. To accept a value from the user , it has to be declared as a SUBSTITUTION VARIABLE. Substitution variables when used accept value from user.<br />SYNTAX:<br />VARIABLE variable_name DATATYPE[SIZE] := &variable_name;<br />EXAMPLE :<br />VARIABLE age NUMBER;<br /> DECLARE<br />ename VARCHAR(20) := &ename;<br /> BEGIN<br /> SELECT age INTO eage FRM InfoTable WHERE name = ename;<br /> END;<br />
  9. 9. THANK YOU<br />9<br />THANK YOU FOR VIEWING THIS PRESENTATION<br />FOR MORE PRESENTATIONS AND VIDEOS ON ORACLE AND DATAMINING ,<br />please visit: <br />www.dataminingtools.net<br />

×