Sie sind auf Seite 1von 11

RDBMS Using Oracle

PL/SQL
Procedural Language/Structural Query Language
PL/SQL Procedures

Kamran.Munir@niit.edu.pk

Use of

IN OUT
Example:

Format phone Procedure

Example:

Format phone Procedure

Input

3009504455

Output

(300)950 - 4455

Format phone Procedure


create OR REPLACE PROCEDURE FORMAT_PHONE
(v_phone_no in out varchar2)
is
begin
v_phone_no := '(' || substr(v_phone_no,1,3) || ')'
|| substr(v_phone_no,4,3) || ''- '
|| substr(v_phone_no,7);
DBMS_OUTPUT.put_line(v_phone_no);
DBMS_OUTPUT.put_line(v_phone_no
);
END;

Procedure created.

Format phone Procedure contd


contd

Declaring variable

SQL> VARIABLE g_phone_no varchar2(15);

Assigning value to variable


SQL> begin
:g_phone_no := 3009504455;
End;

Execute Procedure
?

SQL> execute FORMAT_PHONE(:g_phone_no


FORMAT_PHONE(:g_phone_no);
);
PL/SQL procedure successfully completed.

SQL> PRINT G_PHONE_NO


G_PHONE_NO;;
G_PHONE_NO
-------------------------------(300)950--4455
(300)950

Output (300)950 - 4455

Example 2 (SHOW SUMMARY)


SQL> create OR REPLACE PROCEDURE show_Summary
(v_deptno in number)
is
v_count number; v_avg number; v_sum number;
v_min number; v_max number;
begin
select count(empno
count(empno),
), avg(sal
avg(sal),
), sum(sal
sum(sal),
), min(sal
min(sal),
), max(sal
max(sal))
into
v_count,, v_avg
v_count
v_avg,, v_sum
v_sum,, v_min
v_min,, v_max
from emp where deptno = v_deptno
v_deptno;;
DBMS_OUTPUT.put_line(concat('Total # of employees = ', v_count
v_count));
));
DBMS_OUTPUT.put_line(concat('Salary Average = ', v_avg
v_avg));
));
DBMS_OUTPUT.put_line(concat('Salary Total = ', v_sum
v_sum));
));
DBMS_OUTPUT.put_line(concat('Maximum Salary = ',v_max
', v_max));
));
DBMS_OUTPUT.put_line(concat('Minimum Salary = ', v_min
v_min));
));
END;

Procedure Execution/OUTPUT
SQL> execute show_summary(10);
Total Number of employees are = 3
Salary Average = 2916.666666666666
Salary Total = 8750
Maximum Salary = 5000
Minimum Salary = 1300
?

PL/SQL procedure successfully completed.

Methods for passing


Parameters
Positional
2. Named
3. Combination
1.

Methods for passing Parameters


?

Positional
List values in the order in which the parameters are
declared.

Named
List values in arbitrary order, using parameter name.

Combination
We can also use combination of both above.

Example
create or replace procedure add_emp
(v_empno in emp.empno%type
emp.empno%type,,
v_deptno in emp.deptno%type
emp.deptno%type,,
v_ename in emp.empno%type DEFAULT UNKNOWN)
is
begin
insert into emp(empno,deptno,ename
emp(empno,deptno,ename))
values(v_empno,, v_deptno
values(v_empno
v_deptno,, v_ename
v_ename);
);
end;
?

Procedure created.

Note:- Similarly Practice procedures


For UPDATE & DELETE

Methods for passing Parameters


?

Positional
SQL> add_emp (1234, 10, TOM);
Named
SQL> add_emp (v_derpno => 10 , v_empno =>1234,
v_ename = > TOM);
SQL> add_emp (v_derpno => 10 , v_empno =>1234)

Combination
SQL> add_emp (1234, v_derpno => 10 );

Invoking a procedure from a Anonymous


PL/SQL Block
Declare
V_id number := 7900;
Begin
procedure_name (v_id
v_id))
Commit;

We can also assign value


to a variable like this.

Procedure Call
Inside procedure

End;

Drop Procedure

Syntax
SQL> Drop procedure <procedure_name
<procedure_name>;
>;

SQL/PLSQL Tips
?

search PL/SQL code


SELECT * FROM USER_SOURCE

To retrieve the version information for Oracle, you execute the


following SQL statement:
select * from v$version
where banner like 'Oracle%';

SQL/PLSQL Tips

SQL/PLSQL Tips

Select the Nth highest value from a


table
select level, max('col_name') from my_table
where level = '&n' connect by prior ('col_name') > 'col_name ')
group by level;

Example
SQL> select level, max(sal) from emp
where level = '&n' connect by prior (sal) >
sal
group by level

Learn IfIf -Else What is the result of


comparing NULL with NULL?
Declare
a number := NULL;
b number := NULL;
Begin
if a=b then

NULL is neither equal


to NULL, nor it is not
equal to NULL. Any
comparison to NULL is
evaluated to NULL.
Look at this code
example to convince
yourself.

dbms_output.put_line('True, NULL = NULL');


elsif a<>b then
dbms_output.put_line('False, NULL <> NULL');
else
dbms_output.put_line('Undefined NULL is neither = nor <> to

NULL');

end if;
end;

You can grant users various privileges to


tables. These privileges can be any combination of
select, insert, update, delete, references, alter, and
index. Below is an explanation of what each privilege
means.

Privilege

Description

Select

Ability to query the table with a select statement.

Insert

Ability to add new rows to the table with the insert statement.

Update

Ability to update rows in the table with the update statement.

Delete

Ability to delete rows from the table with the delete statement.

References Ability to create a constraint that refers to the table.


Alter

Ability to change the table definition with the alter table statement.

SQL/PLSQL Tips

SQL/PLSQL Tips

The syntax for granting privileges on a table is:

Grant privileges on object to user


?

For example, if you wanted to grant select, insert, update, and delete
privileges on a table called suppliers to a user name smithj
smithj,, you would
execute the following statement:

GRANT SELECT, INSERT, UPDATE, DELETE ON SUPPLIERS TO SMITH;

You can also use the all keyword to indicate that you wish all
permissions to be granted. For example:

GRANT ALL ON SUPPLIERS TO SMITH;


REVOKE ALL ON SUPPLIERS TO SMITH; (To Revoke Privilege)
?

If you wanted to grant select access on your table to all users, you
could grant the privileges to the public keyword. For example:

GRANT SELECT ON SUPPLIERS TO PUBLIC;

SQL/PLSQL Tips

SQL/PLSQL Tips

How can I protect my PL/SQL


source code?
PL/SQL V2.2, available with Oracle7.2, implements a binary
wrapper for PL/SQL programs to protect the source code.
This is done via a standalone utility that transforms the
PL/SQL source code into portable binary object code
(somewhat larger than the original). This way you can
distribute software without having to worry about exposing
your proprietary algorithms and methods. SQL*Plus and
SQL*DBA
SQL*
DBA will still understand and know how to execute
such scripts. Just be careful, there is no "decode"
command available.
? The syntax is:
? wrap iname
iname=
=myscript.sql oname
oname=
=xxxx.plb
?

Can one call DDL statements


from PL/SQL?
?

One can call DDL statements like CREATE, DROP,


TRUNCATE, etc. from PL/SQL by using the "EXECUTE
IMMEDATE"" statement. Users running Oracle versions
IMMEDATE
below 8i can look at the DBMS_SQL package (see FAQ
about Dynamic SQL).
Begin
EXECUTE IMMEDIATE 'CREATE TABLE X(A DATE)';
end;
NOTE: The DDL statement in quotes should not be
terminated with a semicolon.

10

Thanks

Kamran.Munir@niit.edu.pk

11

Das könnte Ihnen auch gefallen