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...

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you que...

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using s...