Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't seem to declare variables within a pl/sql function?

This is a really simple question, but I can't seem to find the syntax for this anywhere.

I have something like this:

FUNCTION some_function
(
t_string IN VARCHAR2
) RETURN NUMBER IS

some_variable NUMBER;

BEGIN
//logic
END some_function;

It hits the some_variable declaration and tells me it was expecting "language" where/how do I declare variables? I've seen examples which have done it this way but for some reason it doesn't work.

Many thanks, Fugu

like image 618
Fugu Avatar asked Jan 12 '11 11:01

Fugu


People also ask

Is declare mandatory in Plsql?

The declaration section is required if any variables are to be used in a PL/SQL block. The declaration section also defines cursors, types, local procedures, and functions that are used in a block. If no variables or other elements need to be declared, then this section may be omitted.

What is the correct way to declare CONSTANT in the declaration section of a PL SQL block?

To learn how to declare a constant in PL/SQL let's quickly take a look at the syntax. This is our syntax: constant_name CONSTANT datatype (data-width) := value; First you need to give a valid name to your constant followed by keyword CONSTANT that indicates the declaration of a constant in your program.


1 Answers

Did not found anything wrong with your declared variable:

create or replace FUNCTION some_function
(
t_string IN VARCHAR2
) RETURN NUMBER 
IS
some_variable NUMBER;

BEGIN

return some_variable;

END some_function;

Returned NULL as expected:

select some_function('ff') from dual  
like image 74
Michael Pakhantsov Avatar answered Sep 19 '22 21:09

Michael Pakhantsov