Max_Salah
Max_Salah

Reputation: 2497

How to call a sql function?

I have written a function minimum2 which take two numbers as arguments and returns the minimum number. This function compiles without any error. when I call the function minimum(1,2); I get the errors PLS-00103. Here is the function and the function call:

CREATE OR REPLACE FUNCTION minimum2(v1 number, v2 number) RETURN number IS
     BEGIN
       IF v1 < v2 THEN
         RETURN v1;
       ELSE
         RETURN v2;
       END IF;
     END;     
 --I call the function asl follow
 minimum2(1,2);

What did I do wrong? I wrote this code in sql developer

Upvotes: 0

Views: 101

Answers (2)

user330315
user330315

Reputation:

You need to run a select

select minimum2(1,2) 
from dual

You also need to end the function with a /:

enter image description here

For details on how and why to use the / see here


Are you aware that there is a built-in function for that?

select least(1,2) 
from dual

Upvotes: 4

akash
akash

Reputation: 11

--specifying 'in' and 'out' to parameters is important or it can work as it is, but best practice is to use:

CREATE OR REPLACE FUNCTION minimum2(v1 IN number, v2 IN number) RETURN number IS
     BEGIN
       IF v1 < v2 THEN
         RETURN v1;
       ELSE
         RETURN v2;
       END IF;
     END; 

--I call the function as follows through anonymous block and you can not call the function directly.

set serveroutput on;
begin
dbms_output.put_line('Output is ' || minimum2(1,2));
END;
/

Upvotes: 1

Related Questions