Writing a function in plsql
I'm querying the DB and need to parse one of the fields for spe开发者_Python百科cific values (using string functions). So I figured it'd be best to write a function for it. I've never written a function before in plsql, so I decided to look at some examples.
I got a copy of a simple "square" function which takes a number and returns the square of it, but am not sure how to call it from the SQL statement.
I only have read access. Will I be able to write functions and use them to retrieve the data I need? And if so, how?
To be able to create a function your user needs the CREATE PROCEDURE privilege granted to it by the DBA:
grant create procedure to myschema;
If you have the privilege then you can create a function like this
create function square(n in number) return number
is
return n*n;
end;
And you can call it from SQL like this:
select num, square(num)
from mytable;
Note: in Oracle it is usually preferred to create functions in packages, in which case the calling SQL would be like:
select num, mypackage.square(num)
from mytable;
精彩评论