Out parameters in functions?
Is it possible to use out parameters in a function? if so in which scenario or situations we use this method?
Hi,
Composite type? but composite types are plsql types? Not necessarily,
CREATE TYPE customer_typ_demo AS OBJECT
( customer_id NUMBER(6)
, cust_first_name VARCHAR2(20)
, cust_last_name VARCHAR2(20)
, cust_address CUST_ADDRESS_TYP
, phone_numbers PHONE_LIST_TYP
, nls_language VARCHAR2(3)
, nls_territory VARCHAR2(30)
, credit_limit NUMBER(9,2)
, cust_email VARCHAR2(30)
, cust_orders ORDER_LIST_TYP
i've gone through oracle Plsql fundamentals, but in that its given only valid sql datatypes are valid in return statement!!I think you have misread something. It is true though, that if you want to use your function in SQL it should return an SQL datatype (And can have no OUT parameters)
I understand its a bad practice.. but is there any worst case scenario where we need to use functions with out parameters, that no othr methods r possible?None that I can think of..
Regards
Peter
Similar Messages
-
How to retreive out parameters from function in sql statement
hi dear,
Suppose i have a fuction with two "out" parameters, i want to show the values of these paramenters in sql stm.
like
select my_function() from dual;
ThanxCan't be done. To use a function in SQL it can only have a single return value.
Try something like this changing the data types appropriately.
var x number
var y number
var z number
exec :x := my_function(:y, :z)
print -
Hi,
o_ErrorText
Bring back to the caller the error text in long form.
Validated only in failure case.
o_Errorcode
Return back to the caller the errorcode
How can i use these two as out perameters.
Venkat.Functions should be considered as expressions, so out parameters in functions are considered "not done", although it's possible.
Here is an example using out parameters in a procedure:
SQL> create procedure validate_date_ddmmyyyy
2 ( p_input in varchar2
3 , p_errorcode out varchar2
4 , p_errortext out varchar2
5 )
6 is
7 l_date date;
8 begin
9 l_date := to_date(p_input,'ddmmyyyy');
10 exception
11 when others then
12 p_errorcode := sqlcode;
13 p_errortext := sqlerrm;
14 end;
15 /
Procedure is aangemaakt.
SQL> create procedure test (p_input in varchar2)
2 is
3 l_errorcode varchar2(10);
4 l_errortext varchar2(1000);
5 begin
6 dbms_output.put_line(p_input)
7 ;
8 validate_date_ddmmyyyy(p_input,l_errorcode,l_errortext)
9 ;
10 if l_errorcode is not null
11 then
12 dbms_output.put_line(' *** ERROR ***');
13 dbms_output.put_line(' ' || l_errorcode);
14 dbms_output.put_line(' ' || l_errortext);
15 end if;
16 end;
17 /
Procedure is aangemaakt.
SQL> exec test('abc')
abc
*** ERROR ***
-1858
ORA-01858: a non-numeric character was found where a numeric was expected
PL/SQL-procedure is geslaagd.
SQL> exec test('19192007')
19192007
*** ERROR ***
-1843
ORA-01843: not a valid month
PL/SQL-procedure is geslaagd.
SQL> exec test('46062007')
46062007
*** ERROR ***
-1847
ORA-01847: day of month must be between 1 and last day of month
PL/SQL-procedure is geslaagd.
SQL> exec test('01010001')
01010001
PL/SQL-procedure is geslaagd.Regards,
Rob. -
SQLJ : sending refcursors via out parameters of PL/SQL functions possible ?
Hello,
in SQLJ it's possible to get a refcursor back from a PL/SQL
function via
the return value(the usual case in the online examples).
Is it also possible to give a refcursor back via an out
parameter
so that the return value could be used for others ways ?
SQLJ generation in JDeveloper handles the first way, but we
didn't
get it to work with the out parameter.
Thank you for your help.It's certainly possible to have a stored procedure with an OUT parameter that is a REF CURSOR and use that to return data to JDBC. That's actually the most common way that JDBC programmers get ResultSets from stored procedures. I would have to assume that SQLJ would facilitate that?
Is there a reason that you're looking to use a stored function rather than a stored procedure? The latter seems like a much more logical construct when you have OUT parameters. I've never seen a stored function that was defined with any OUT parameters.
Justin
Distributed Database Consulting, Inc.
www.ddbcinc.com -
Calling a function from SQL that has Out Parameters
Hello folks,
I am not sure if this is do-able, but here is my scenrio at hand. I have a database function that accepts a few IN parameters and OUT parameters. I want one of the Out parameters to display as part of my select statement. Is this possible?
ThanksNo you cannot use a function with out parameters in sql statements.
If possible make the function return that value, then use it in select.( imean instead of OUT parameter, get that OUT parameter through RETURN)
G. -
Is it possble to use out/inout parameters in function?
Is it possble to use out/inout parameters in function?
Hi,
user10784864 wrote:
But I can't call the function from SQL if it contains DML statements also right?Right.
I know of two separate and independent reasons why fucntions with OUT arguments (or IN OUT arguments; I'll just say OUT in the remainder of this message)might not be a good idea.
(1) They can be confusing, and
(2) you can't call them from SQL statements.
My reasons for (1):
They are potenially confusing, because people usially expect the function value (alone) to pass information back to the caller. I like to write code that works. I like to avoid writing code that doesn't work, even if the reason is simply because someone else didn't read the documentation carefully. Notice I didn't say "Never write a function with OUT arguments", I said think carefully before you do. I believe there are good uses for such functions. I also believe that there are good uses for duct tape, and those good uses account for maybe 1% of the actual applications. (By the way, DML in stored procedures is another thing that is done far more often than it should.)
If one of these reasons doesn't apply in your case, you should still consider the other reason. -
Can a function take OUT parameters...?
Hi all.
Can a function take OUT parameters...?
Thanks in advance,
PalYou can do it in PL/SQL but not in SQL. The following example demonstarte that -
satyaki>
satyaki>create or replace package S_glob
2 as
3 b varchar2(20);
4 end;
5 /
Package created.
satyaki>
satyaki>
satyaki>create or replace function S_test_mul_out(a in number,c out varchar2)
2 return number
3 is
4 cursor c1(eno in number)
5 is
6 select ename,job,sal
7 from emp
8 where empno = eno;
9
10 rec c1%rowtype;
11 d number(10);
12 begin
13 open c1(a);
14 loop
15 fetch c1 into rec;
16 exit when c1%notfound;
17 glob.b:= rec.ename;
18 c:= rec.job;
19 d:= rec.sal;
20 end loop;
21 close c1;
22 return d;
23 end;
24 /
Function created.
satyaki>
satyaki>
satyaki>set serveroutput on
satyaki>
satyaki>
satyaki>declare
2 yy varchar2(30);
3 zz number(10);
4 begin
5 ZZ:= s_test_mul_out(7566,yy);
6
7 dbms_output.put_line('Ename: '||glob.b);
8 dbms_output.put_line('Job: '||yy);
9 dbms_output.put_line('Sal: '||zz);
10 end;
11 /
Ename: JONES
Job: MANAGER
Sal: 1000
PL/SQL procedure successfully completed.
satyaki>Regards.
Satyaki De.
Message was edited by:
Satyaki_De -
Oracle Function with out parameters
Hi, I'm trying to access a stored function on an oracle server with 2 out parameters, 2 in parameters and an out ref cursor.
PL/SQL looks like this
nStatusCode OUT INTEGER,
sStatusMsg OUT VARCHAR2,
sUsrNr IN VARCHAR2,
sPassword IN VARCHAR2,
crsReturn OUT pck_cursor.retRecordSet
I drag the function onto dataset designer and it successfully maps the results to the datatable, it maps datatypes for first out parameters to decimal and string.
the method genearated for fill by looks like (out decimal?,out string,string, string, out object)
problem is, everytime i try to call this function it gives me casting exceptions for the first 2 out parameters, for the decimal it's not specified in more detail. for the other one it says there was an exception casting from OracleString to string. If I change the PL/SQL function to leave the first 2 out parameters away it works fine. Do I manually need to change the TableAdapter definitions??Hi,
I think I found the problem but not a good solution. It seems to be a problem with auto code generation for TableAdapters in the Dataset wizard. In the Parameter Definition for the Command I set DbType.Decimal and DbType.Sting for the SqlDbtype Property of the out params. The GetData and Fill Method is generated with Fill(out decimal? out1, out string out2). When I look in the generated code ith first sets the SqlDbtype as written but after this also the OracleDbtype to OracleDbType.Decimal and OracleDbType.Varchar2. The casting these types before returning the throws the exception. Any idea how to change this behavior? -
Hi,
Below is a helper method used for calling a server-side function which loops through the inbound bindVars parameter to populate the function's IN parameters. Is there a way to dynamically determine the IN/OUT parameters based on the procedure name in the stmt parameter? No members of the CallableStatement class seemed promising, but the getParameterMetaData() method in the PreparedStatement class seemed like it could be helpful lead. However, I have not found any detailed descriptions (yet) of how to use it.
protected Object callStoredFunction(int sqlReturnType, String stmt,
Object[] bindVars) {
CallableStatement st = null;
try {
// 1. Create a JDBC CallabledStatement
st = getDBTransaction().createCallableStatement(
"begin ? := "+stmt+";end;",0);
// 2. Register the first bind variable for the return value
st.registerOutParameter(1, sqlReturnType);
if (bindVars != null) {
// 3. Loop over values for the bind variables passed in, if any
for (int z = 0; z < bindVars.length; z++) {
// 4. Set the value of user-supplied bind vars in the stmt
st.setObject(z + 2, bindVars[z]);
// 5. Set the value of user-supplied bind vars in the stmt
st.executeUpdate();
// 6. Return the value of the first bind variable
return st.getObject(1);
catch (SQLException e) {
throw new JboException(e);
finally {
if (st != null) {
try {
// 7. Close the statement
st.close();
catch (SQLException e) {}
JamesThe PreparedStatement.getParameterMetaData() object is exactly what you need for this task.
Once you have the ParameterMetaData you can ask it how many parameters are present and which mode they are. The parameters are numbered from 1 to n and you can use ParameterMetaData.getParameterMode(1); to get the mode of the 1st parameter. The modes are defined as static values in the ParameterMetaData object. Check out the doc at http://docs.oracle.com/javase/7/docs/api/java/sql/ParameterMetaData.html
Timo -
Reg:execute procedure with in out parameters
hi,
what is the code to execute a procedure with in out parameters.can anyone give me an example
thanks872296 wrote:
thanks for the reply.
i am very much new to oracle database.i need this code to put in one of my informatica mapping.
so can you just elaborate what does 'karthick' mean?is it the name of the procedure.No, karthick is the value of the variable that is being passed into the procedure called "P" in karthicks example, then if that procedure changes the value inside, the variable will have that new value passed back out of the procedure to it.
PROCEDURE prc_mv (name VARCHAR2)
IS
BEGIN
dbms_mview.refresh (mv_name);
END prc_mv;
PROCEDURE refresh (response IN OUT NUMBER)
IS
BEGIN
dbms_mview.refresh('mv1','C');
dbms_mview.refresh('mv2','C');
response := 1;
EXCEPTION
WHEN OTHERS
THEN
response := 0;
END refresh;
can you give the code for this procedure.Yes.
DECLARE
v_response NUMBER;
BEGIN
refresh(v_response);
END;Though your code is awful. There's no point in having the response parameter as an IN OUT if you're not going to pass IN a value and use that in the code anywhere. In your case it only needs to be an OUT parameter because you're just passing back OUT a value. You are also masking any exceptions that happen by using a WHEN OTHERS clause.
Better code would be something like...
FUNCTION refresh (mv_name) RETURN NUMBER IS
v_response NUMBER := 0; -- default response value
e_mv_not_exist EXCEPTION; -- exception variable
PRAGMA EXCEPTION_INIT(e_mv_not_exist, -23401); -- connect exception name to internal oracle error number
BEGIN
dbms_mview.refresh(mv_name,'C');
v_response := 1;
EXCEPTION
WHEN e_mv_not_exist THEN -- handle specific expected exception
-- if the materialized view does not exist, handle it gracefully as we don't want to stop
response := 0;
END refresh;
declare
v_response NUMBER;
begin
v_response := refresh('mv1');
if v_response = 0 then
-- the materialized view did not exist
else
-- the materialized view refreshed ok
end if;
end;where your exception handler explicity checks for expected exceptions such as :
ORA-23401: materialized view "SCOTT"."FRED" does not exist... and any other exceptions that you're not expecting will be raised for you to see.
It's also better as a function because you don't need to pass in a response value, you just want to get a response value back.
There's rarely a good need to use OUT or IN OUT parameters. (there's some cases, but it's not something to consider doing as part of your regular design) -
Database procedure with IN/OUT parameters
Hi,
I have a procedure with multiple OUT parameters,
but I do not know how to get the values of these out parameters in the calling procedure.
What I mean is I can simply get the value of a function from a calling procedure as:-
declare
val1 number;
begin
val1 := func_get_num;
end;
How can I get the values of OUT parameters of a procedure in a similar way?like
SQL> var ename_v varchar2(30);
SQL> var empno_v number;
SQL> create or replace procedure get_employee(empno out number, ename out varchar)
2 as
3 begin
4 select empno, ename into empno, ename from emp where rownum <=1;
5 end;
6 /
Procedure created.
Elapsed: 00:00:00.51
SQL> exec get_employee(:empno_v, :ename_v);
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.12
SQL> print empno_v
EMPNO_V
666
SQL> print ename_v;
ENAME_V
fdddfdf1
SQL> -
Please help me out with the function code of print option in module pool
please help me out with the function code of print option in module pool, along with CASE condition.
regards,
asifHi
you can use the Function module
CALL FUNCTION 'GET_PRINT_PARAMETERS'
EXPORTING
destination = 'LP01' "'Printer name
list_name = 'TEST'
list_text = 'SUBMIT ... TO SAP-SPOOL'
immediately = ' '
line_size = '2000'
no_dialog = 'X' "pass space to Pop screen for Print option
IMPORTING
out_parameters = wa_pri_params
valid = w_valid.
"next call below things
NEW-PAGE PRINT ON NO DIALOG PARAMETERS wa_pri_params.
"and try to Print the values inside the new-page
NEW-PAGE PRINT OFF.
Prabhudas -
Count of IN/OUT Parameters.
Hello Gurus,
Is there anyway I can get the Count or list of IN/OUT parameters which are suplied to Procedure or function. It would be great if its Generic statement.
Thanx in advance,
PritamYou can quyer out the data dictionary :
User_Arguments
All_Arguments
i.e.
select object_name, package_name, object_id, argument_name,
position, data_type, default_value,
default_length,
data_length, data_precision,
in_out
from all_arguments
where object_name = '&Enter_Proc_Or_Func_Name'
/ -
A simple question about parameters to functions
hello,
Passing parameters between functions in java suppose to be "by reference".
I have a simple test case that it doesn't work.
I'll be happy if someone will tell me what is wrong:
public class Test
String text ;
public static void main(String[] args)
Test test = new Test();
test.poo() ;
public void poo ()
text=new String(" bbb") ;
foo (text) ;
System.out.println ("final text= " +text) ;
private void foo(String text)
System.out.println ("text before update= " +text) ;
text="aaa" ;
System.out.println ("text after update= " +text) ;
The output here is:
text before update= bbb
text after update= aaa
final text= bbb
How can you explain that?
thank you,
Moran> Passing parameters between functions in java suppose to be "by reference".
Nope. First, we call them "methods" rather than "functions". Second, everything in Java is passed "by value". Everything.
Pass-by-value
- When an argument is passed to a function, the invoked function gets a copy of the original value.
- The local variable inside the method declaration is not connected to the caller's argument; any changes made to the values of the local variables inside the body of the method will have no effect on the values of the arguments in the method call.
- If the copied value in the local variable happens to be a reference (or "pointer") to an object, the variable can be used to modify the object to which the reference points.
Some people will say incorrectly that objects are passed "by reference." In programming language design, the term [i]pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory.... The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.
-- James Gosling, et al., The Java Programming Language, 4th Edition -
Collection as out parameter in function - help required
I am having a fucntion which returns 3 different values based on a given in parameter.
Now , I want to return these three values.
Instead of using three out parameters, how can I use a collection for returning the values.
Please tell me the best way in returing the multiple values(more than 3)
Thanks in advance
Regards
RaghuSQL> create type my_obj as object (no integer, name varchar2(10))
2 /
Type created.
SQL> create type my_tbl as table of my_obj
2 /
Type created.functional approach...
SQL> create or replace function my_fn return my_tbl
2 as
3 ltbl my_tbl;
4 begin
5 select my_obj(1,'karthick') bulk collect into ltbl from dual;
6
7 return ltbl;
8 end;
9 /
Function created.
SQL> select * from table(cast(my_fn() as my_tbl))
2 /
NO NAME
1 karthickprocedureal approach...
SQL> create or replace procedure my_proc(ptbl out my_tbl)
2 as
3 begin
4 select my_obj(1,'karthick') bulk collect into ptbl from dual;
5 end;
6 /
Procedure created.
SQL> declare
2 ltbl my_tbl;
3 begin
4 my_proc(ltbl);
5
6 for i in 1..ltbl.count
7 loop
8 dbms_output.put_line(ltbl(i).no||' '||ltbl(i).name);
9 end loop;
10 end;
11 /
1 karthick
PL/SQL procedure successfully completed.
Maybe you are looking for
-
Hi, i would love to know how to take my dvd's from the laptop to the ipad. I have a 14 hour flight coming up and i really need some dvd's. I have tried a couple of programs but nothing has worked yet. I have snow leapord 10.6.8. Thanks in advance!
-
Out of nowhere I can only receive gmail on iPhone, cannot send?!?
Out of nowhere my iPhone stopped sending email from my work gmail account. My personal account works fine. The error message I get says the password is wrong - but I know it's not because I can log on via the web and send email from there. I have tri
-
Who can help me? I look for adobe Illustrator 8.0 for free
Who can help me? I look for adobe Illustrator 8.0 for free
-
How to output the digital clock and synchronization signal from the NI USB-6211
Hello, I need to connect the NI USB-6211 to control a digital to analog convertor chip (AD5541). However, this chip requires three input signals :1) Clock input, 2) Logic input or a synchronization signal and 3) Signal Serial Data input (CS, SCLK, D
-
How to exit full screen mode while playing a presentation in Keynote '09
Hi, my question is: when I play a presentation it starts by default in full screen mode; I would like instead to see it in a little box, in order to be able to take notes on another file; how can I do that?