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
Srini
Srini
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
Similar Messages
-
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 -
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 -
Using TRIM function in select statement
Hi All,
I'm using the TRIM function in my select statement to eliminate the white spaces.
But while using in select the TRIM function is not working in SQL*PLUS client(The query returns the white spaces also)
Kindly provide some pointers regarding the issue.........
I want to get only the data without the spaces in select statement
Regards,
MohanHi, Mohan,
SQL*Plus always pads columns to make them line up nicely.
If you have a column declared as VARCHAR2 (20), then SQL*Plus will normally display 20 characters for that column, even in the maximum actual length is, say, 5 (or even if the column always happens to be NULL).
If you want the output to include only the actual data, without the padding that SQL*Plus adds, then concatenate all the columns into one big string column.
People often do something like the following to generate a CSV file, with no exta spaces:
SELECT TO_CHAR (empno)
|| ',' || ename
|| ',' || job
|| ',' || TO_CHAR (deptno)
|| ',' || TO_CHAR (hiredate, 'DD-Mon-YYYY') AS all_data
FROM scott.emp; -
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 ;). -
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 -
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. -
Calling PL/SQL code from Select statement
Hi
I have a PL/SQL function to calculate a value.
create or replace procedure "SR_GROSS_MARGIN"
(netsales IN NUMBER,
margin IN NUMBER,
GM OUT NUMBER)
is
BEGIN
IF NETSALES = 0 THEN
GM := 0;
ELSIF
NETSALES < 0 THEN
GM := 0;
ELSE
GM := NETSALES / MARGIN;
END IF;
END;How do I call this from a SELECT statement?
Regards
Adamhere you go:
create or replace function SR_GROSS_MARGIN
(netsales IN NUMBER,
margin IN NUMBER)
return number
is
gm number;
BEGIN
IF NETSALES = 0 THEN
GM := 0;
ELSIF
NETSALES < 0 THEN
GM := 0;
ELSE
GM := NETSALES / MARGIN;
END IF;
return gm;
END;then you can:
select gm(2500,20) from dual; -
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 HANA SQL Functions in select list of CDS Views
Dear Expert,
Kindly please let me know if we can use the HANA SQL Function (Ex: ADD_DAYS, SECONDS_BETWEEN) in the select list of CDS Views?
If I create a CDS like below I get error that timestamp is not supported.
For Example:
@AbapCatalog.sqlViewName: 'ZMR_H_CA'
@EndUserText.label: 'CAG A'
define view viewname
with parameters start_ts:abap.dec( 15, 0 ) , end_ts:abap.dec( 15, 0 )
as select from table {
key resource_key,
TO_TIMESTAMP(begtstmp) as start_tmp,
TO_TIMESTAMP(begtstmp) as end_tmp
where
(begtstmp > $parameters.start_ts or endtstmp > $parameters.start_ts )
and
(begtstmp < $parameters.end_ts or endtstmp < $parameters.end_ts )
Thanks,
GiriHi Giri,
the list of provided features can be found in the ABAP Language Documentation (F1 in the CDS View).
As CDS in ABAP is abstracted from the database layer, the database features are not directly accessible. That means, you cannot use any HANA or MaxDB feature available. So the answer to your question is no. The reason for this is, that ABAP CDS views can be created on all SAP-supported databases, hence, we can only provide those features, supported by all databases.
Having said this, there are some exception, e.g. the CDS views with input parameters, which are not supported by all databases. In these cases, you'd have to additionally use the utility class CL_ABAP_DBFEATURES (see http://scn.sap.com/community/abap/blog/2014/10/10/abap-news-for-740-sp08--abap-core-data-services-cds) for more details.
Best,
Jasmin -
Sql Error in Select statement when doing subquery
Hi,
I am trying to see what the error is in the subquery part of the select statement.
Subquery should be fetching the safety_stock_quantity based on the MAX(effectivity_date).
Any suggestions?
SELECT kbn.last_update_date,itm.segment1,itm.description,kbn.kanban_card_number,kbn.kanban_size,
(SELECT msc.safety_stock_quantity
FROM mtl_safety_stocks msc
WHERE msc.effectivity_date = (select MAX(msc2.effectivity_date)
from mtl_safety_stocks msc2
where msc2.inventory_item_id = itm.inventory_item_id
and msc2.organization_id = itm.organization_id)
AND msc.inventory_item_id = itm.inventory_item_id
AND msc.organization_id = itm.organization_id
FROM mtl_system_items_b itm
,mtl_onhand_quantities_detail moqd
,mtl_safety_stocks msc
,mtl_kanban_card_activity kbn
WHERE itm.inventory_item_id = kbn.inventory_item_id
AND itm.organization_id = kbn.organization_id
AND itm.inventory_item_id = moqd.inventory_item_id
AND itm.organization_id = moqd.organization_id
AND moqd.subinventory_code = kbn.source_subinventory
AND kbn.card_status = 1
AND kbn.supply_status = 5
AND msc.inventory_item_id = itm.inventory_item_id
AND msc.organization_id = itm.organization_id
GROUP BY
kbn.last_update_date,itm.segment1,itm.description,kbn.kanban_card_number,kbn.kanban_size;
Thanks
PravnHi, Pravn,
Remember the ABC's of GROUP BY:
When you use a GROUP BY clause and/or an aggregate fucntion, then every item in the SELECT clause must be:
(A) an <b>A</b>ggregate function,
(B) one of the "group <b>B</b>y" expressions,
(C) a <b>C</b>onstant, or
(D) something that <b>D</b>epends entirely on the above. (For example, if you "GROUP BY TRUNC(dt)", you can "SELECT TO_CHAR (TRUNC(dt), 'Mon-DD')").
There's a GROUP BY clause in your main query, so every item in the main SELECT clause must be one of the above. The last item, the unnamed scalar sub-query, is none of the above.
How can you fix this problem? That depends on your data, the results you want, and perhaps on your Oracle version. If you'd like help, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved. Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
Always say which version of Oracle you're using.
You may have noticed that this site normally doesn't display multiple spaces in a row.
Whenever you post formatted text (including, but limited to, actual code) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
Maybe you are looking for
-
Hi to all Please tell me in which table Customizing requests are stored. So that i can find out which user has generated request with object name. Regards Anubhav
-
Unable to get backed up data in pages after iCloud restore
hi friends, i restored my ipad from an i iCloud backup. i am able to see all data restored including numbers but not of pages. when i go to the iCloud tab, i am seeing all the documents listed, but they don't appear in pages. any guidance on this wil
-
I have been fighting this on my wife's iMac (Intel Core 2 Duo, OS X 10.6.7) for about the past year, and nothing seems to help. I maxed out the RAM (from 2 GB to 4 GB) and it seemed to help for a while, but it's back. I've tried numerous hardware tes
-
Taxline itmes at Business Area level
HI Gurus, My client wants Tax line itmes at Busienss Area level. Please help me on validation and substitutions to post taxline itmes at business area level. Below is the Scenario: Input Tax Purchase A/c--------------Dr (Business Area) Input Tax A/c-
-
Infopackage:Determine the variables for periodic data loading
Hi, we have an infopackage for full update and we have set in "Data selection" tab as selection , an OLAP variable. If I do the load using the OLAP variable selection, 12 last months , I need to delete data can be duplicated. But for example, If I d