Can we write function in select statement?
i have function and it has Out parameter,so in this case can i write select statement for my function to retrieve the value?
Or, you could use pipelined function - i guess.
Like ->
satyaki>
satyaki>select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
Elapsed: 00:00:00.00
satyaki>
satyaki>create or replace type a_obj as object
2 (
3 e_nm varchar2(30),
4 e_sal number(7,2)
5 )
6 /
Type created.
Elapsed: 00:00:00.00
satyaki>
satyaki>
satyaki>create or replace type a_rec as table of a_obj;
2 /
Type created.
Elapsed: 00:00:00.00
satyaki>
satyaki>create or replace function multi_return(e_cd in number)
2 return a_rec
3 pipelined
4 is
5 cursor c1
6 is
7 select a_obj(ename, sal) as a_det_rec
8 from emp
9 where empno = e_cd;
10
11 r1 c1%rowtype;
12 begin
13 for r1 in c1
14 loop
15 pipe row(r1.a_det_rec);
16 end loop;
17
18 return ;
19 end;
20 /
Function created.
Elapsed: 00:00:00.01
satyaki>
satyaki>
satyaki>select * from table(cast(multi_return(&e_no) as a_rec));
Enter value for e_no: 7369
old 1: select * from table(cast(multi_return(&e_no) as a_rec))
new 1: select * from table(cast(multi_return(7369) as a_rec))
no rows selected
Elapsed: 00:00:00.02
satyaki>
satyaki>
satyaki>select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO JOB1 DOB
7521 WARD SALESMAN 7698 22-FEB-81 226.88 500 30 SALESMAN
7654 MARTIN SALESMAN 7698 28-SEP-81 1815 1400 30 SALESMAN
7788 SCOTT ANALYST 7566 19-APR-87 598.95 20 ANALYST
7839 KING PRESIDENT 17-NOV-81 7260 10 PRESIDENT
7844 TURNER SALESMAN 7698 08-SEP-81 2178 0 30 SALESMAN
7876 ADAMS CLERK 7788 23-MAY-87 159.72 20 CLERK
7900 JAMES CLERK 7698 03-DEC-81 1379.4 30 CLERK
7902 FORD ANALYST 7566 03-DEC-81 5270.76 20 ANALYST
7934 MILLER CLERK 7782 23-JAN-82 1887.6 10 CLERK
7566 Smith Manager 7839 23-JAN-82 1848 0 10 Manager 23-JAN-89
7698 Glen Manager 7839 23-JAN-82 1848 0 10 Manager 23-JAN-89
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO JOB1 DOB
7599 BILLY ANALYST 7566 10-JUN-09 4500 30
12 rows selected.
Elapsed: 00:00:00.01
satyaki>
satyaki>select * from table(cast(multi_return(&e_no) as a_rec));
Enter value for e_no: 7698
old 1: select * from table(cast(multi_return(&e_no) as a_rec))
new 1: select * from table(cast(multi_return(7698) as a_rec))
E_NM E_SAL
Glen 1848
Elapsed: 00:00:00.00
satyaki>
satyaki>/
Enter value for e_no: 7599
old 1: select * from table(cast(multi_return(&e_no) as a_rec))
new 1: select * from table(cast(multi_return(7599) as a_rec))
E_NM E_SAL
BILLY 4500
Elapsed: 00:00:00.00
satyaki>Regards.
Satyaki De.
Similar Messages
-
PL/SQL Function in Select statement
Hi
I am calling a function in select statement. The query works in Toad and PL/SQL developer except SQL plus and disconnecting Oracle connection.
The error is “ERROR at line 1: ORA-03113: end-of-file on communication channel”.
When I called the same query from BC4J View Object the error message is “java.sql.SQLException: No more data to read from socket”.
Can any one advise me please?
Thanks
SriniSrini
I've seen similar cases in the past with 9.2.0.x (x <= 5). What Oracle version are you using? It's worth checking the bug database (I can't just now - I don't have a valid support id in my current contract). And as Warren says, post your SQL query.
HTH
Regards nigel -
Why I Can't use procedure in select statement
Why I Can't use procedure in select statement
We can use function in select statement but we couldn't use procedure with one out parameters in select statement... You can use Function because they are designed for this but procedure are not. Functions can return value (without OUT parameter) which can be used in SELECT whereas procedures do not have such concept. As you can see in the above post you can not call even functions also if it has any out parameter.
I have just trying to use procedure in select statement ..for this I require technical answer..The technical answer is because conceptually procedure is for doing set of operation, performning DMLs on the tables , whereas functions are for processing and producing a single result. Functions are basically for not using INSERT/UPDATE/DELETE in it. That is the reason they are allowed to be used in SELECT because conceptually they are not supposed to do any data changes.
Regards,
Avinash -
Unable to call local function in select statement.
Hi all,
I am unable to call the local function in select statement in below scenario.
DECLARE
l_cnt NUMBER;
FUNCTION FUN1 RETURN NUMBER
AS
BEGIN RETURN 2;
END;
BEGIN
SELECT FUN1 INTO l_cnt FROM DUAL;
DBMS_OUTPUT.PUT_LINE(l_cnt );
END;
/Any alternate way to call local function in select statement?
Thanks
Ram.Hi,
Sorry, you can't call a locally defined function in a SQL statement, even if that SQL statement is in a PL/SQL block where the function is in scope.
This applies to packages, also. If a function is not declared in the package spec, but only in the package body, then you can't call it from SQL statements in the package body.
Why do you want a locally defined function? Why not a function defined outside the procedure? -
How to pass rowtype argument to a function from select statement?
Hi all!
I have function that takes mytable%rowtype as in parameter. can I pass entire row of mytable to the function from select statement? kind of
select myfunction(mytable.*) from mytable where ....
Thanks in advanceThe function can be used in a SQL statement only if it accepts SQL types and returns SQL type. %ROWTYPE being PL/SQL construct and not a SQL datatype, can not be used in this context.
http://download-west.oracle.com/docs/cd/B14117_01/server.101/b10743/datatype.htm#i2093 -
Using java function in select statement
Hi,
I am trying to use java function in select statement.
public class ClassA{
private static String MyConst = "foo";
public static String functionA(){
return MyConst;
in my query I have:
select
ClassA.functionA() AS id,
groupId AS newID,
from
myChannel[now]
ClassA is part of the application (no need to import).
I get and error of Invalid Expression on ClassA.functionA().
I also tried to declare the function in the processor element:
<wlevs:processor id="proc">
<wlevs:function function-name="A" exec-methode="functionA">
<bean class="mtPackage.ClassA"/>
</wlevs:function>
<wlevs:processor>
but then I get a different error in the processor XML file: "An InvocationTargetException was encoutered while attemting to register the user defind function A. The message was null"
What am I missing here?Hi,
From the above description, you have tried two manners to call method functionA() in the user defined class ClassA. One uses java cartridge manner directly and the other try to use user defined function manner.
For the java cartridge manner, the following CQL query should work if the ClassA is really included in the OEP app. I have done similar test before, it works.
select
ClassA.functionA() AS id,
groupId AS newID,
from
myChannel[now]
For user defined function manner, I think two things you need to change:
1. Need to declare the function in the EPN assembly file(under META-INF/spring/), not component configuration file(under META-INF/wlevs/). The following is an example:
<wlevs:processor id="proc">
<wlevs:function function-name="A" exec-methode="functionA">
<bean class="mtPackage.ClassA"/>
</wlevs:function>
</wlevs:processor>
2. Call the user defined function in the CQL query in the component configuration file under processor. For example:
select A() from myChannel
Regards,
XiYing -
Can we write function in a trigger??
Can we write function in a trigger??
3360 wrote:
810534 wrote:
Can we write function in a trigger??This can be better phrased as two questions.
Would we want to write a function in a trigger?
Answer - No.
Is there any problem that needs to be solved by writing a function in a trigger?
Answer - No.Even better would be the same two questions with "a function in " removed.
Same answers. -
Can i join these sql select statement in one
Hi All,
Can i join this two select statement in to one select statement.
select username from dba_users where username like 'SAP%%%'; # to the Schemaid of the below /BMC/YGO_CPROD.
select YOP_PRD_NM, YOP_VERS from <schemaid>."/BMC/YGO_CPROD"; # To know a version from the table.
I am using this in a script can any help to join the above select statements into one.
Schemaid should be passed to this select YOP_PRD_NM, YOP_VERS from <schemaid>."/BMC/YGO_CPROD"; and i need to get output!
Thanks a lotAre you asking about using the output of one query as the input to the WHERE clause of another?
If so look at the demos here: http://www.psoug.org/reference/conditions.html
PS: There is zero value in the construct 'SAP%%%'
What is it you are trying to do?
One "%" wildcard is sufficient. -
Using function in select statement
Hi,
CREATE FUNCTION [dbo].[udf_testFunction]
@value int
RETURNS int
AS
BEGIN
-- Declare the return variable here
declare @returnValue int
set @returnValue = @value*2;
return @returnValue;
END
GO
create table #Temp
EmpID int,
EmpName Varchar(50)
insert into #Temp(EmpID,EmpName) values(1,'Name1');
insert into #Temp(EmpID,EmpName) values(2,'Name2');
insert into #Temp(EmpID,EmpName) values(3,'Name3');
insert into #Temp(EmpID,EmpName) values(4,'Name4');
insert into #Temp(EmpID,EmpName) values(5,'Name5');
select EmpID,EmpName, [dbo].[udf_testFunction](EmpID), [dbo].[udf_testFunction](EmpID)*EmpID,[dbo].[udf_testFunction](EmpID)+2 from #Temp
In the above select statement i used [dbo].[udf_testFunction]() function 3 times. But i want to use only once and reuse it.
Something like
select EmpID,EmpName, testfunctionvalue,testfunctionvalue*EmpID,testfunctionvalue+2 from #Temp
Please advise me.... Thanks in Advance...You can use this code:
WITH cte
AS ( SELECT EmpID ,
EmpName ,
[dbo].[udf_testFunction](EmpID) AS testfunctionvalue
FROM #Temp
SELECT EmpID ,
EmpName ,
testfunctionvalue ,
testfunctionvalue * EmpID ,
testfunctionvalue + 2
FROM cte
But using scalar functions in select clause can hurt the performance. Please see this link:
SQL Server Scalar User Defined Function Performance
T-SQL Articles
T-SQL e-book by TechNet Wiki Community
T-SQL blog -
Function in select statement need to be called only for the last record.
Select state,
local,
fun_state_loc_other_details(19) details
from state_local
where pm_key=19
resuts:_
State Local Details
AP APlocal details1
UP UPLocal details1
MP MPLocal details1
i) The above query returns 100 records
ii) fun_state_loc_other_details is also getting called 100 times. But I want this function to be called only for the last record that is 100th record.
is there any way to do that?Thanks amatu.
One more small query. Can I do it on condition based.
Select state,
local,
fun_state_loc_other_details(19) details
from state_local
where pm_key=19
Like if one state it need to be called once.
AP -- 50 records
UP - 20 records
MP -- 10 records.
fyi: this record no. varies
I want the function to be called for AP once, UP once, MP once. -
Using Column Name returned by function in SELECT statement
Hi
Output from my function (RETURN data type is VARCHAR2) is column name. I want to use it directly in my SELECT statement. Below is simplified example of this:
--- Function
CREATE OR REPLACE FUNCTION simple RETURN varchar2 IS
BEGIN
RETURN ‘my_column’;
END simple;
--- Select
SELECT simple FROM my_table;
This does not work. It seems that output from function is passed in quotation i.e.
SELECT ‘my_column’ FROM my_table;
So the output from SELECT is a list of rows populated with values my_table:
COLUMN simple
ROW1 my_column
ROW2 my_column
ROW3 my_column
Can please someone help me with this?I'm not sure I got you right.
In standard SQL everything must be known at compile time. If not dynamic SQL is required, but is a costly operation (usually requires parsing before each execution) so it should better not be used when standard SQL can do it.
I provided a design time example where a function returns the column name from the given the table name and column id for a varchar2 column data type to make things simple. Then a query string is constructed and executed dynymically to return all column values of the chosen table_name.column_name.
SELECT simple FROM my_tableAt compile time the simple function return value is unknown (any varchar2 value would do) you already find out you get the (same) return value (i.e column name) for each table row => dynamic SQL needed to get the column values
The purpose of function would be to rename all columns for provided table.The table name would be provided, right? If yes => dynamic SQL
What is the function supposed to return the column name (where would the new name come from?), the column alias (which table column would be renamed to the new name?)
The user could use the new_column name as the column alias name submitting the query.
Is it possible to do this?Maybe () using a pipelined function (different data types - number,date, ... not cosidered yet) but your simple query;
<tt>SELECT simple FROM my_table</tt>
might look like:
<tt>select my_column_value new_column_name from table(get_column_value(table_name,column_name))</tt>
Sorry, no Database at hand to provide a specific example.
Regards
Etbin -
Call a C function in select statement
I have a C function FUN1(int,int) in a c file utility.c.
I want to use this function in PL/SQL or Pro*c in the select statement as
Select col1,col2,fun1(col3,col4)r_fun1
from table_1;
Can any one tell the way to do this.
Rergards,You need to implement the C routine as an external procedure. There is a whole chapter on this in the online docs here.
But in outline...
(1) Copy the compiled C code to a suitable location. Note that in 9.2 or lower Oracle is pretty fussy about this, and you might have configuration issues - check the docs.
(2) Create an Oracle LIBRARY object for this routine.
(3) Create a PL/SQL wrapper for the routine using the library.
Cheers, APC -
Functions in select statements - time consuming?!
Hi All,
I have a fairly complex view that uses a char based date (last business day) as one of the selection criteria. We have a report (based on the view) that needs to be run on a daily basis but the date will be different each day. I don't want to have to change the view every day to modify the date. The date is part of a larger text column and we're using a 'LIKE %dd/mm/yyyy%' to identify it in the select statement.
I wrote a small function that returns the char based date with the %'s attached and have tested it on a small table. It works fine, however when I tried it on a larger table (50,000 rows) the difference between the response times of the following statements was unusable
select count(*) from table where x like '%dd/mm/yyyy%' (420msec)
and
select count(*) from table where x like get_date_function (52 sec)
I imagine that this is due to the function being called once for every row of the table. This isn't really going to be any good for me as the table I need to run the complex view against has nearly 750k rows and is growing every day.
Any thoughts or suggestions as to how I can solve this problem (or work around it) would be much appreciated.
rgds
JohnThanks Andrew,
The rebuild the view on a daily basis seems to be the favorite solution at the moment. Its easy to do and will solve the problem, not elegantly but it will work.
My understanding of calling functions inline from SQL is that the function will be called for every row that the SQL will reference regardless of what the function does.
So if I wanted to do...
select * from tablea where cola = sysdate-1
on a table with 500 rows, sysdate would be called 500 times.
If I was writing a procedure/package I would assign sysdate-1 to a variable and then do the select against the variable resulting in the sysdate function being called once and result in the query executing much faster.
I can't help thinking that there must be a way of doing this, but then maybe I'm just being optimistic.
I suppose what I am really trying to get is a way of calling a function once in some SQL regardless of how many rows are being referenced by the query.
rgds
John
PS the code for the function is very simple, the performance hit is being caused by it being called once for every row in the table. The table it needs to run on is just under 1 million rows, which causes a significant lag ;). -
Function within SELECT statement
Help please...
How can I get the my custom RFCal function to work within
this select statement??
My Error:
The value SUM((a2.act_rate + a2.act_gratuity) *
ap2.actpac_quantity) cannot be converted to a number.
My Code:
SELECT outer select blah blah,
(SELECT #RFCal("SUM((a2.act_rate + a2.act_gratuity) *
ap2.actpac_quantity)" + p2.pac_serviceFee, p2.pac_occupancy)#
FROM tablep2 p2 INNER JOIN tableap2 ap2 ON p2.pac_id =
ap2.pac_id
INNER JOIN tablea2 a2 ON ap2.act_id = a2.act_id
WHERE p2.pac_id = p.pac_id) AS myTotal
FROM tablep p INNER JOIN tableap ap ON p.pac_id = ap.pac_id
INNER JOIN tablea a ON ap2.act_id = a.act_id
WHERE outer select blah blahThanks for responding Dan.
Your option of running the function on the result via a
cfloop is what I am currently doing... problem is I want to be able
to sort the function result. So with that, what would be the
simplest method?
I will test running an additional query of queries on the
result. -
Output of value returned from function in SELECT statement ??
Hi
I have created the below function
create or replace
FUNCTION jc_test
RETURN VARCHAR2
IS myrec VARCHAR2(270);
BEGIN
SELECT RPAD('*',270,'*')
INTO myrec
FROM DUAL ;
RETURN myrec;
END ;
and I executed the SELECT statement in Oracle SQL developer as a script
select
LENGTH(jc_test()) len
*,jc_test() rec*
from dual ;
I get exact output as below
LEN REC
270 ******************************************************************************************************************************************************************************************************************************************************************************
So here LEN is correctly shown as 270 characters but when I see the astrisk's (also appended with spaces ) its total length is 4000 characters
Can anyone give their thoughts on this.
Its fine if I run as normal, i mean not as a script.
Regards
jc
Edited by: JC on Jun 16, 2011 11:25 AMHi,
So here LEN is correctly shown as 270 charactersYes, correct
but when I see the astrisk's (also appended with spaces ) its total length is 4000 charactersNo buts. It is not padded. That is just how it is displayed by your tool. In SQL*Plus this is controlled by LINESIZE and COLUMN
SQL> create or replace function dummy return varchar2 as begin return null; end;
2 /
Function created.
SQL> select dummy from dual;
D
X
SQL> select dummy() from dual;
DUMMY()
SQL> col "dummy()" for a10
SQL> select dummy() from dual;
DUMMY()
SQL>P.S: It is a really bad idea to create your own wrapper functions, built-in functions. Hopefully you are not really doing that?
Regards
Peter
Maybe you are looking for
-
Protecting a schema???
Hello! I just created a 9i schema and it contains a few tables, triggers, views, etc...however I'm not ready for it to go live just yet. How can I make this schema read-only even to the schema owner? Is this possible? TIA! -orasyb
-
SCCM 2012 Proof %appdata%
Hello, i wnat to copy some settings to the current logged user within an application package. The proofing should check if the file is store under %appdata% folder of the users. I added this as check in Apllication but it failes
-
What reasons would my movie render but then freeze up during the burn to disc?
So now I have a 64 minute movie edited, music in parts, menus made, ready to burn to a DVD. I go to publish & share > choose save to disk > DVD> I want to burn it to 9 disks so set copies to 9> it is about 4 gigs so I set it to highest quality> then
-
F111- Error paying Bank to Bank transfer payment request
Hi, We recently applied support pack 640 in ECCF. Now we are unable to pay the bank to bank payment request via F111. We get error message KM183. Our ABAPer debuged the program(F111) and the ACCIT table gets incorrect profit centre for the paying com
-
Java web service using pl/sql DB package - JDev 10.1.3.1
Hi All, I wanted to create a java web service, instead of a pl/sql web service, that calls a pl/sql package in my DB. Jdev (10.1.3.1) can generate java code for my pl/sql package. But the generated code can't be used with a java web service. I've fou