Home » Other » Training & Certification » function with out variable and other doubt
function with out variable and other doubt [message #267822] Sat, 15 September 2007 06:17 Go to next message
ashish2345
Messages: 50
Registered: September 2007
Member
Dear friends,
i been told that function should not have an out variable but functions being created by that also with no error.
can anyone tell/refer the reason and its implications.

secondly if i cannot use out how can i return more than 1 paramter.can anyone give example of using record or anything else

thanks
as lot
regards
Ashish
Re: function with out variable and other doubt [message #267827 is a reply to message #267822] Sat, 15 September 2007 07:47 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
i been told that function should not have an out variable

This is plain wrong.

Quote:
if i cannot use out how can i return more than 1 paramter

You can but you can also return more than one value in the return field using an object type:
SQL> create or replace type mytype as object (
  2    v1 integer,
  3    v2 varchar2(20)
  4    )
  5  /

Type created.

SQL> create or replace function f return mytype
  2  is
  3  begin
  4    return mytype(1,'Michel');
  5  end;
  6  /

Function created.

SQL> select f from dual;
F(V1, V2)
----------------------------------------------------------
MYTYPE(1, 'Michel')

1 row selected.


Regards
Michel
Re: function with out variable and other doubt [message #267830 is a reply to message #267827] Sat, 15 September 2007 07:58 Go to previous messageGo to next message
ashish2345
Messages: 50
Registered: September 2007
Member
thanks alot.
but i read using out variable should be avoided why..

and how to do that with record.
and use it to get returned value (object) from function in procedure in which function is invoked

regards
Re: function with out variable and other doubt [message #267832 is a reply to message #267830] Sat, 15 September 2007 08:34 Go to previous messageGo to next message
Soumen Kamilya
Messages: 128
Registered: August 2007
Location: Kolkata
Senior Member

Quote:
but i read using out variable should be avoided why..

There is no such rules for that...
Functions are already returns values using RETURN clause so why more out parameter?
One more reason is you can use the function in a select statement if that function is not using any out parameter..

Michle has already shows how to return record.

Cheers
Soumen
Re: function with out variable and other doubt [message #267833 is a reply to message #267827] Sat, 15 September 2007 08:51 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
Michel Cadot wrote on Sat, 15 September 2007 14:47
Quote:
i been told that function should not have an out variable

This is plain wrong.

No it is not. It is very correct in my opinion. Note the word should, indicating that according to good standards you should not use out-parameters with functions.
(hmm, rereading this, I should add: good standards, again in my opinion)
A function should return a single variable, which could be in the form of a more or less complex structure, like a table%rowtype for instance.
Re: function with out variable and other doubt [message #267834 is a reply to message #267822] Sat, 15 September 2007 09:11 Go to previous messageGo to next message
ashish2345
Messages: 50
Registered: September 2007
Member
thanks a lot friends..

i have one more doubt .. i want to autoinsert in table for that i want to insert id and increment by one but for that i need to get the last id (means from last row) how to get it..

how to get the first item of last column ie to get element of last row ie identifying last row simply..........


Re: function with out variable and other doubt [message #267835 is a reply to message #267822] Sat, 15 September 2007 09:57 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
INSERT .......... RETURNING ........
Re: function with out variable and other doubt [message #267836 is a reply to message #267833] Sat, 15 September 2007 10:42 Go to previous message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
...indicating that according to good standards you should not use out-parameters with functions.

Most C functions only return return code, and real values in out parameters. I don't think this bad practice.
The only bad practice is to not follow the standard you have in your entreprise (and of course not having standard).

Of course, if you want to use this function in SQL, you can only use the return value.

Regards
Michel
Previous Topic: Leading Value
Next Topic: getting rownumber
Goto Forum:
  


Current Time: Fri Mar 29 02:25:11 CDT 2024