Functions in where clauses vs. in group-by ... inconsistent?
Hello all.
(assuming Oracle 9.2)
First of all, does anyone know why the Oracle SQL Parser will not allow
us to use the <name>-part of "table.column AS <name>" in where or
group-by clauses?? Is this just being lazy on their part, or is there
an actual reason behind this?
Second, and relating to this I am confused as to how ORACLE handles
group-by clauses with functions. (Or rather I find it a bit
inconsistent.)
a)
select my_func(col1), col2
from a_table
where my_func(col1) = :filter;
==> This will evaluate my_func() two(2) times for every row in the
table.
b)
select my_func(col1), sum(col2)
from a_table
group by my_func(col1);
==> This will NOT evaluate the function twice(2) for every result row,
instead it is EXACTLY the same as when I would write:
c = b)
select col1_func, sum(col2)
from (select my_func(col1) col1_func, col2)
group_by col1_func
Consider also the attached example to further confuse the issue.
Any opinions, pointers, corrections on this are most welcome.
cheers,
Martin
** small example **
CREATE OR REPLACE
PACKAGE TEST_PACKAGE
IS
global_counter NUMBER;
END TEST_PACKAGE;
CREATE OR REPLACE
FUNCTION TEST_PUT(x IN VARCHAR2)
RETURN VARCHAR2 IS
ret VARCHAR2(100) := x || test_package.global_counter;
BEGIN
dbms_output.put_line(ret);
test_package.global_counter := NVL(test_package.global_counter, 0) +
1;
return ret;
END;
-- Test_Put will be evaluated 2 times(!):
select test_put('fn_called') my_string, COUNT(object_name)
from all_objects
where rownum <= 2
group by test_put('fn_called')
-- Test_Put will be evaluated 4 times(!):
select test_put('fn_called') my_string, COUNT(object_name)
from all_objects
where rownum <= 2
group by test_put('fn_call')
Hello
Hopefully this will help to explain:
SQL> create or replace package test_package
2 is
3 counter_select number;
4 counter_where number;
5 end;
6 /
Package created.
SQL> create or replace function test_func_select
2 return varchar2
3 is
4 begin
5
6 test_package.counter_select := NVL(test_package.counter_select,0) + 1;
7
8 dbms_output.put_line('test_func_select Call # '||TO_CHAR(test_package.counter_select));
9
10 return 'Call # '||TO_CHAR(test_package.counter_select);
11
12 end;
13 /
Function created.
SQL>
SQL> create or replace function test_func_where
2 return varchar2
3 is
4 begin
5
6 test_package.counter_where := NVL(test_package.counter_where,0) + 1;
7
8 dbms_output.put_line('test_func_where Call # '||TO_CHAR(test_package.counter_where));
9
10 return 'Call # '||TO_CHAR(test_package.counter_where);
11
12 end;
13 /
Function created.
SQL> set serveroutput on
SQL>
SQL> SELECT
2 test_func_select,
3 count(*)
4 FROM
5 dt_test_tab
6 WHERE
7 rownum <=2
8 GROUP BY
9 test_func_select
10 /
TEST_FUNC_SELECT COUNT(*)
Call # 2 1
Call # 1 1
test_func_select Call # 1
test_func_select Call # 2
SQL> exec test_package.counter_select:=NULL;
PL/SQL procedure successfully completed.
SQL> SELECT
2 test_func_select
3 FROM
4 dt_test_tab
5 WHERE
6 rownum <=2
7 AND
8 test_func_where LIKE 'Call #%'
9 /
TEST_FUNC_SELECT
Call # 1
Call # 2
test_func_where Call # 1
test_func_select Call # 1
test_func_select Call # 2
SQL>
SQL> create or replace function test_func_select(av_param in varchar2)
2 return varchar2
3 is
4 begin
5
6 test_package.counter_select := NVL(test_package.counter_select,0) + 1;
7
8 dbms_output.put_line('test_func_select Call # '||TO_CHAR(test_package.counter_select));
9
10 return 'Call # '||TO_CHAR(test_package.counter_select);
11
12 end;
13 /
Function created.
SQL>
SQL> create or replace function test_func_where(av_param in varchar2)
2 return varchar2
3 is
4 begin
5
6 test_package.counter_where := NVL(test_package.counter_where,0) + 1;
7
8 dbms_output.put_line('test_func_where Call # '||TO_CHAR(test_package.counter_where));
9
10 return 'Call # '||TO_CHAR(test_package.counter_where);
11
12 end;
13 /
Function created.
SQL> exec test_package.counter_select:=NULL;
PL/SQL procedure successfully completed.
SQL>
SQL> SELECT
2 test_func_select(object_name) test_func_select,
3 count(*)
4 FROM
5 dt_test_tab
6 WHERE
7 rownum <=2
8 GROUP BY
9 test_func_select(object_name)
10 /
TEST_FUNC_SELECT COUNT(*)
Call # 2 1
Call # 1 1
test_func_select Call # 1
test_func_select Call # 2
SQL> exec test_package.counter_select:=NULL;
PL/SQL procedure successfully completed.
SQL> exec test_package.counter_where:=NULL;
PL/SQL procedure successfully completed.
SQL> SELECT
2 test_func_select(object_name) test_func_select
3 FROM
4 dt_test_tab
5 WHERE
6 rownum <=2
7 AND
8 test_func_where(object_name) LIKE 'Call #%'
9 /
TEST_FUNC_SELECT
Call # 1
Call # 2
test_func_where Call # 1
test_func_select Call # 1
test_func_where Call # 2
test_func_select Call # 2In the fist example, oracle only has to call the function once to satisfy the predicate as it is not technically being applied to the table. There could be 1million rows but if the function did not return a string in the form Call #, none of the result set would be materialised as the predicate had failed. In the second example, the predicate is being applied to the table as we are passing a column from the result set to the function. This means that the where clause function has to be called once for every row in the result set.
HTH
David
Similar Messages
-
Using round off function in where clause
Hi All,
I'm trying to use round off function in where clause, I seek help in completing this script.
WITH CR_Details AS
(Select
request_id,
parent_request_id,
fcpt.user_concurrent_program_name Request_Name, phase_code, status_code,
round((fcr.actual_completion_date - fcr.actual_start_date),3) * 24 * 60 as Run_Time,
round(avg(round(to_number(actual_start_date - fcr.requested_start_date),3) * 1440),2) wait_time,
fu.User_Name Requestor,
fcr.argument_text parameters,
to_char (fcr.requested_start_date, 'MM/DD HH24:mi:SS') requested_start,
to_char(actual_start_date, 'MM/DD/YY HH24:mi:SS') ACT_START,
to_char(actual_completion_date, 'MM/DD/YY HH24:mi:SS') ACT_COMP,
fcr.completion_text
From
apps.fnd_concurrent_requests fcr,
apps.fnd_concurrent_programs fcp,
apps.fnd_concurrent_programs_tl fcpt,
apps.fnd_user fu
Where 1=1
and fcr.concurrent_program_id = fcp.concurrent_program_id
and fcp.concurrent_program_id = fcpt.concurrent_program_id
and fcr.program_application_id = fcp.application_id
and fcp.application_id = fcpt.application_id
and fcr.requested_by = fu.user_id
and fcpt.language = 'US'
and fcr.actual_start_date like sysdate )
select crd.*
from CR_Details crd
where Run_time <> '0'
AND wait_time <> '0'
GROUP BY
crd.request_id,
crd.parent_request_id,
crd.fcpt.user_concurrent_program_name,
crd.requested_start_date,
crd.User_Name,
crd.argument_text,
crd.actual_completion_date,
crd.actual_start_date,
crd.phase_code,
crd.status_code,
crd.resubmit_interval,
crd.completion_text,
crd.resubmit_interval,
crd.resubmit_interval_unit_code,
crd.description
Not sure about the GROUPBY function referencing the "crd." .Hi,
The best thing for you to do is start over. Start as small as possible, then take baby steps.
Pick one of the tables; fcr perhaps, and write a query that just uses that table, like this:
SELECT *
FROM apps.fnd_concurrent_requests fcr
WHERE fcr.actual_start_date >= TRUNC (SYSDATE)
AND fcr.actual_start_dt < TRUNC (SYSDATE) + 1
;(I think this is what you meant to do when you said "... LIKE SYSDATE".)
Make sure this little query gets credible results. When that tiny query is working perfectly, add another line or two. You can cut and paste code from what you posted, if that helps you.
If you get stuck, post the last version of your code that worked perfectly, and the latest version (only a line or two bigger) that has the problem. Describe what the problem is. If you get an error, post the complete error message. In any event, post CREATE TABLE and INSERT statements for all the tables and columns needed to run the query, and the results you want to get from that query.
When you post any code, format it, so that how the code looks on the screen gives some clues about how it is working.
When you post any formatted text on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
If you going to use the AVG function in the sub-query, then you probably need to do a GROUP BY in the sub-query.
If you're not using any aggregate functions (like AVG) in the main query, then you probably don't want to do a GROUP BY in the main query.
I know this is a lot of work. I'm sorry. If there was an easier way, I wouldn't ask you to do all this. -
Clarification on using function in where clause of oracle sql query
I have an issue in regarding function using where clause of sql query..
We are facing performance issue while executing query, so in what ways to improve the performance of the query which i have posted below.
select col ,case when my_function(parameter)
from tab1 a ,tab2 b,tabl3 c
where a.column1=b.column2
and b.column3 =c.column6
and my_function(parameter)>0
Regards
Dinesh
Edited by: wild fire on May 18, 2012 4:15 PMDinesh,
remind that when you use a function in the where clause it normally will get started for each record in the table.
So your answer is two-fold:
1. make the function only start when needed by adding a function based index on the table (this will make inserts and updates slower)
2. make the function faster by adding the DETERMINISTIC clause if possible. this will make Oracle "cache" the result of the function.
Regards,
Richard
blog: http://blog.warp11.nl
twitter: @rhjmartens
If this question is answered, please mark the thread as closed and assign points where earned.. -
Can't have aggregate function in WHERE clause clause
Dear all,
I've created object in BO XI 3.1 Designer with following criterias:
http://img4.imageshack.us/img4/833/20111201124314.th.jpg
It is a simple number - 1,2,3,4,5.
Now I need to use this object as a criteria for WHERE function in another object.
http://img607.imageshack.us/img607/1543/20111201124717.th.jpg
I receive an error "Can't have aggregate function in WHERE clause <clause>"
How can I overcome this?
P.S. I'm sorry in advance if such topic already exist - I didn't found one.
Edited by: Ashot Antonyan on Dec 1, 2011 9:50 AM
Edited by: Ashot Antonyan on Dec 1, 2011 9:51 AMHi,
You will have to use Sub query to achieve this. Give more details on what is available and what you need then i could help you out with the complete solution.
Thanks,
Ravichandra K -
Help:alternate for calling function in where clause
Hi ,
In below query i'm calling function in where clause to avoid COMPLETE status records,due to this query taking 700 secs to return result.If i'm remove below function condition it's returning results with in 5 secs.Can you some one advice to any alternate idea for this?
WHERE mark_status != 'COMPLETE'
SELECT assessment_school,
subject,
subject_option,
lvl,
component,mark_status,
mark_status
NULL AS grade_status,
NULL AS sample_status,
:v_year,
:v_month,
:v_formated_date,
:v_type,
cand_lang
FROM
(SELECT assessment_school,
subject,
subject_option,
lvl,
programme,
component,
paper_code,
cand_lang,
mark_entry.get_ia_entry_status(:v_year, :v_month, assessment_school, subject_option, lvl, cand_lang, component, paper_code) AS mark_status
FROM
(SELECT DISTINCT ccr.assessment_school,
ccr.subject,
ccr.subject_option,
ccr.lvl,
ccr.programme,
ccr.language AS cand_lang,
ccr.paper_code,
ccr.component
FROM candidate_component_reg ccr
WHERE ccr.split_session_year = :v_year
AND ccr.split_session_month = :v_month
AND EXISTS
(SELECT 1
FROM IBIS.subject_component sc
WHERE sc.year = ccr.split_session_year
AND sc.month = ccr.split_session_month
AND sc.paper_code = ccr.paper_code
AND sc.assessment_type = 'INTERNAL'
AND sc.subject_option NOT LIKE '%self taught%'
AND sc.component NOT IN ('PERFORMANCE PRODUCTION','PRESENTATION WORK','REFLECTIVE PROJECT','SPECIAL SYLLABUS INT. ASSESSMENT')
AND NVL(ccr.withdrawn,'N') = 'N'
AND ccr.mark_status != 'COMPLETE'
AND EXISTS
(SELECT 1
FROM school s
WHERE s.school_code = ccr.assessment_school
AND s.training_school = 'N'
WHERE mark_status != 'COMPLETE';One thing you can test quickly is to put the function call in it's own select ...from dual.
This might make a difference.
However, only you can check this, I don't have your tables or data.
So, what happens if you use:
paper_code,
cand_lang,
(select mark_entry.get_ia_entry_status(:v_year, :v_month, assessment_school, subject_option, lvl, cand_lang, component, paper_code) from dual ) AS mark_status
FROM
(SELECT DISTINCT ccr.assessment_school, --<< is the DISTINCT really needed?
ccr.subject,
ccr.subject_option,
...Also, try to find out the purpose of that above DISTINCT, is it really needed or is there some join missing? -
My function in where clause? help me please.
hello friends, I need to call my function in where clause as dummy example below:
declare
name_ table1.name%type;
function return_id(id number) return number is
begin
return 1;
end return_id;
begin
select name into name_
from table1
where id = return_id(table1.id);
end;
raise exception: 'the function doesn't used in where clause'. why????-- CREATING A FUNCTION AVAILABLE THROUGH A PACKAGE SPEC.
SQL> ed
Wrote file afiedt.buf
1 create package mypackage is
2 function myfunc(p_val NUMBER) RETURN NUMBER;
3* end;
SQL> /
Package created.
SQL> ed
Wrote file afiedt.buf
1 create or replace package body mypackage is
2 function myfunc(p_val NUMBER) RETURN NUMBER IS
3 begin
4 RETURN (p_val*p_val)-1;
5 end;
6* end;
SQL> /
Package body created.
SQL> declare
2 v_myval NUMBER := 2;
3 begin
4 select mypackage.myfunc(v_myval)
5 into v_myval
6 from dual;
7 dbms_output.put_line(v_myval);
8 select mypackage.myfunc(v_myval)
9 into v_myval
10 from dual;
11 dbms_output.put_line(v_myval);
12 end;
13 /
3
8
PL/SQL procedure successfully completed.
-- CREATING A STANDALONE DATABASE FUNCTION.
SQL> create or replace function myfunc2(p_val number) return number is
2 begin
3 return (p_val*2)+1;
4 end;
5 /
Function created.
SQL> ed
Wrote file afiedt.buf
1 declare
2 v_myval NUMBER := 2;
3 begin
4 select myfunc2(v_myval)
5 into v_myval
6 from dual;
7 dbms_output.put_line(v_myval);
8 select myfunc2(v_myval)
9 into v_myval
10 from dual;
11 dbms_output.put_line(v_myval);
12* end;
SQL> /
5
11
PL/SQL procedure successfully completed.
-- CREATING A LOCAL FUNCTION IN THE ANONYMOUS PL/SQL BLOCK
SQL> ed
Wrote file afiedt.buf
1 declare
2 v_myval NUMBER := 2;
3 function myfunc3(p_val number) return number is
4 begin
5 return (p_val*p_val*p_val);
6 end;
7 begin
8 select myfunc3(v_myval)
9 into v_myval
10 from dual;
11 dbms_output.put_line(v_myval);
12 select myfunc3(v_myval)
13 into v_myval
14 from dual;
15 dbms_output.put_line(v_myval);
16* end;
SQL> /
select myfunc3(v_myval)
ERROR at line 8:
ORA-06550: line 8, column 10:
PLS-00231: function 'MYFUNC3' may not be used in SQL
ORA-06550: line 8, column 10:
PL/SQL: ORA-00904: : invalid identifier
ORA-06550: line 8, column 3:
PL/SQL: SQL Statement ignored
ORA-06550: line 12, column 10:
PLS-00231: function 'MYFUNC3' may not be used in SQL
ORA-06550: line 12, column 10:
PL/SQL: ORA-00904: : invalid identifier
ORA-06550: line 12, column 3:
PL/SQL: SQL Statement ignored
SQL>As you can see (and as previously mentioned by someone else) the function has to be available to the SQL engine either via a datavbase package or as a database function. If you declare the function locally within the anonymous PL/SQL block then the function only exists in the scope of that PL/SQL block and thus when your SQL statement is sent to the SQL engine for execution, the SQL engine can't see it.
;) -
Using function in where clause
I have created a function as follows
create or replace FUNCTION get_codes RETURN varchar2 IS
scodes varchar2(50) := 'A1,A2';
BEGIN
scodes := '('''||REPLACE(scodes,',',''',''')||''')';
return scodes;
END;
this function returns ('A1','A2')
now i want to use this in where clause, both below statements fetches no rows
select * from tablea where code in (select get_codes from dual);
select * from tablea where code in get_codes;
but the following will fetch rows
select * from tablea where code in ('A1','A2')
how to use function in where clause
ThanksHi,
The code that works:
where code in ('A1','A2')is testing whther code is either of two 2-character strings, A1 or A2.
The ones that don't work, such as:
where code in get_codes;are comparing code to one 9-character string: 'A1','A2', where characters 1, 4, 6 and 9 are single-quotes. (The function can only return one value.)
You can use dynamic SQL to embed that 9-character string in part of your query, where it will be interpreted as two 2-character strrings.
[Oracle Base|http://www.oracle-base.com/articles/misc/DynamicInLists.php] and AskTom have good pages on the subject of dynamic IN-lists.
Of you can use INSTR or LIKE:
where INSTR ( get_codes
, '''' || code || ''''
) > 0 -
i insert the below rows in a table cash_book.
PRJ_CODE PRJ_NAME
1203 SHIFA
1203 SHIFA
1203 SHIFA
1202 FATIMA
1202 FATIMA
1203 SHIFA
if i select 1202 code then return 1202 code rows, if i select 1203 then returns 1203 code rows, if i select other than both values then returns all rows. how can i define in one query with use (case or decode function) in where clause ??perhaps something like this:
create table test
as
select 1200 + rownum prj_code
from dual
connect by level <= 10;
select * from test;
PRJ_CODE
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
var v_PRJ_CODE number
exec :v_PRJ_CODE := 1200;
select *
from test
where PRJ_CODE = :v_PRJ_CODE
or 1 = case when :v_PRJ_CODE in (1202, 1203) then 0 else 1 end;
PRJ_CODE
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
exec :v_PRJ_CODE := 1203;
select *
from test
where PRJ_CODE = :v_PRJ_CODE
or 1 = case when :v_PRJ_CODE in (1202, 1203) then 0 else 1 end;
PRJ_CODE
1203Regards
Martin -
How can we use DECODE function in where clause.
Hi Guys,
I have to use DECODE function in where clause.
like below
select * from tab1,tab2
where a.tab1 = b.tab2
and decode(code, 'a','approved')
in this manner its not accepting?
Can any one help me on this or any other aproach?
Thanks
-LKR>
I am looking for to decode the actual db value something in different for my report.
like if A then Accepted
elseif R then Rejected
elseif D then Denied
these conditions I have to check in where clause.
>
what are you trying to do?
may be you are looking for
select * from tab1,tab2
where a.tab1 = b.tab2
and
(decode(:code, 'A','Accepted') = <table_column>
or
decode(:code, 'R','Rejected') = <table_column>
or
decode(:code, 'D','Denied') = <table_column>
) -
Error when using record group type as param in a function in where clause
Hi folks.
I have a record type defined in a packages specification. I have to use this record as parameter to call a function. I am able to call the function if as return its value to a string variable. However, the function should be called into a query to compare the returned value and this code is not compiled.
The code is below:
DECLARE
l_line_group_rec INL_CUSTOM_PUB.inl_ship_headers_rec;
l_name VARCHAR2(100);
l_count NUMBER;
BEGIN
l_name := INL_GET_PARTY_SITE_NAME(l_line_group_rec); -- Compiling successfully
SELECT COUNT(1)
INTO l_count
FROM dual
WHERE INL_GET_PARTY_SITE_NAME(l_line_group_rec) = 'Allied Manufacturing'; --- Error when compiling
END;
I got the error: PLS-00306: wrong number or types of arguments in call to 'INL_GET_PARTY_SITE_NAME'
I would like to know if there is any restriction in using record type as parameter in where clauses or if there is another way to do it.
Thanks in advanceHi.
The definition of INL_GET_PARTY_SITE_NAME:
FUNCTION INL_GET_PARTY_SITE_NAME(p_line_group_rec INL_CUSTOM_PUB.inl_ship_headers_rec) RETURN VARCHAR2 IS
l_party_site_name VARCHAR2(100);
BEGIN
l_party_site_name := 'Allied Manufacturing';
URN l_party_site_name;
END INL_GET_PARTY_SITE_NAME;
Thanks. -
Function in where clause is executed and returns null instead a valid value
Hi all
i have a strange problem.
There is a access-application where i have a pass-through-sql.
This sql is fetching a foreign exchange rate from a history table which holds this rates for every day and i'm going to fetch the rate for the last day of the last month.
Example of the fraction which does not work and which i executed directly on the DB, Oracle 11.2.0.3.0:
select val
,last_day(add_months((trunc(sysdate)+3),-1)) tag
from k.obj_ts_hist th
where obj_id = 2660
and eff_date = last_day(add_months((trunc(sysdate)+3),-1))
--and eff_date = '01-mar-2013'
eff_date on table k.obj_ts_hist is defined as date.
The function last_day in the where clause leads to null for both values in the select (val and tag).
If i use eff_date = '01-mar-2013' it's all ok, i get the rigth values.
I tested some combinations of the clause, e.g.
eff_date = to_date(to_char(last_day(add_months((trunc(sysdate)+3),-1)),'YYYYMMDD'),'YYYYMMDD')
or
to_char(eff_date,'YYYYMMDD') = to_char(last_day(add_months((trunc(sysdate)+3),-1)),'YYYYMMDD') , etc., but all of them do not work.
Also i tested the clause with select ... from dual, e.g.
select eff_date from
select last_day(add_months((trunc(sysdate)+3),-1)) eff_date from dual
where eff_date = last_day(add_months((trunc(sysdate)+3),-1))
This work's !
A cross check on Oracle 9.2.0.8.0: it works as expected !
Thanks in advance for your effort.
Regards, Konrada little simulation of your formula
select the_date,last_day(add_months((trunc(the_date) + 3),-1)) eff_date
from (select to_date('20130224','yyyymmdd') + level - 1 the_date
from dual
connect by level <= 7
)reveals you might have been querrying future rates
THE_DATE EFF_DATE
24.02.2013 31.01.2013
25.02.2013 31.01.2013
26.02.2013 28.02.2013 /* future date */
27.02.2013 28.02.2013 /* future date */
28.02.2013 28.02.2013 /* current date */
01.03.2013 28.02.2013
02.03.2013 28.02.2013Regards
Etbin -
Date functions in WHERE clause? HELP
The following two queries are identical except for how I supply the date values in the where clause, yet the first query using the a custom my_date function runs 30x slower than the one using the TO_DATE() function. Both return DATE types...any reason for the difference?
SELECT * from outcomes
WHERE start_time >=fn_my_date('START_MONTH')
AND start_time < fn_my_date('END_MONTH')+1
-- This runs 30x faster--
SELECT * from outcomes
WHERE start_time >=TO_DATE('08/01/2001','MM/DD/YYYY')
AND start_time < TO_DATE('08/31/2001','MM/DD/YYYY')+1
On the flip side, I've also experienced queries running slower using the TO_DATE() function vs the LAST_DAY(sysdate) for equivalenet dates.
nullI haven't seen the message coming up when using LENGTH or SUBSTR, but every time I connect to a database or attempt to change my preferences (including the "NLS Parameters: Comp" preference), this appears.
You are attempting to set the preference you should be to switch this from ANSI to something else, but SQL Developer is ignoring the preference setting (which I think is a bug).
A way to set the NLS_COMP to something else is to use something like "alter session set nls_comp = BINARY;". Note that changing any preference after that appears to overwrite this and you need to do this for each new connection you start. -
User Defined function in Where clause
DB:- 11.2
Input:- 'ACCOUNTING,SALES'
Output:- ('ACCOUNTING','SALES')
WITH T AS (select 'ACCOUNTING,SALES' str from dual)
select '('||regexp_replace(str,'([[:alpha:]]+)','''\1''')||')' from t /*this works*/I've created a function to use this in a where clause
create or replace function ss(dname varchar2)
return varchar2 is
begin
RETURN '('||regexp_replace(dname,'([[:alpha:]]+)','''\1''')||')';
end;
select ss('ACCOUNTING,SALES') from dual --this worksBut when I am using this function in a where clause result is not coming..any thing i am missing?
select * from dept where dname in ss('ACCOUNTING,SALES') --no rows940838 wrote:
Output:- ('ACCOUNTING','SALES')Wrong. ('ACCOUNTING','SALES') is a list of two strings 'ACCOUNTING' and 'SALES' while your function returns a single string '(''ACCOUNTING'',''SALES'')'.
IN clause requires a comma-separated list of values while your function, again, returns just one value. So query is comparing 'SALES' whith '(''ACCOUNTING'',''SALES'')', not with ('ACCOUNTING','SALES') and obviously no match. What you are trying to do is called dynamic SQL. There are plenty examples on how to use it. But you don't need it. Use nested table or varray. I'll use Oracle supplied varray type sys.OdciVarchar2List:
select *
from dept
where dname in (
select *
from table(sys.OdciVarchar2List('ACCOUNTING','SALES'))
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
30 SALES CHICAGO
SQL>SY. -
Strange errors when using user defined function in where clause
Hello,
I am having trouble with a function that, when used in the where clause of a select will cause an error if the first column selected is of type INTEGER. Not sure whether I am doing something wrong or whether this is a bug.
Here is a very simple test case:
create table test(
col1 integer not null,
col2 varchar(20) ascii default ''
insert into test values(1,'2011-03-15 05:00:00')
insert into test values(2,'2011-03-15 07:00:00')
CREATE FUNCTION BTR_TAG RETURNS VARCHAR AS
VAR ret VARCHAR(20);
SET ret='2011-03-15 06:00:00';
RETURN ret;
Select * from test where col2 >= BTR_TAG()
Select col1,col2 from test where col2 >= BTR_TAG()
=> Error in assignment;-3016 POS(1) Invalid numeric constant
Select '',* from test where col2 >= BTR_TAG()
Select col2,col1 from test where col2 >= BTR_TAG()
=> works as it should
MaxDB V 7.7.07.16 running on Windows Server 2003
I can replicated the test case above with Sql Studio and other ODBC based tools.
Thanks in advance,
Silke ArnswaldHello Siva,
sorry, but I don't understand your reply:
This is not right forum to posting this question.
You are from which module or working any 3rd party application.
MaxDB 7.7.07.16 is the community version of MaxDb,
we are not using it for SAP
and no 3rd party software is required to reproduce my problem,
Sql Studio or Database Studio will do.
Regards,
Silke Arnswald -
Using user defined function in where clause
Hi,
I have defined function to get maximum date before passed date on the table 'A' and I'm using the same function to get details on that date from the same table 'A' in where clause.
for ex:
SELECT x,y,z
FROM A
WHREE a.date = max_date;
But on one database instance it is running fine and on other it is going in a infinite loop.
Pls help me out
Thanks in advance,
PrashantHello Siva,
sorry, but I don't understand your reply:
This is not right forum to posting this question.
You are from which module or working any 3rd party application.
MaxDB 7.7.07.16 is the community version of MaxDb,
we are not using it for SAP
and no 3rd party software is required to reproduce my problem,
Sql Studio or Database Studio will do.
Regards,
Silke Arnswald
Maybe you are looking for
-
we have an application server(proc) that dequeues messages. If the application server stops or is killed the database session still remains active.(in v$session) Is there something that I missed in order to free up these sessions.
-
Log file when ruinning the report
Hi , When I am running the report I get an error """com.waveset.util.WavesetException: XPRESS <new> exception com.waveset.util.WavesetException: Invalid or missing constructor arguments. java.lang.IllegalArgumentException""" I know why I am getting t
-
Accessing a specific schema with JDBC
My database has several schemas and I'm trying to access a specific schema using the following code snipet: Class driverClass = Class("oracle.jdbc.driver.OracleDriver"); Connection jdbcConnection = Drivermanager.getConnection("jdbc:oracle:thin:@datas
-
Linkage of Smartform payslip to Portal
Hi Sap Gurus, Request information on linking the payslip developed thru smartforms into ESS portal in ECC 6.0 environment. Please help Thanks in advance Rajesh
-
Selecting and Updating in SQL 2005
I have a situation where I need to select some records from a table. This select query will run again after a certain amount of time. I dont want the second execution of the query to select the records that were selected in the first run. So, what I