473,549 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PL/SQL-FUNCTIONS - 1

debasisdas
8,127 Recognized Expert Expert
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 6999

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3340
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 about sql database and sql server, specially this article: http://www.aspfaq.com/show.asp?id=2195 will someone help me understand: 1. with *SQL...
2
12645
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 ${SCHEMA_NM}/${SCHEMA_PASSWD}@${DB_NM} <<-ANALYZE_TABLE SET TERMOUT ON
0
10173
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 database table, using dynamic sql. Executing 'EXEC SQL DESCRIBE SELECT LIST FOR S INTO select_dp;' the error code -2149 is returned That means...
10
6722
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 find an administration front-end to SQL, other than the VisualStudio, which is ok for creating databases and adding tables/columns. However when I go...
2
15211
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 Personal disk from the SQL Server 2000 Enterprise kit as this is reported here on the MSDN web site to be the version that is supported on Windows XP. ...
6
6756
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 the SQL Profile to watch the T-SQL-Command which Access ( who creates the commands?) creates and noticed:
4
7293
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 get the same error message: "An error occurred while creating one or more registry entries. Please see C:\WINDOWS\sqlstp.log for details. The...
1
6601
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 Express Edition x86: Component Microsoft SQL Server 2005 Express Edition x86 returned an unexpected value. ***EndOfSession***? Microsoft SQL Server...
14
3005
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 anyone please help me with this as I want to install SQL server and also wouold be grateful, if you can suggest me any workaround to dealwith this...
5
9267
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 records from an external Oracle source using a SQL statement. The SQL statement works as expected when the loop code is commented out, but I receive...
0
7524
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7960
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7475
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7812
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5372
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3501
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
766
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.