473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

Pl/sql-package - 4

debasisdas
8,127 Expert 4TB
PACKAGE WITH LOCAL FUNCTION
=============================
Expand|Select|Wrap|Line Numbers
  1. create or replace package my_pkg as
  2. procedure my_proc(arg1 in varchar2);
  3. function my_fun(arg1 in number) return varchar2;
  4. end my_pkg;

PACKAGE BODY
===============
Expand|Select|Wrap|Line Numbers
  1. create or replace package body my_pkg as
  2.  
  3. function my_private_fun(arg1 in number) return varchar2
  4. is
  5. return_val varchar2(20);
  6. begin
  7. select col1 into return_val from tab2 where col2=arg1;
  8. return return_val;
  9. exception
  10. when NO_DATA_FOUND THEN
  11. return 'Sorry no data found.....!';
  12. end  my_private_fun;
  13.  
  14. function my_fun(arg1 in number)return varchar2
  15. is
  16. begin
  17. return my_private_fun(arg1);
  18. end my_fun;
  19.  
  20. procedure my_proc(arg1 in varchar2)
  21. is
  22. begin
  23. update tab1 set col1=col1+1 where col2=arg1;
  24. end my_proc;
  25. end my_pkg;
Note:----In this case the function my_private_fun is a local function in the function body which is not been defined in the package. So this function is local to the package, so it can be accessed from within the package only. It cant be referenced through the package name.

and in PL/SQL the procedures and function must be defined before it is being used. Other wise user will get compilation error.

package
==========
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE PACKAGE PACK
  2. IS
  3. P_ENAME EMP.ENAME%TYPE;
  4. P_SAL EMP.SAL%TYPE;
  5. P_JOB EMP.JOB%TYPE;
  6. P_REC EMP%ROWTYPE;
  7. CURSOR C IS SELECT * FROM EMP;
  8. C_REC C%ROWTYPE;
  9. PROCEDURE DEPT_INS
  10. (
  11. D_DEPTNO DEPT.DEPTNO%TYPE,
  12. D_DNAME DEPT.DNAME%TYPE,
  13. D_LOC DEPT.LOC%TYPE
  14. );
  15. FUNCTION ANSAL(ENO EMP.EMPNO%TYPE)
  16. RETURN NUMBER;
  17. END;
  18.  
USE OF GLOBAL VARIABLES
-------------------------
Expand|Select|Wrap|Line Numbers
  1. BEGIN
  2. SELECT ENAME,SAL,JOB INTO PACK.P_ENAME,PACK.P_SAL,PACK.P_JOB FROM EMP WHERE EMPNO=&NO;
  3. DBMS_OUTPUT.PUT_LINE(PACK.P_ENAME||' '||PACK.P_SAL||' '||PACK.P_JOB);
  4. END;
  5.  
USE OF CURSOR
---------------------------------
Expand|Select|Wrap|Line Numbers
  1. BEGIN
  2. OPEN PACK.C;
  3. LOOP
  4. FETCH PACK.C INTO PACK.C_REC;
  5. EXIT WHEN PACK.C%NOTFOUND;
  6. DBMS_OUTPUT.PUT_LINE(PACK.C_REC.ENAME);
  7.  
  8. END LOOP;
  9. CLOSE PACK.C;
  10. END;
  11.  
----------------------------------
PACKAGE BODY
----------------------------------
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE PACKAGE BODY PACK
  2. IS
  3.  
  4. PROCEDURE DEPT_INS
  5. (
  6. D_DEPTNO DEPT.DEPTNO%TYPE,
  7. D_DNAME DEPT.DNAME%TYPE,
  8. D_LOC DEPT.LOC%TYPE
  9. )
  10. IS 
  11. BEGIN
  12. INSERT INTO DEPT VALUES(D_DEPTNO,D_DNAME,D_LOC);
  13. DBMS_OUTPUT.PUT_LINE('1 RECORD INSERTED....');
  14. EXCEPTION
  15. WHEN DUP_VAL_ON_INDEX THEN
  16. DBMS_OUTPUT.PUT_LINE('DUPLICATE RECORD FOUND.....!');
  17. WHEN OTHERS THEN
  18. DBMS_OUTPUT.PUT_LINE('SOME OTHER ERROR......!');
  19. END DEPT_INS;
  20.  
  21. FUNCTION ANSAL
  22. (
  23. ENO EMP.EMPNO%TYPE
  24. )
  25. RETURN NUMBER
  26. AS
  27. SALARY NUMBER(8);
  28. BEGIN
  29. SELECT (SAL+NVL(COMM,0))*12 INTO SALARY FROM EMP WHERE EMPNO=ENO;
  30. RETURN SALARY;
  31. END ANSAL;
  32. END PACK;
  33.  
Jun 8 '07 #1
0 6228

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: cooldv | last post by:
i am running a website on Windows 2000 server with ASP 3 webpages and Access 2000 database. (with a hosting company) traffic is slow at this time but expect to grow. lately i have been reading...
2
by: Peter | last post by:
I run most of my SQL scripts via kornshell on AIX. I use the "here-document" to run some of the smaller ones. Example: #!/bin/ksh # Analyze the table. sqlplus...
0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
10
by: Dagwood | last post by:
Good morning: At least it's morning where I am. :) I have a rather newbie question I'm afraid. I have VisualStudio.NET, and have installed it along with SQL server. However I can't seem to...
6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
1
by: Peter | last post by:
I've purchased VS.NET 2005 Standard and have tried to install SQL Server 2005 Express, but get the following error in the error log. Please could someone help me.... Microsoft SQL Server 2005...
6
by: Fuzzydave | last post by:
I am back developing futher our Python/CGI based web application run by a Postgres DB and as per usual I am having some issues. It Involves a lot of Legacy code. All the actual SQL Querys are...
14
by: Developer | last post by:
Hello All, i have recently installed VS2005 and was trying to install SQL sever 2000. I have Win XP' SP2. But when I tried installing, it only installed client tools and not the database. Can...
5
by: dbrother | last post by:
Access 2003 Win XP Pro SP3 Using SQL /ADO Recordsets in a Do Loop Hello, I'm using a random number generator based on an integer input from a user from a form that will get X number of random...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.