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

PL/SQL-FUNCTIONS - 2

debasisdas
8,127 Expert 4TB
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
May 31 '07 #1
0 6933

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...
2
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My employer has provided me with the SQL Server 2000...
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...
4
by: coosa | last post by:
Hi, I was installing SQL Server on my machine and during installation my PC freezed. It happens frequently on my machine. So i tried after restarting to install it again and since then i always...
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...
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: 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...
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...
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.