473,385 Members | 1,877 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,385 developers and data experts.

PL/SQL-PACKAGE - 2

debasisdas
8,127 Expert 4TB
SAMPLE PACKAGE EX#3
====================
PACKAGE SPECIFICATION
--------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE PACKAGE MYPACK AS
  2. PROCEDURE SHOWENAME(EMPID IN NUMBER);
  3. FUNCTION SHOWSAL(EMPID IN NUMBER) RETURN NUMBER;
  4. END MYPACK;
  5.  
-------------------
package body
----------------------
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE PACKAGE BODY MYPACK AS
  2. PROCEDURE SHOWENAME(EMPID IN NUMBER) IS
  3. MYNAME EMP.ENAME%TYPE;
  4. BEGIN
  5. SELECT ENAME INTO MYNAME FROM EMP WHERE EMPNO=EMPID;
  6. DBMS_OUTPUT.PUT_LINE(MYNAME);
  7. END SHOWENAME;
  8. FUNCTION SHOWSAL(EMPID IN NUMBER) RETURN NUMBER IS
  9. MYSAL EMP.SAL%TYPE;
  10. BEGIN
  11. SELECT SAL INTO MYSAL FROM EMP WHERE EMPNO=EMPID;
  12. RETURN MYSAL;
  13. END SHOWSAL;
  14. END MYPACK;
SAMPLE PACKAGE EX#4
====================
PACKAGE SPECIFICATION
--------------------------------------------

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE  PACKAGE EMP_ACTIONS AS
  2. PROCEDURE HIRE_EMPLOYEE (EMPNO NUMBER,ENAME VARCHAR2,JOB  VARCHAR2,
  3. MGR NUMBER,HIREDATE DATE,SAL NUMBER,COMM         NUMBER,DEPTNO NUMBER);
  4. PROCEDURE FIRE_EMPLOYEE (EMP_ID NUMBER);
  5. PROCEDURE TRANSFER_EMPLOYEE(EMP_ID NUMBER,DEPT_NO NUMBER);
  6. PROCEDURE PROMOT_EMPLOYEE(EMP_ID NUMBER,EMP_JOB VARCHAR2);
  7. PROCEDURE INCREMENT(EMP_ID NUMBER,INCR NUMBER);
  8. FUNCTION ANSAL(NUM IN OUT NUMBER)RETURN NUMBER;
  9. END EMP_ACTIONS;
-------------------
package body
---------------------
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE PACKAGE BODY emp_actions AS
  2. PROCEDURE hire_employee (EMPNO NUMBER,ENAME VARCHAR2,JOB  VARCHAR2,
  3. MGR NUMBER,HIREDATE DATE,SAL NUMBER,COMM         NUMBER,DEPTNO NUMBER)
  4. IS
  5. BEGIN
  6. INSERT INTO EMP VALUES(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO);
  7. EXCEPTION
  8. WHEN DUP_VAL_ON_INDEX THEN
  9. RAISE_APPLICATION_ERROR(-20001,'DUPLICATE ENTRY...!');
  10. WHEN OTHERS THEN
  11. RAISE_APPLICATION_ERROR(-20002,'SOME OTHER ERROR...!');
  12. END hire_employee;
  13. PROCEDURE fire_employee (emp_id NUMBER) IS
  14. I INT;
  15. BEGIN
  16. SELECT COUNT(*)  INTO I FROM EMP    WHERE EMPNO=EMP_ID;
  17. IF I=1 THEN
  18. DELETE FROM emp WHERE empno = emp_id;
  19. ELSE
  20. DBMS_OUTPUT.PUT_LINE('EMPLOYEE DOES NOT EXIST...!');
  21. END IF;
  22. END fire_employee;
  23. PROCEDURE TRANSFER_EMPLOYEE(EMP_ID NUMBER,DEPT_NO NUMBER)
  24. IS
  25. NUM EMP.EMPNO%TYPE;
  26. BEGIN
  27. SELECT COUNT(EMP_ID) INTO NUM FROM EMP WHERE EMPNO=EMP_ID;
  28. IF NUM=1 THEN
  29. UPDATE EMP SET DEPTNO=DEPT_NO WHERE EMPNO=EMP_ID;
  30. END IF;
  31. END TRANSFER_EMPLOYEE;
  32. PROCEDURE PROMOT_EMPLOYEE(EMP_ID NUMBER,EMP_JOB VARCHAR2)
  33. IS
  34. I INT;
  35. BEGIN
  36. SELECT EMPNO INTO I FROM EMP WHERE EMPNO=EMP_ID;
  37. IF I=1 THEN
  38. UPDATE EMP SET JOB=EMP_JOB WHERE EMPNO=EMP_ID;
  39. END IF;
  40. END PROMOT_EMPLOYEE;
  41. PROCEDURE INCREMENT(EMP_ID NUMBER,INCR NUMBER)
  42. IS
  43. NUM EMP.EMPNO%TYPE;
  44. BEGIN
  45. SELECT COUNT(*) INTO NUM FROM EMP WHERE EMPNO=EMP_ID;
  46. IF NUM=1 THEN
  47. UPDATE EMP SET SAL=SAL+INCR WHERE EMPNO=EMP_ID;
  48. END IF;
  49. END INCREMENT;
  50. FUNCTION ANSAL(NUM IN OUT NUMBER)RETURN NUMBER
  51. IS
  52. II NUMBER(4);
  53. BEGIN
  54. SELECT COUNT(*) INTO II FROM EMP WHERE EMPNO=NUM;
  55. IF II=1 THEN
  56. SELECT (SAL+NVL(COMM,0))*12 INTO NUM FROM EMP WHERE EMPNO=NUM;
  57. END IF;
  58. RETURN NUM;
  59. END ANSAL;
  60. FUNCTION SHOWMANAGER(ID IN NUMBER)RETURN VARCHAR2
  61. IS
  62. MG EMP.MGR%TYPE;
  63. II NUMBER(4);
  64. BEGIN
  65. SELECT COUNT(*) INTO II FROM EMP WHERE EMPNO=ID;
  66. IF II=1 THEN
  67. SELECT MGR INTO MG FROM EMP WHERE EMPNO=ID;
  68. END IF;
  69. RETURN MG;
  70. END SHOWMANAGER;
  71. END EMP_ACTIONS;

SAMPLE PACKAGE EX#5
====================
PACKAGE SPECIFICATION
------------------------------------------

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE  PACKAGE PACK1 IS 
  2. PROCEDURE PROC1(DEPTN NUMBER);
  3. PROCEDURE PROC2(NO NUMBER);
  4. PROCEDURE PROC3(NUM NUMBER);
  5. END PACK1;
package body
----------------------
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE PACKAGE BODY PACK1 AS
  2.  
  3. PROCEDURE PROC1(DEPTN NUMBER) IS
  4. salary emp.sal%type;
  5. ex exception;
  6. begin
  7. if (deptn=10 or deptn=20 or deptn=30) then
  8. select max(sal) into salary from emp where deptno=deptn;
  9. dbms_output.put_line('The max salary for deptno '||deptn||' is '||salary);
  10. else 
  11. raise ex;
  12. end if;
  13. exception
  14. when ex then
  15. raise_application_error(-20001,'Please enter a valid department number');
  16. end proc1;
  17.  
  18. procedure proc2(no number) is
  19. a number;
  20. bb number;
  21. c number;
  22.  
  23. begin
  24. c:=1;
  25. bb:=no;
  26. loop
  27. c:=c*bb;
  28. bb:=bb-1;
  29. exit when bb<1;
  30. end loop;
  31. dbms_output.put_line('The factorial of  is '||c);
  32.  
  33. END PROC2;
  34.  
  35. PROCEDURE PROC3(NUM NUMBER) IS
  36. FIRS number;
  37. SECON NUMBER;
  38. SMM NUMBER(4);
  39. B NUMBER;
  40. BEGIN
  41.   B:=0;
  42.   FIRS:=0;
  43.   SECON:=1;
  44.    SMM:=0;
  45. DBMS_OUTPUT.PUT_LINE(FIRS);
  46. DBMS_OUTPUT.PUT_LINE(SECON);
  47. WHILE B<NUM-2 LOOP
  48. SMM:=FIRS+SECON;
  49. FIRS:=SECON;
  50. SECON:=SMM;
  51. B:=B+1;
  52. DBMS_OUTPUT.PUT_LINE(SMM);
  53. END LOOP;
  54. END proc3;
  55.  
  56. END PACK1;
To execute the members of the package from anomymous block.
-----------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. declare
  2. a number;
  3. b number;
  4. wrong exception;
  5. begin
  6. a:=&a;
  7. b:=&b;
  8. if a=1 then
  9. goto hello1;
  10. elsif a=2 then
  11. goto hello2;
  12. elsif a=3 then
  13. goto hello3;
  14. else
  15. raise wrong;
  16. end if;
  17. <<hello1>>
  18. pack1.proc1(b);
  19. return;
  20. <<hello2>>
  21. pack1.proc2(b);
  22. return;
  23. <<hello3>>
  24. pack1.proc3(b);
  25. return;
  26. exception
  27. when wrong then
  28. raise_application_error(-20001,'Please enter the numbers in range 1(for dept),2(for factorial), or 3(for febonacci)');
  29. end;
Also check PL/SQL-PACKAGE - 3
Jun 8 '07 #1
0 3810

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...
0
by: AlessanBar | last post by:
Hello Friends !! I have a strange problem, and I need to know what would be the source of this. I have a laptop computer with the following configuration: Pentium III Brand : Toshiba Speed :...
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: annie | last post by:
Hi all, I have recently ported my Access 2000 app to SQL Server, keeping the Access client as the front end using linked tables. I am also using triggers on my SQL tables to trap orphan...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.