dbTalk Databases Forums  

How to use user function in SELECT query

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss How to use user function in SELECT query in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Procrastinator
 
Posts: n/a

Default How to use user function in SELECT query - 05-03-2006 , 04:56 AM






This doesn't work

Declare

bignum number ;

function sqr(n number) return number
is
begin
return n * n ;
end;
begin

-- dbms_output.put_line(sqr(3));
select sqr(3) into bignum from dual;

end;


Reply With Quote
  #2  
Old   
Maxim Demenko
 
Posts: n/a

Default Re: How to use user function in SELECT query - 05-03-2006 , 05:23 AM






Procrastinator schrieb:
Quote:
This doesn't work

Declare

bignum number ;

function sqr(n number) return number
is
begin
return n * n ;
end;
begin

-- dbms_output.put_line(sqr(3));
select sqr(3) into bignum from dual;

end;

But this does:
Declare

bignum number ;

function sqr(n number) return number
is
begin
return n * n ;
end;
begin

-- dbms_output.put_line(sqr(3));
--select sqr(3) into bignum from dual;
bignum := sqr(3);

end;

Best regards

Maxim


Reply With Quote
  #3  
Old   
Vladimir M. Zakharychev
 
Posts: n/a

Default Re: How to use user function in SELECT query - 05-03-2006 , 05:31 AM




"Procrastinator" <ali711 (AT) sunnipath (DOT) com> wrote

Quote:
This doesn't work

Declare

bignum number ;

function sqr(n number) return number
is
begin
return n * n ;
end;
begin

-- dbms_output.put_line(sqr(3));
select sqr(3) into bignum from dual;

end;

Declare the function standalone or in a package and you
will be able to call it from SQL. Local functions are not
visible to the SQL engine and therefore can't be used in
SQL statements, only PL/SQL block they are declared
in can see them. Besides, your select is pointless, you
can simply

bignum := sqr(3);

And please familiarize yourself with PL/SQL Developer's
Guide and Reference before posting more such questions
as it's all described there in great detail. Oracle manuals
are available for free from Oracle Technology Network.

--
Vladimir M. Zakharychev
N-Networks, makers of Dynamic PSP(tm)
http://www.dynamicpsp.com




Reply With Quote
  #4  
Old   
gazzag
 
Posts: n/a

Default Re: How to use user function in SELECT query - 05-03-2006 , 05:38 AM



Procrastinator wrote:
Quote:
This doesn't work

Declare

bignum number ;

function sqr(n number) return number
is
begin
return n * n ;
end;
begin

-- dbms_output.put_line(sqr(3));
select sqr(3) into bignum from dual;

end;
This does:

1 Declare
2 bignum number ;
3 function sqr(n number) return number
4 is
5 begin
6 return n * n ;
7 end;
8 begin
9 bignum:=sqr(3);
10 dbms_output.put_line('bignum='||bignum);
11* end;
SQL> set serveroutput on
SQL> /
bignum=9

PL/SQL procedure successfully completed.



Reply With Quote
  #5  
Old   
Procrastinator
 
Posts: n/a

Default Re: How to use user function in SELECT query - 05-03-2006 , 05:49 AM



Thanks.

Quote:
Besides, your select is pointless, you
can simply

bignum := sqr(3);
I know, but I needed to know how to use in select query.
The example itself is not of any value to me.



Reply With Quote
  #6  
Old   
AT
 
Posts: n/a

Default Re: How to use user function in SELECT query - 05-03-2006 , 06:04 AM



Why not?
You weren't asking how to use the function in a select, weren't you?
You dumped your code with the remark 'This doesn't work', without even
verifying yourself it *could* actually work.

--
Sybrand Bakker
Senior Oracle DBA


Reply With Quote
  #7  
Old   
Procrastinator
 
Posts: n/a

Default Re: How to use user function in SELECT query - 05-03-2006 , 06:29 AM



Quote:
You weren't asking how to use the function in a select, weren't you?
I was. That was the subject of the post.

'How to use user function in SELECT query'

Quote:
You dumped your code with the remark 'This doesn't work', without even
verifying yourself it *could* actually work.
It doesn't work because my main purpose was to use it in the SELECT
query
and that fails. However now i've created a package as advised and it
works.

thx.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.