Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PLSQLNotes

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

PL/SQL is the procedural language extansion to the SQL, in which we get procedural

language features with SQL. In this chapter first we


will discuss some of the limitaions of SQL and then features of PL/SQL that
overcome these limitations.

LIMATATIONS OF SQL

SQL IS the most powerful and comphrensive database language. Is is flexible and
efficient. SQL is designed for working with relation data.
Even though is powerful and efficient it has certain limitations.
some of the llimitaions are:
-SQL is non procudural

to execute the output from the database use the following commands

--out/put--?
SET SERVEROUTPUT ON;
/

--commit,rollback,savepoint =====>TCL commands (Transction commands);

declare
--Single line comment

v1 number:=39;
v2 number:=23;
begin
dbms_output.put_line('Result is '||(v1+v2));
end;
/

--named block
<<OuterBlock>>
declare
v1 number(5):=202;
v2 number(5):=402;
begin
<<InnerBlock>>
declare
v3 number(5):=402;
begin
dbms_output.put_line('Success From Nested Block' ||v1);
dbms_output.put_line('Value From Inner Block' ||InnerBlock.v3);
end;
dbms_output.put_line('Success' ||v1);
end;
/

IF LOOP
declare
v1 number:=94;
v2 number:=32;
temp number;
begin
if(v1>v2) then
temp:=v1;
v1:=v2;
v2:=temp;
end if;
dbms_output.put_line('After swapping values the values are as : '||v1||' '||v2);
end;
/

IF.......ELSE LOOPS
declare
v1 number:=&a;
begin
if(mod(v1,2)=0) then
dbms_output.put_line('The number is even');
else
dbms_output.put_line('The Number is odd');
end if;
end;
/

FOR LOOP

declare
v1 number:=1;
v2 number:=10;

begin
for i in v1..v2
loop
dbms_output.put_line(i);
end loop;
dbms_output.put_line('Success ');
end;

REVERSE FOR LOOP

declare
v1 number:=1;
v2 number:=10;

begin
for i in reverse v1..v2
loop
dbms_output.put_line(i);
end loop;
dbms_output.put_line('Success ');
end;

-----
declare
v1 number:=1;
v2 number:=10;

begin
for i in reverse v1..v2
loop
dbms_output.put_line(v2);
v2:=v2-1;
end loop;
dbms_output.put_line('Success ');
end;

-----
declare
v1 number:=1;
v2 number:=10;
begin
for i in v1..v2
loop
if(i=7) or (i=8) then
dbms_output.put_line('');
else
dbms_output.put_line(i);
end if;
end loop;
dbms_output.put_line('Success ');
end;
/

------

FOR NEW LINE USE COMMAND 'chr(10)'


--------

Store procedure (sub-program)


--------WORA-------
Write once run any number of times
--------compile--------

CREATE OR REPLACE PROCEDURE greeting


is begin
dbms_output.put_line('Welcome To PL/SQL');
end greeting;

CREATE OR REPLACE PROCEDURE greeting


as begin
dbms_output.put_line('Welcome To PL/SQL');
end greeting;

------------

Exceptionn handeling

declare
v1 number:=&entervalue;
v2 number:=&entervalue;
result number;
begin
result:=v1/v2;
exception
when zero_divide then
dbms_output.put_line(sqlerrm);
end;

--------------
Explicit Cursors
These are user-defined cursors.
Explicit cursors are defined in the declaration section of the PL/SQL block.

---------CORSOR WITH FOR LOOP--------------

declare
cursor mycursor is
select * from employee1;
empdata mycursor%rowtype;
begin
for empdata in mycursor
loop
dbms_output.put_line(empdata.emp_name);
end loop;
end;

------TRIGGER-------

a database trigger is a PL/SQL program unit, which get fired automatically whenever
the data event such as DML or DDL
or system event such as lagon or startup tskes place on a schema or adtabase.

Trigger can be categorised in various ways.


-Trigger type
-Triggering Time

Types of triggers:
Statement trigger and row trigger.

1 create or replace trigger empTrigger


2 before insert or update or delete
3 on employee1
4 declare
5 v_weekday varchar2(20);
6 begin
7 v_weekday:=to_char(SYSDATE, 'Dy');
8 if(v_weekday='Tue') then
9 raise_application_error(-20009,'Illegal Day for transction');
10 end if;
11* end;
12 /

Statement trigger
A statement trigger is a trigger in which the trigger action is executed once
for the menipulation operation that firs the trigger.

You can use a statement trigger when the trigger action is performed repeatedly
for each row of the table that is affected by the manipuation operation that fires
the trigger.

You use a row trigger when the trigger action is depemdemt om that data

create or replace trigger record before delete


on emp1
declare
begin
time:=to_char(sysdate, 'HH24');
if time BETWEEN 13 and 14 then
raise_application_error(-20500,'We cannot delete the record in 1PM and 2PM..');
end if;
end;

/* -20000 to -20999 */

You might also like