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

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 6988

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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:
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,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.