28 March 2015

Select and Loop Control Statement in Oracle Pl SQL

Hi friends,today we will learn about Select statement in Pl Sql.It is second part of control statement.
we will also discus Loop statement in Pl Sql.Let's start..

What is select control statement in Pl Sql:

It provide selection strategy in PL Sql.This is achieve by CASE statement.There are two types of selection statement



  1. Simple case
  2. Searched case

As you know i always want to understand you by a example.So i will present example which is screenshot of execute code in Oracle 10g,
Note:Before starting to writing code must write this command in your oracle 10g "set serveroutput on;" and hit enter.

Example of Simple case statement in Oracle Pl Sql

select control statement in Oracle Pl Sql

Example of Searched case statement in Oracle Pl Sql

select control statement in pl Sql

Note:Simple case statement is like "if then else" statement.In both statement when no expression match then it run else statement.

Iterative(loop) statement in Pl Sql:

Iterative control statements are used when we want to repeat the execution of one or more statements for specific number of time.

There are Three types of Loop in Pl Sql:-

  1. Simple Loop
  2. While Loop
  3. For Loop

Example of Simple loop


Above code Loop code starts by Loop keyword and it will execute all code before "end loop".In between if condition is used to put condition so after a number of time executing the code execution come out from loop.exit keyword is used for it.

Example of While Loop

Loop  statement in Pl Sql

Here while keyword simply put a condition and after Loop keyword do as above i mentioned.

Example of For Loop


Note:
Counter:its is implicitly declared integer whose values automatically increase or decrease by 1 on each iteration of the loop until the upper bound or lower bound is reached.
Reverse:It is a keyword and cause the counter decrease  with each iteration from upper bound to lower bound.
In for statement myindex keyword is user define.This is counter as i told you earlier.This save the increase or decrease number of startnum.Here we use Varchar for output so that we can conconnet to integer value in it because varchar is high rang.

Example of nested Loop


Nested Loop statement in pl swl
here in MIndex 1 will store and in second loop will execute 1 to 1.Thus var variable which is varchar will store * one time and second loop finish it will print *.In second time MIndex will be 2 because it is counter.This time secomd loop will execute 1 to upto 2.It will hold two * in var variable and this process will continue till first loop reached MIndex 5.

If you like this post then you will also like:Control Structure in PL/Sql
If you have some better technique to code above program then make a comment and give your valuable advice.

No comments:

Post a Comment

Ads Inside Post