IF function in a Select
I would like to know if there is the possibility to use an IF function into a Select.
I would like to count a column and IF this counter is greater than 1 the select should return a NULL value,or a value that I can give, IF the counter is
0 then the select should give the value of the column.
The column is a CLOB data
:)
A part of the select I have is:
SELECT PCMS_CHDOC_DATA.CH_DOC_ID,
PCMS_CHDOC_DATA.CH_UID,
PCMS_CHDOC_DATA.CH_DOC_TYPE,
PCMS_CHDOC_DATA.STATUS,
PCMS_CHDOC_DATA.TOOL,
PCMS_CHDOC_DATA_2.CH_DOC_ID,
PCMS_CHDOC_DATA_2.CH_UID,
PCMS_CHDOC_DATA_2.CH_DOC_TYPE,
PCMS_CHDOC_DATA_2.STATUS,
PCMS_CHDOC_DATA_2.TOOL,
PCMS_CHDOC_DATA_2.DROP_TRIGGER,
PCMS_CHDOC_DETAIL_DESC.CH_DOC_ID,
PCMS_CHDOC_DETAIL_DESC.CH_UID,
PCMS_CHDOC_DETAIL_DESC.FILE_VERSION,
COUNT(CAST(PCMS_CHDOC_DETAIL_DESC.DATA AS VARCHAR2(3500)))
FROM PCMS_CHDOC_DATA,
PCMS_CHDOC_DATA PCMS_CHDOC_DATA_2,
PCMS_CHDOC_DETAIL_DESC
WHERE PCMS_CHDOC_DATA.TOOL='ICD'
AND PCMS_CHDOC_DATA.CH_DOC_ID = PCMS_CHDOC_DATA_2.DROP_TRIGGER
AND PCMS_CHDOC_DATA.CH_DOC_ID = PCMS_CHDOC_DETAIL_DESC.CH_DOC_ID
GROUP BY PCMS_CHDOC_DATA.CH_DOC_ID,
PCMS_CHDOC_DATA.CH_UID,
PCMS_CHDOC_DATA.CH_DOC_TYPE,
PCMS_CHDOC_DATA.STATUS,
PCMS_CHDOC_DATA.TOOL,
PCMS_CHDOC_DATA_2.CH_DOC_ID,
PCMS_CHDOC_DATA_2.CH_UID,
PCMS_CHDOC_DATA_2.CH_DOC_TYPE,
PCMS_CHDOC_DATA_2.STATUS,
PCMS_CHDOC_DATA_2.TOOL,
PCMS_CHDOC_DATA_2.DROP_TRIGGER,
PCMS_CHDOC_DETAIL_DESC.CH_DOC_ID,
PCMS_CHDOC_DETAIL_DESC.CH_UID,
PCMS_CHDOC_DETAIL_DESC.FILE_VERSION
but it doesn't work because of the CLOB data.
At the moment I didn't write the CASE bacause I would like first to convert the CLOB data.
I wrote VARCHAR2(4000) but it doesn't work,
The select I would like is that if the count return 1 the value is returned instead if the counter is greater than 1 is returned a null or a string more or less like in COALESCE function
Similar Messages
-
Calling a function in the select part of a query
I am integrating Apex with E-Bus Suite 11.5.10.2.
I have a function in a sql query. The function calls an apps view. When i run the sql query in TOAD, I get the correct data. When I run it in Apex, i get an error - it says "Invalid number"
My apex report is registered in my 11i menu. Who is the user that is running this report? Is it apps?
thanksWhen you run it in TOAD, you are passing through a value, correct? Maybe when your EBS call to your APEX report is done, it is passing through a character value as apposed to a number. You might want to convert your passed in value in your function to a number to be sure..
Also, it is not always the best idea to call a function in a select statement.. Is there a reason you can't do in sql what your function is doing?
Thank you,
Tony Miller
Webster, TX -
Limitations in using a function within a select statement
I have a function which retrieves various data elements from the
database and formats it accordingly. The data (varchar2)
returned could be in excess of 2,000 characters length.
I need to use the returned data as part of a view. I am able to
use the function in a "select" statement, but when I use it with
returned data in excess of 2,000 chars, I get the following
error:
ORA-06502: PL/SQL: numeric or value error
This error is occurring whenever the returned data is in excess
of 2,000 characters.
Is there an alternative method to what I am proposing, I have
tried alternative data types but if I am able to use it in a
"select" statement, I get the above error when the returned
length exceeds 2,000 chars.
Thanks
Peterare u using oracle 7. varchar2 limit in 8 is 4000.
-
How to pass parameter in Function by using select statement?
Hi,
I got a problem. I cant pass in parameter to function by using select statement. But it can pass in parameter by using 'hardcode' method. How can I solve this problem?
Eg,
select * from table (SplitFunction('HS750020,HS750021')) <<< this work.
but
select * from table (SplitFunction(select LOT_NO from TRACER_SEARCH_SCHEDULE where JOB_ID = '36')) <<< do not work.
Thanks for who try to help. Thanks.skymonster84 wrote:
I have try this before. But it not work.here is an example
create or replace type stringlist as table of varchar2(100)
create or replace function splitstring(pstring in varchar2) return stringlist
as
lstringlist stringlist;
begin
select regexp_substr(pstring,'[^,]+',1, level) bulk collect into lstringlist
from dual
connect by level <= length(pstring)-length(replace(pstring,','))+1;
return lstringlist;
end;
select * from table(select splitstring('xx,yy,zz') from dual)
create table t(str varchar2(100))
insert into t values('x,y,z')
insert into t values('a,b,c')
select * from table(select splitstring(str) from t where rownum<2)
/If you supply multiple values then it will fail.
select * from table(select splitstring(str) from t)
/ -
Will a explain plan consider a function in a select statement.
Hi gurus,
I have a question regarding explain plan.
I ran a query, which returns me explain plan with multiple CPU costs around 300K.
now i use a function, and the number of lines displayed in explain plan reduces from 12 to 8 lines.
What i dont understand is.. Is the explain plan considering the function in the select statement ?
ex.
explain plan for
select column1,
column2,
function(value1)
from case
where case_no = 1will a explain plan consider the functions in a select statement ?
Thank you.What i dont understand is.. Is the explain plan considering the function in the select statement ?Maybe there are tweaks which reveal more information from the explain plan, but a straightforward way won't necessarily expose any function call:
SQL> create or replace function get_dname (i_deptno integer)
return varchar2
as
l_dname dept.dname%type;
begin
select dname
into l_dname
from dept
where deptno = i_deptno;
return l_dname;
end get_dname;
Function created.
SQL> explain plan
for
select ename, deptno, get_dname (deptno) dname
from emp
where deptno = 10
Explain complete.
SQL> select * from table (dbms_xplan.display ())
PLAN_TABLE_OUTPUT
Plan hash value: 3956160932
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 5 | 45 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 5 | 45 | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
1 - filter("DEPTNO"=10)
13 rows selected.
SQL> truncate table plan_table
Table truncated.
SQL> explain plan
for
select ename, deptno
from emp
where deptno = 10
Explain complete.
SQL> select * from table (dbms_xplan.display ())
PLAN_TABLE_OUTPUT
Plan hash value: 3956160932
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 5 | 45 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 5 | 45 | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
1 - filter("DEPTNO"=10)
13 rows selected. -
How to pass an array to a function from a SELECT statement
Hi all. I have a problem with passing an array to a function directly from a SELECT statement.
Here is what I want. If I have a function
function AAA(arrayVar <ArrayType>) return number;
I want to be able to call this function this way
select AAA((2,3,4))
from dual
or this way
select AAA((10,12))
from dual
In other words I want to be able to pass an arbitrary number of numbers to the function. And I want this to work in a SELECT statement.
Does anyone have any ideas how to implement this? What <ArrayType> should I use?(I've read about VARRAY, nested tables in the Oracle documentation but as far as I've understood these array types are meant to be used within PL/SQL blocks).
I found only this http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:208012348074 through Google but it didn't help me.
Thank you in advance.> What <ArrayType> should I use?
SQL data types - as 3360 showed above. You cannot use PL/SQL structures and user types in the SQL Engine.
You can however use all SQL structures and types in PL/SQL.
Arrays in SQL is created as collection type - basic o-o. The collection type (or class) serve as a container for instantiated objects or scalar type.
This is covered in detail in [url http://download-uk.oracle.com/docs/cd/B19306_01/appdev.102/b14260/toc.htm]
Oracle® Database Application Developer's Guide - Object-Relational Features -
Use a package function inside a select of the same package
Hi.
How can i use a function defined inside a package, with a select used inside that same package ?
Example
... package example
CREATE OR REPLACE PACKAGE BODY pkg_example
AS
FUNCTION sum_two_values(p_val1 NUMBER,p_val2 NUMBER) RETURN NUMBER
IS
BEGIN
RETURN p_val1 + p_val2;
END sum_two_values;
FUNCTION use_another_function_example RETURN VARCHAR2
IS
v_value NUMBER;
BEGIN
SELECT sum_two_values(2,2) INTO v_value FROM dual;
RETURN 'waaaazzzzupppppp'
END use_another_function_example;
END pkg_example;
This will not work
-- SELECT sum_two_values(2,2) INTO v_value FROM dual;
How can i call a function inside a select statement, a function of the same package where the query is being called.
(i have Oracle 9.2x)
Cheers.are you sure? you are not using package1, you are using pack1
SQL> CREATE OR REPLACE PACKAGE package1
2 IS
3 FUNCTION f1
4 RETURN NUMBER;
5
6 FUNCTION f2
7 RETURN NUMBER;
8 END;
9 /
Package created.
SQL> CREATE OR REPLACE PACKAGE BODY package1
2 IS
3 a NUMBER;
4
5 FUNCTION f1
6 RETURN NUMBER
7 IS
8 BEGIN
9 RETURN 1;
10 END;
11
12 FUNCTION f2
13 RETURN NUMBER
14 IS
15 BEGIN
16 SELECT package1.f1
17 INTO a
18 FROM DUAL;
19
20 RETURN a;
21 END;
22 END;
23 /
Package body created.
Why not just this
SQL> ed
Wrote file afiedt.buf
1 CREATE OR REPLACE PACKAGE BODY package1
2 IS
3 a NUMBER;
4 FUNCTION f1
5 RETURN NUMBER
6 IS
7 BEGIN
8 RETURN 1;
9 END;
10 FUNCTION f2
11 RETURN NUMBER
12 IS
13 BEGIN
14 a:= f1;
15 RETURN a;
16 END;
17* END;
SQL> /
Package body created.formatted
Message was edited by:
devmiral -
I have a function which returns the name of a client when given
the id of the customer as an input parameter. I want to call the
function inside a select with the customer id as the input
parameter not the actual id and get the name something like
select id_cust as "id", test_function(id_cust) as "surname"
from customers
where -----
Please help because I have tried and I get an error message
ora-14551 Cannot Perform DML Inside QueryIt would help if you would post your table structure and the code for creating your function. It sounds like your function is doing something other than what you have described that you want it to do, like some unnecessary DML. See if you can use the following example:
SQL> CREATE TABLE customers
2 (id_cust NUMBER,
3 last_name VARCHAR2 (30))
4 /
Table created.
SQL> INSERT INTO customers
2 VALUES (1, 'TEST1')
3 /
1 row created.
SQL> INSERT INTO customers
2 VALUES (2, 'TEST2')
3 /
1 row created.
SQL> CREATE OR REPLACE FUNCTION test_function
2 (p_id_cust IN NUMBER)
3 RETURN VARCHAR2
4 AS
5 v_surname customers.last_name%TYPE := NULL;
6 BEGIN
7 SELECT last_name
8 INTO v_surname
9 FROM customers
10 WHERE id_cust = p_id_cust;
11 RETURN v_surname;
12 END test_function;
13 /
Function created.
SQL> COLUMN "surname" FORMAT A30
SQL> SELECT id_cust AS "id",
2 test_function (id_cust) AS "surname"
3 FROM customers
4 WHERE 1 = 1
5 /
id surname
1 TEST1
2 TEST2 -
Hi
can someone please show me how to call a function from a select* statement.
Thanks
Helenaselect age
from show_details
where age=func_test();
Here the function returns some value that function definition is like
CREATE OR REPLACE FUNCTION func_test RETURN NUMBER
AS
lv_a NUMBER;
BEGIN
lv_a := 0;
lv_a := lv_a + 1;
RETURN lv_a;
dbms_output.put_line(lv_a);
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error is '||SQLERRM);
END;
This is to manually call a function
DECLARE
v_a NUMBER;
BEGIN
v_a := func_test();
dbms_output.PUT_LINE(v_a);
END;
************The o/p is '1'.
If the show_details table contains the age value as 1 then the field will be selected by calling the function. -
Can call a function in the select statement?
Is there any ways to call a function in the select statement?
what I like to do is this:
select deptno, totalEmployees(deptno), TotalSalary(deptno)
from emp;
I know it can be done by count(*) and join tables, but my case
is much more complex and the where clauses are different from
one function to another, and have many tables to join and many
combinations
ThanksFunctions can be used in a select statement subject to certain
restrictions, see
http://otn.oracle.com/docs/products/oracle8i/doc_library/817_doc/
server.817/a85397/statem9b.htm#2062024
It's under "CREATE FUNCTION> Keywords and Parameters> function>
Restrictions on User-Defined Functions"
Here is an except...
When a function is called from within a query or DML statement,
the function cannot:
a) Have OUT or IN OUT parameters
b) Commit or roll back the current transaction, create or roll
back to a savepoint, or alter the session or the system. DDL
statements implicitly commit the current transaction, so a user-
defined function cannot execute any DDL statements.
c) Write to the database, if the function is being called from a
SELECT statement. However, a function called from a subquery in
a DML statement can write to the database.
d) Write to the same table that is being modified by the
statement from which the function is called, if the function is
called from a DML statement.
Except for the restriction on OUT and IN OUT parameters, Oracle
enforces these restrictions not only for the function called
directly from the SQL statement, but also for any functions that
function calls, and on any functions called from the SQL
statements executed by that function or any function it calls. -
Using an own function in a select how can i set that the function run once?
Hi
Using an own function in a select how can i set that the function run once, not in every row?
Please help me
Thanks
ViktorSomething like this ?
SQL> select * from dept;
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> create or replace function ret_name (deptnum in number) return varchar2
2 is
3 name varchar2(50);
4 begin
5 select dname into name
6 from dept
7 where deptno=deptnum;
8 return name;
9 exception
10 when no_data_found then
11 return('Not existent deptno');
12* end;
SQL> /
Function created.
SQL> select deptno, decode(rownum,1,ret_name(deptno),null) dname from dept;
DEPTNO DNAME
10 ACCOUNTING
20
30
40
SQL> -
Standard function modules for selection from vbak/vbup/ekko/ekpo
hi experts ,
do you know if there is an existing standard function modules for selection from vbak/vbup/ekko/ekpo.
please helpHi,
for VBAK
ADSPCM_READ_VBAK Read VBAK (with SPEC2KM-data)
PRS_GET_GLOBAL_VBAK Get global structure VBAK
Regards,
Jyothi CH.
Edited by: Jyothi Chinnabathuni on Feb 23, 2009 2:46 PM -
Function Module to select all entries in ALV output
Hi,
I have an ALV report and Select ALL and Deselect ALL Buttons added to it .
When the Select ALL button is pressed,all entries of the ALV report should be selected.
When the Deselect ALL button is pressed,all entries of the ALV report should be deselected.
Is there any function module to select all entries of the output and vice versa.
Thanks in advance.
Regards,
NavasHi Navas,
I have written a part of the code you can use.
TYPE-POOLS: slis.
DATA : ls_event TYPE slis_alv_event.
Set PF Status
ls_event-name = slis_ev_pf_status_set .
ls_event-form = slis_ev_pf_status_set .
APPEND ls_event TO gt_event.
CLEAR ls_event.
FORM pf_status_set USING rt_extab TYPE slis_t_extab.
SET PF-STATUS 'Z_STATUS'.
ENDFORM . "set_pf_status
(no need for any perform. This will be called automatically).
While calling the FM for ALV, pass the internal table gt_event in it_events parameter.
Double click on Z_STATUS and add two buttons in Application Toolbar.
1. Select All - Give function code name as &ALL,
2. Deselect All - Give function code name as &SAL.
Hope this helps.
Thanks,
Dawood.
Edited by: Dawood.S.Ghasletwala on Apr 3, 2009 3:51 PM -
Call function after AutoSuggest select
Hello,
Looking at the
Auto-suggest
Overview I'm wondering if any clever folks out there can tell
me the best way to call a function after a user has selected an
item from the list (preferably by either mouse or keyboard). The
function takes the selected value as an argument.
On a related note does anyone think that
Spry.Widget.AutoSuggest would benefit from a finish option like the
apry effects have?
Cheers,
PeterHi Cristian,
Thanks for your reply.
Firstly, I forgot to say in my previous post how great I
think Spry is, I've played with some other frameworks in the past
but Spry is the one that has really clicked with me.
The future developments look very interesting, I'll be
keeping my eye on Spry.
As for the what I mean by the finish: callback function, it
occurs to me that many people may want to have spry do 'x' after
the user has selected something from the AutoSuggest list. In my
case I want to filter a recordset based on what the user has just
selected, this can be done easily by having them click a button
that calls a function after they have made their selection but
having the option of this happening 'automatically' would be a real
boon.
I don't really know anything about advanced JS so I don't
know if something like this would be possible. E.g.:
var ac6 = new Spry.Widget.AutoSuggest("productSampleDiv",
"productMenuDiv", "dsProducts3", 'name', {containsString: true,
minCharsType: 2, finish: function x() }); -
Why a function within a SELECT FROM DUAL is faster?
1)I have a SELECT with a function. Ex:
"SELECT DISTINCT function(...) FROM table"
2)I changed the SELECT and put the function inside a SUB-SELECT with FROM DUAL. Ex:
"SELECT DISTINCT (SELECT function(...) FROM DUAL) FROM table"
3)The second SELECT is faster than the first.
I discovered this problem on my tables and my functions. So I did a generic example for this forum with a common function and large table. My results:
Table has 2.196.058 records and the field is a VARCHAR2:
SELECT DISTINCT SUBSTR(field, 2) FROM Table -> Executed in 110 seconds
SELECT DISTINCT (SELECT SUBSTR(field, 2) FROM DUAL) FROM Table -> Executed in 39 seconds
Why "SELECT DISTINCT (SELECT function(...) FROM DUAL) FROM table" is faster than "SELECT DISTINCT function(...) FROM table"
thanks,
FernandoHi hoek,
I followed your informations and, sorry, I am more confused! I did an interesting example below.
1)I created a table:
CREATE TABLE T
name VARCHAR2(50)
); 2)I inserted 10 lines:
NAME
A
B
C
D
E
F
G
H
I
J 3)I created a function that returns the parameter concatenated with the current millisecond:
CREATE OR REPLACE FUNCTION f (param1 IN VARCHAR2) RETURN VARCHAR IS
BEGIN
return param1 || '-' || to_char(CURRENT_TIMESTAMP, 'FF');
END; 4)The query(A): SELECT f(name) AS col1, f(name) AS col2, f(name) AS col3 FROM t; gave me:
COL1 COL2 COL3
A-693786000 A-693887000 A-693941000
B-693989000 B-694017000 B-694043000
C-694071000 C-694097000 C-694124000
D-694153000 D-694180000 D-694206000
E-694235000 E-694261000 E-694287000
F-694316000 F-694341000 F-694367000
G-694396000 G-694422000 G-694448000
H-694477000 H-694503000 H-694529000
I-694557000 I-694583000 I-694609000
J-694638000 J-694664000 J-694690000If I repeat the SELECT, new values are generated.
4)The query(B): SELECT (SELECT f(name) FROM Dual) AS col1, (SELECT f(name) FROM Dual) AS col2, (SELECT f(name) FROM Dual) AS col3 FROM t; gave me:
COL1 COL2 COL3
A-253546000 A-253643000 A-253746000
B-253791000 B-253821000 B-253850000
C-253881000 C-253909000 C-253937000
D-253969000 D-253997000 D-254026000
E-254057000 E-254085000 E-254113000
F-254145000 F-254173000 F-254202000
G-254232000 G-254261000 G-254289000
H-254320000 H-254348000 H-254376000
I-254407000 I-254436000 I-254464000
J-254495000 J-254523000 J-254551000The result generated new values too.
5)I changed the function and I put the DETERMINISTIC clause:
CREATE OR REPLACE FUNCTION f (param1 IN VARCHAR2) RETURN VARCHAR DETERMINISTIC IS
BEGIN
return param1 || '-' || to_char(CURRENT_TIMESTAMP, 'FF');
END; 6)I repeated the queries(A) and (B) and the result has generated different values. Then, with the DETERMINISTIC clause, the result has not changed
7)I changed the function and I put the RESULT_CACHE clause:
CREATE OR REPLACE FUNCTION f (param1 IN VARCHAR2) RETURN VARCHAR RESULT_CACHE IS
BEGIN
return param1 || '-' || to_char(CURRENT_TIMESTAMP, 'FF');
END;8)I repeated the query(A) and now, the result was different:
COL1 COL2 COL3
A-381048000 A-381048000 A-381048000
B-381242000 B-381242000 B-381242000
C-381322000 C-381322000 C-381322000
D-381400000 D-381400000 D-381400000
E-381481000 E-381481000 E-381481000
F-381556000 F-381556000 F-381556000
G-381634000 G-381634000 G-381634000
H-381815000 H-381815000 H-381815000
I-381895000 I-381895000 I-381895000
J-381971000 J-381971000 J-381971000The line columns are equals and if I repeat the SELECT, all the result is the same too. The query(B) returned the same result like the query(A)! Now I see the cache in action!
After the tests, I concluded that the function in the subquery doesn't use cache and it is faster than the normal function. The mistery continues...
Maybe you are looking for
-
IMovie won't open, is this a leopard problem or a application problem?
Every time I try and open imovie, I get the "application quit unexpectedly" before the program even launches. I reinstalled ilife but same result. all my other applications work fine, so I don't really know what to do. Any suggestions?
-
IPhoto'11 - a disappointment.
hello all! even though i use iPhoto privately, i have some experience in organizing of and working with a large amount of photos in my job. also i would call myself as a rather advanced computer user. recently i got this new macbook pro and decided t
-
TB 31.1.2 automatically checks for new mail after entering TB password. It then reports, for example, that it is downloading "3 new unread messages" (or whatever number of messages). When complete, the messages are nowhere to be found either in the T
-
Hi, I've created a process which is assigned to different persons/groups. First there is only one attachment for the task but in one of the steps the process provides 2 attachments one is read only the other is read/write able. The user is able to ad
-
I am investigating issue with authenticating with WinRM (CredSSP) and I've followed the procedure described in this blog: http://blogs.technet.com/b/jonjor/archive/2009/01/05/how-to-capture-a-winrm-trace.aspx However, I do not get any meaningful info