473,461 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

PL/SQL-FUNCTIONS - 1

debasisdas
8,127 Expert 4TB
This thread contains some useful tips/samples regarding FUNCTIONS in oracle, that the forum members may find useful.

FUNCTION:
==========
1.IT IS A COMPILED BLOCK OF CODE WHICH IS STORED AS AN OBJECT WITHIN THE DATABASE.
2.IT MUST RETURN A VALUE TO THE CALLING PROCEDURE.
3.ONCE A FUNCTION IS CREATED IT BECOMES PART OF THE DUAL TABLE.
4.IF A FUNCTION CONTAINS ANY OUT OR IN-OUT PARAMETER IT CAN'T BE EXECUTED AT STATMENT LEVEL.

BASIC SYNTAX
=============

Expand|Select|Wrap|Line Numbers
  1. CREATE [OR REPLACE] FUNCTION FUNCTION NAME[PARAMETER PARAMETER MODE],....])]
  2. RETURN DATATYPE
  3. {IS/AS}
  4. [LOCAL DECLARATION]
  5. BEGIN
  6. EXECUTABLE STATMENT
  7. RETURN VALUE;
  8. [EXCEPTION
  9. EXCEPTION HANDLER]
  10. RETURN VALUE;
  11. END [FUNCTIONNAME];
  12.  
sample code to show a parameterless function
====================================

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION HELLO RETURN VARCHAR2 IS
  2. BEGIN
  3. RETURN 'Hello welcome to TSDN';
  4. END HELLO;

To call
--------------
Expand|Select|Wrap|Line Numbers
  1. select hello from dual;

SAMPLE FUNCTION WITH IN PARAMETER
==================================
Expand|Select|Wrap|Line Numbers
  1.  
  2.  CREATE OR REPLACE FUNCTION date_diff (max_date STRING, min_date STRING)
  3.  RETURN PLS_INTEGER IS
  4.  
  5.  BEGIN
  6.    RETURN TO_DATE(max_date) - TO_DATE(min_date);
  7.  EXCEPTION
  8.    WHEN OTHERS THEN
  9.    RETURN NULL;
  10.  END date_diff;
  11.  
TO EXECUTE THE FUNCTION
------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. SELECT date_diff('15-JUL-1980', '19-MAR-1979') FROM dual;
IN MODE FUNCTION EXAMPLE #2
=============================

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION EVENODD(num_in NUMBER) RETURN STRING IS
  2. BEGIN
  3.   IF MOD(num_in, 2) = 0 THEN
  4.     RETURN 'EVEN';
  5. ELSE
  6. RETURN 'ODD';
  7.   END IF;
  8. END EVENODD;

TO EXECUTE
------------------------
Expand|Select|Wrap|Line Numbers
  1. select evenodd(1234) from dual;

IN MODE FUNCTION EXAMPLE #3
=============================

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE function shoname (num in number) return string is
  2. name varchAr2(10);
  3. begin
  4. select ename into name from emp where empno=num;
  5. return name;
  6. end;
  7.  
TO EXECUTE
------------------------
Expand|Select|Wrap|Line Numbers
  1. SELECT shoname (7788) FROM DUAL;
IN MODE FUNCTION EXAMPLE #4
=============================

Expand|Select|Wrap|Line Numbers
  1. create or replace function spelldate(dt date) return varchar2 as
  2. mdate varchar2(50);
  3. begin
  4. select to_char(dt,'ddspth month year') into mdate
  5. from dual;
  6. return mdate;
  7. end;

Also check PL/SQL-FUNCTIONS - 2
May 31 '07 #1
0 6993

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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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: 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.