Skip to main content

PL/SQL in Oracle and basic Programs in PL/SQL #Diksha Oracle Training Centre

             PL/SQL in Oracle with basic Programs.


Hello Everyone,

Today I will discuss about PL/SQL in Oracle .In Previous Blog we discussed about cursor and anonymous block used in cursor as well as uses of %TYPE and %ROWTYPE attribute. Before starting this topic please go through my below blog.

https://dikshaoracletraining.blogspot.com/2020/10/cursor-and-types-of-cursor-in-oracle.html

https://dikshaoracletraining.blogspot.com/2020/11/benefits-of-type-and-rowtype-in-oracle.html


PL/SQL is basically a Procedural Language in SQL and in PL/SQL we use separate two parts first is Anonymous Block and second is Named Block. The PL/SQL programming language was developed by Oracle Corporation in the late 1980s as procedural extension language for SQL relational database. PL/SQL high-performance transaction-processing language.PL/SQL is completely portable.  PL/SQL provides a built-in, interpreted and OS independent programming environment.  PL/SQL can directly be called from the command-line in SQL.  PL/SQL direct call any external programming language like Java ,C++ etc  to database.  Apart from Oracle, PL/SQL is available in other RDBMS like Sybase, DB2, Microsoft SQL Server etc.     Features of PL/SQL Follows object-oriented programming.  PL/SQL is integrated with SQL.  PL/SQL offers extensive error checking.  PL/SQL offers various type of data types.  PL/SQL offers a variety of programming structures.  PL/SQL supports structured programming through functions and procedures.  PL/SQL supports the development of web applications and server pages.     Main Advantages of PL/SQL  SQL is the standard database language and PL/SQL is strongly integrated with SQL. PL/SQL supports both static and dynamic SQL. Static SQL supports DML operations and transaction control from PL/SQL block. In Dynamic SQL, SQL allows embedding DDL statements in PL/SQL blocks.  PL/SQL gives high productivity to programmers as it can query, transform, and update data in a database.  PL/SQL allows sending an entire block of statements to the database at one time. This reduces network traffic and provides high performance for the applications.  PL/SQL saves time on design and debugging by strong features, such as exception handling, encapsulation, data hiding, and object-oriented data types.  PL/SQL provides high security level and PL/SQL provides access to predefined SQL packages.  

  PL/SQL has two blocks

  1.     Anonymous Block (without name work on session only) 

  2.     Named Block  (Procedure, Functions, Packages etc which we can use anytime)    

PL/SQL block basic Syntax:  PL/SQL is a block structured language, below are the parts of block when we try to write any small piece of code.

  DECLARE     <declarations section>

  BEGIN     <executable command(s)>

 EXCEPTION     <exception handling> 

 END;

Note: Below is the Anonymous block where we not use any create statement so this syntax can be used in sessions only.

 

DECLARE

i NUMBER := 1;

BEGIN

LOOP

EXIT WHEN i>10;

DBMS_OUTPUT.PUT_LINE('WELCOME TO DIKSHA ORACLE TRAINING CENTRE');

i:= i+1;

END LOOP;

END;

/

 

Below is the output of above PL/SQL code.

 


 

For Printing 1 to 10 value in PL/SQL Anonymous Block we use below code.

 

DECLARE

i NUMBER := 1;

BEGIN

LOOP

EXIT WHEN i>10;

DBMS_OUTPUT.PUT_LINE(i);

i:= i+1;

END LOOP;

END;

/

 

Below is the output of above PL/SQL code.

 



DECLARE

v_empid number;

v_name  employee.name%type;

v_designation employee.designation%type;

begin

select emp_id , name , designation

into v_empid ,v_name , v_designation

from employee

where salary=27000;

dbms_output.put_line(v_empid||' '||v_name || ' '||v_designation);

end;

/

Here we not use loop so the above block will give only 1 records.

Below is the output of above code:



 

For getting all the records we need to use loop and below is the code:

 DECLARE

  CURSOR c1

  IS

    SELECT

        emp_id, designation

    FROM

        employee

    ORDER BY

        emp_id DESC;

BEGIN

  FOR i IN c1

  LOOP

    dbms_output.put_line( i.emp_id ||' ' || 'DOTC' || ' '||  i.designation );

  END LOOP;

END;

 

Below is the output of above PL/SQL anonymous code.



 

This Blog is important to write some small piece of code to start PL/SQL anonymous block .In next blog when we start  Named block like creating procedures , functions , packages etc it’s easy to understand all the  things like usage of cursors , %type , %rowtype  , exceptional handling etc . Later on we will use some dynamic SQL , Collection records in PL/SQL.    

Please read this blog carefully as this blog is important for writing PL/SQL block   so that in next blog you can easily understand to create some small procedures using cursors, exceptional handling etc and see the reusability of Procedures with various examples from PL/SQL.    

Thanks.

Comments

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Basic SQL Commands in Oracle Database #Diksha Oracle Training Centre

  Basic SQL Commands in Oracle Database.   Hello Everyone, In my previous blog I discussed about SQL and Classification of SQL . Today I will discuss about SQL basic commands which widely used in RDBMS. The Topics of SQL command which I am going to cover in this blog are mainly divided into four Categories: ·         DDL:   DDL consists of commands which are used to define and design the database. ·         DML:   DML consists of commands which are mainly used to manipulate the data in the database. ·        DCL: DCL Consists of commands which deal with the user permissions/access and controls in the database. ·       TCL:   TCL Consist of commands which deal with the transaction in the database. If you want to explore theory part please follow my below blog as in today blog I will discuss about how to use the commands. h...

Pseudo Column in Oracle #Diksha Oracle Training Centre

  Pseudo Column in Oracle. Hello Everyone, Today I will discuss about Pseudo column in Oracle. This topic is very important so before going to read this blog, I suggest you to please see my previous blogs which is in archive folder for your reference so that it’s easy for you to understand this topic. Let’s discuss about Pseudo Column in Oracle. Pseudo column: A pseudo-column is dynamic in Oracle as it behaves like a table column but is not stored in the table. Pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudo columns, but you cannot insert, update, or delete their values. A pseudo column is also similar to a function without arguments. In   Simple word we can say Pseudo column is a column that yields a value when selected, but which is not an actual column of the table. An example is RowID or SysDate. It can be use in combination with the DUAL table. Below are the Pseudo Column commonly used in Oracle Dat...