开发者

Writing a function in plsql

开发者 https://www.devze.com 2023-03-11 11:03 出处:网络
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 fun

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消