Documente Academic
Documente Profesional
Documente Cultură
th the use of SQL user can only perform basic operations such as selecti
e information from some prefabricated tables, inserting information into
ose tables, updating the information stored into tables and also used to
lete information from these tables
/SQL extends SQL by adding control structure found in the other procedu
nguages
PL/SQL allow us to use all the SQL data manipulation commands, tran-
section control commands, SQL functionsand operators, thus allowing u
manipulates data values in a table more flexibly and effectively
PL/SQL BLOCK STRUCTURE
1. Declare section
2. Executable section
3. Exception-handling section.
EXCEPTION - Exception-handling
statements
END ;
DECLARATION SECTION
Example
DECLARE
v_first_name VARCHAR2(35) ;
v_last_name VARCHAR2(35) ;
v_counter NUMBER := 0 ;
EXECUTABLE SECTION
BEGIN
SELECT first_name, last_name
INTO v_first_name, v_last_name
FROM student
WHERE student_id = 123 ;
DBMS_OUTPUT.PUT_LINE
(Student name : || v_first_name || ||
v_last_name);
END;
EXCEPTION-HANDLING SECTION
The exception-handling section is the last section of
the
PL/SQL block.
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE
( There is no student with student id 123 );
END;
EXAMPLE
DECLARE
v_first_name VARCHAR2(35);
v_last_name VARCHAR2(35);
BEGIN
SELECT first_name, last_name
INTO v_first_name, v_last_name
FROM student
WHERE student_id = 123;
DBMS_OUTPUT.PUT_LINE
('Student name: '||v_first_name||' '||v_last_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE
('There is no student with student id 123');
END ;
PL/SQL LANGUAGE ELEMENTS
Identifiers
Literals
Comments
ample :-
Assignment Operator :: A:=2
Range Operator :: 1..4
Concatenate :: || ( dav||college)
Comments
Begin with /* and end with an asterisk-slash */
IF THEN Statement ::
Syntax
IF condition THEN
Sequence_of_statements;
END IF;
Example :-
IF a>b THEN
dbms_output.put_lines( a is greater);
END IF;
IF THEN ELSE ::
Syntax
IF condition THEN
Sequence_of_statements1;
ELSE;
Sequence_of_statements2;
END IF;
EXAMPLE
To illustrate of IF-ELSE construct PL/SQL block to find greate of two numb
User will enter any two numbers and IF statement will check for the grea
among the entered number.
SOLUTION :-
DECLARE
A NUMBER := &ENTER_A;
B NUMBER := &ENTER_B;
BEGIN
IF A > B THEN
DBMS_OUTPUT.PUT_LINE(A IS GREATER);
ELSE
DBMS_OUTPUT.PUT_LINE(B IS GREATER);
END IF;
END;
IF THEN ELSEIF
The::third form of IF statement uses the keyword
ELSIF (not ELSEIF) to introduce additional
Condition, as follows
SYNTAX
IF condition1 THEN
Sequence_of_statements1;
ELSIF condition2 THEN
Sequence_of_statements2;
ELSE
Sequence_of_statements3;
END IF;
Control structures(LOOP)
LOOP: loop repeats a statement. Loops are used to execute statements
until an exit condition is achieved.
There are three types of loops
1. basic loop
2. for loop: is used to perfrom iterative actions based on a count.
3. while loop: perform iterative actions based on condition.
An exit statement is used to terminate loop.
1. for loop: The initial step is executed first, and only once. This step
allows you to declare and initialize any loop control variables.
Next, the condition, i.e.,initial_value .. final_valueis evaluated. If it is
TRUE, the body of the loop is executed. If it is FALSE, the body of the loop
does not execute and flow of control jumps to the next statement just
after the for loop.
After the body of the for loop executes, the value of thecountervariable
is increased or decreased.
The condition is now evaluated again. If it is TRUE, the loop executes and
the process repeats itself (body of loop, then increment step, and then
again condition). After the condition becomes FALSE, the FOR-LOOP
terminates.
For loop
Syntax: FOR variable IN initial_value .. final_value
LOOP sequence_of_statements;
END LOOP;
WAP to print a number by using for loop.
DECLARE
a number(2);
BEGIN
FOR a in 10 .. 20 LOOP
dbms_output.put_line('value of a: ' || a);
END LOOP;
END;
To find factorial of number
declare
i number(4):=1;
n number(4):=&n;
f number(4):=1;
begin
for i in 1..n
loop
f:=f*i;
end loop;
Dbms_output.put_line('the factorial of '||n||' is:'||f);
end;
/
While loop
Sometimes, you dont know in advance how many times a
sequence of statements needs to execute because it
depends on a condition which is not fixed at compile time.
In such cases, you should use PL/SQLWHILE
LOOPstatement.
Syntax:
WHILE condition
LOOP
sequence_of_statements;
END LOOP;
DECLARE
a number(2) := 10;
BEGIN
WHILE a < 20
LOOP dbms_output.put_line('value of a: ' || a);
a := a + 1; END LOOP; END;
/
%type:
%type and % row type
ID Name
1 A
2 B
3 C
4 D
Declare
A number(10);
B number(10);
Begin
Select id, name into a,b where id=3;
Dbms_output.put_line(a|| b);
End;
Declare
a T1.id%type;
B T1.name% type;
Begin
Select id, name into a,b where id=3;
Dbms_output.put_line(a|| b);
End;
Declare
A T1%rowtype;
Select * into a from T1 where id=3;
Dbms_output.put_line(a.id ||
a.name);
End;
Triggers
Atriggeris a special kind of stored procedure
that automatically executes when an event
occurs in thedatabaseserver.
DMLtriggersexecute when a user tries to
modify data through a data manipulation
language (DML) event. DML events are INSERT,
UPDATE, or DELETE statements on a table or view.
syntax
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
BEGIN
--- sql statements
END;
options
CREATE [OR REPLACE ] TRIGGER trigger_name- This clause creates a trigger
with the given name or overwrites an existing trigger with the same name.
{BEFORE | AFTER | INSTEAD OF }- This clause indicates at what time should
the trigger get fired. i.e for example: before or after updating a table. INSTEAD
OF is used to create a trigger on a view. before and after cannot be used to
create a trigger on a view.
{INSERT [OR] | UPDATE [OR] | DELETE}- This clause determines the triggering
event. More than one triggering events can be used together separated by OR
keyword. The trigger gets fired at all the specified triggering event.
[OF col_name]- This clause is used with update triggers. This clause is used
when you want to trigger an event only when a specific column is updated.
[ON table_name]- This clause identifies the name of the table or view to which
the trigger is associated.
[REFERENCING OLD AS o NEW AS n]- This clause is used to reference the old
and new values of the data being changed. By default, you reference the values
as :old.column_name or :new.column_name.
[FOR EACH ROW]- This clause is used to determine whether a trigger must fire
when each row gets affected ( i.e. a Row Level Trigger) or just once when the
entire sql statement is executed(i.e.statement level Trigger).
WHEN (condition)- This clause is valid only for row level triggers. The trigger is
fired only for rows that satisfy the condition specified.
Updating a trigger
For Example:The price of a product changes constantly. It
is important to maintain the history of the prices of the
products.
We can create a trigger to update the
'product_price_history' table when the price of the product
is updated in the 'product' table.
1)Create the 'product' table and 'product_price_history' table