Connecting Tech Pros Worldwide Forums | Help | Site Map

PL/SQL-FUNCTIONS - 2

debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,511
#1   May 31 '07
FUNCTION WITH DEFAULT PARAMETER
===================================
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE function weekdaysinmonth(mdate date default
  2.  sysdate-(to_char(sysdate,'dd')-1),weekday integer default 1)  return varchar2 
  3.  as
  4.      myexp exception;
  5.      cursor c1(dt date) is
  6.      with dtsun as (select dt + level-1 dm,
  7.      to_char(dt+ level-1,'d') dy
  8.      from dual connect by level <=to_char(last_day(dt),'dd'))
  9.      select count(dm) from dtsun where dy=weekday;
  10.      mcount number(4);
  11.      wkd varchar2(10);
  12.  begin
  13.      if weekday>7 or weekday<=0 then
  14.          raise myexp;
  15.      end if;
  16.      if to_char(mdate,'dd')<>1 then
  17.          raise myexp;
  18.      end if;
  19.      open c1(mdate);
  20.      fetch c1 into mcount;
  21.      close c1;
  22.      case weekday
  23.      when 2 then
  24.      wkd:='Mon';
  25.      when 3 then
  26.      wkd:='Tue';
  27.      when 4 then
  28.      wkd:='Wed';
  29.      when 5 then
  30.      wkd:='Thu';
  31.      when 6 then
  32.      wkd:='Fri';
  33.      when 7 then
  34.      wkd:='Sat';
  35.      else
  36.      wkd:='Sun';
  37.      end case;
  38.      return ('Number of '|| wkd || ' in '||to_char(mdate,'Month') || ' is ' || mcount);
  39. exception
  40.     when myexp then
  41.          return ('supplied date should be first of the month and weekday should be between 1-7');
  42.     when others then
  43.          return null;
  44. end;
  45.  

FUNCTION WITH OUT PARAMETER
============================
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION out_func (outparm OUT VARCHAR2)
  2. RETURN VARCHAR2 IS
  3.  
  4. BEGIN
  5.   outparm := 'out param';
  6.   RETURN 'return param';
  7. END out_func;
  8.  
TO EXECUTE
---------------------------

Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE
  3.   retval VARCHAR2(50);
  4.   outval VARCHAR2(50);
  5. BEGIN
  6.   retval := out_func(outval);
  7.   dbms_output.put_line(outval);
  8.   dbms_output.put_line(retval);
  9. END;
  10.  
  11.  
FUNCTION WITH IN OUT MODE
==========================
Expand|Select|Wrap|Line Numbers
  1. --through the same variable the function accepts the value and returns the value.
  2. CREATE FUNCTION ANSAL(ENO IN OUT NUMBER)
  3. RETURN NUMBER
  4. IS BEGIN
  5. SELECT (SAL+NVL(COMM,0))*12 INTO ENO FROM EMP WHERE EMPNO=ENO;
  6. RETURN ENO;
  7. EXCEPTION
  8. WHEN NO_DATA_FOUND THEN
  9. DBMS_OUTPUT.PUT_LINE('NO MATCHING DATA FOUND');
  10. RETURN 0;
  11. END;
  12.  
TO EXECUTE
------------------------
Expand|Select|Wrap|Line Numbers
  1. --Since the procedure contains out mode it can't be called directly at SQL Prompt.
  2. DECLARE
  3. NO NUMBER(5):=&NO;
  4. SALARY NUMBER(5);
  5. BEGIN
  6. SALARY:=ANSAL(NO);
  7. DBMS_OUTPUT.PUT_LINE(SALARY);
  8. END;
  9.  

Also check Oracle Tips And Tricks -PL/SQL-FUNCTIONS - 1



Reply