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

FUNCTIONS =============

SQL> varaible nsal1 number; SP2-0734: unknown command beginning "varaible n..." - rest of line ig nSQL> variable nsal1 number; SQL> exec ute :nsal1:=netsal(7902); PL/SQL procedure successfully completed. SQL> print nsal1; NSAL1 ---------0 SQL> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 create or replace function netsal (eno emp.empno%type) return number is esal emp.sal%type; ecomm emp.comm%type; nsal number; begin select sal,comm into esal,ecomm from emp where empno=eno; if ecomm is null then nsal:=esal; else nsal:=esal+ecomm; end if; return(nsal); end; / 300

Function created. SQL> variable nsal1 number; SQL> execute :nsal1:=netsal(7902); PL/SQL procedure successfully completed. SQL> print nsal1; NSAL1 ---------3000 SQL> > CREATE OR REPLACE FUNCTION DEPTSAL (DNO EMP.DEPTNO%TYPE) RETURN NUMBER IS CURSOR EMPC IS SELECT SAL FROM EMP WHERE DEPTNO=DNO; TOTSAL NUMBER:=0; BEGIN FOR I IN EMPC

LOOP TOTSAL:=TOTSAL+I.SAL; END LOOP; RETURN(TOTSAL); END; / ********************************************************* CREATE OR REPLACE FUNCTION EMPEXP (ENO EMP.EMPNO%TYPE) RETURN NUMBER IS JDATE EMP.HIREDATE%TYPE; EXP NUMBER; BEGIN SELECT HIREDATE INTO JDATE FROM EMP WHERE EMPNO=ENO; EXP:=TRUNC(MONTHS_BETWEEN(SYSDATE,JDATE)/12); RETURN(EXP); END; / ******************************************************************* CREATE OR REPLACE FUNCTION NAME_JOB (ENO EMP.EMPNO%TYPE) RETURN VARCHAR2 IS NAME EMP.ENAME%TYPE; EJOB EMP.JOB%TYPE; GETDATA VARCHAR2(35):="; BEGIN SELECT ENAME,JOB INTO NAME,EJOB FROM EMP WHERE EMPNO=ENO; GETDATA:=GETDATA||RPAD(NAME,9)||'('||EJOB||')'; RETURN(GETDATA); END; / *********************************************************************** create or replace function max_sal (i number) return number is msal number; begin if i=1 then select max(sal) into msal from emp; return(msal); elsif i=2 then select max(sal) into msal from emp where sal=(select max(sal) from emp where sal<(select max(sal) from emp)); return(msal); else select max(sal) into msal from emp where sal=(select max(sal) from emp where sal<(select max(sal) from emp where sal<(select max(sal) from emp))); return(msal); end if; end;

******************************************************************** create or replace function getbon (eno emp.empno%type) return number is ejob emp.job%type; esal emp.sal%type; begin select job,sal into ejob,esal from emp where empno=eno; case when ejob='MANAGER' then return(trunc(esal*15/100)); when ejob='ANALYST' then return(trunc(esal*12/100)); when ejob='SALESMAN' then return(trunc(esal*10/100)); when ejob='CLERK' then return(trunc(esal*8/100)); else return(esal); end case; end; / ************************************************************************ CREATE OR REPLACE FUNCTION DFUN (SL EMP.SAL%TYPE) RETURN NUMBER IS TAXRATE NUMBER; BEGIN SELECT DECODE(TRUNC(SL/800),0,0.00,1,0.009,2,0.020,3,0.030,4,0.040,0.045) INTO TAXRATE FROM DUAL; RETURN(TAXRATE); END; / select ename,job,sal,getbon(empno) bonus from emp where getbon(empno)<100 order by job; select empno,ename,sal*0.12 insu from emp where sal*dfun(sal)<10; ********************************************************************** create or replace function mydate (jdate varchar2) return date is begin return(to_date(jdate,'dd-mm-yyyy hh24:mi:ss')); end; insert into emps4 values(1012,'hanuman',23000,mydate('22-dec-1999 11:23:36') ******************************************************************** ====================================================================== PACKAGES

============== create or replace is procedure addno(a procedure mulno(a procedure divno(x end mathope; package mathope number,b number); number,b number); number,y number);

CREATE OR REPLACE PACKAGE BODY MATHOPE IS PROCEDURE ADDNO(A NUMBER,B NUMBER) IS C NUMBER; BEGIN C:=A+B; DBMS_OUTPUT.PUT_LINE('_______________ADD:'||C); END ADDNO; PROCEDURE MULNO(A NUMBER,B NUMBER) IS C NUMBER; BEGIN C:=A*B; DBMS_OUTPUT.PUT_LINE('__________________MUL:'||C); END MULNO; PROCEDURE DIVNO(X NUMBER,Y NUMBER) IS Z NUMBER; BEGIN Z:=TRUNC(X/Y); DBMS_OUTPUT.PUT_LINE('___________________DIV:'||Z); END DIVNO; END MATHOPE; / ******************************************************** create sequence empno_id increment by 1 start with 1131 maxvalue 9999 nocycle nocache; create table emps5 (empno number(4), ename varchar2(10), sal number(7,2), jdate date);

create or replace package over_emps is procedure ins-emps (v_empno emps5.empno%type, v_name emps5.ename%type default'unknown', v_sal emps5.sal%type default 0, v_jdate emps5.jdate%type default sysdate);

procedure ins-emps (v_name emps5.ename%type default'unknown', v_sal emps5.sal%type default0, v_jdate emps5.jdate%type default sysdate); end over-emps; / create or replace package body over_emps is procedure ins-emps (v_empno emps5.empno%type, v_name emps5.ename%type default'unknown', v_sal emps5.sal%type default 0, v_jdate emps5.jdate%type default sysdate) is begin insert into emps5 values(v_empno,v_name,v_sal,v_jdate); end ins-emps; procedure ins-emps (v_name emps5.ename%type default'unknown', v_sal emps5.sal%type default 0, v_jdate emps5.jdate%type default sysdate) is begin insert into emps5 values(empno_id.nextval,v_name,v_sal,v_jdate); end ins-emps; end over_emps; **************************************************** create or replace package emps_dml is procedure ins_rec (eno emps5.empno%type, ena emps5.ename%type, esal emps5.sal%type); procedure upd_rec(eno emps5.empno%type, esal emps5.sal%type); procedure del_rec(eno emps5.empno%type); end emps_dml; / create or replace package body emps_dml is procedure ins_rec (eno emps5.empno%type, ena emps5.ename%type, esal emps5.sal%type); is emps_exist exception; emps_count number; begin select count(*) into emps_count from emps1 where empno=eno; if emps_count>0 then raise emps_exist; else insert into emps5

values(eno,ena,esal); commit; end if; exception when emps_exist then raise_application_error(-20111,'sorry employee exist with this no:'||eno); end ins_rec; procedure upd_rec (eno emps5.empno%type, esal emps5.sal%type); is mycount number; myemps exception; begin select count(*) into mycount from emps5 where empno=eno; if mycount>0 then update emps5 set sal=sal+esal where empno=eno; commit; else end if; exception when myemps then raise_application_error(-20333,'sorry employee does not exist with this no:'||en o); end upd_rec; procedure del_rec(eno emps5.empno%type)

You might also like