exect fetch result error on oracle report - oracle

hi dear all i have a formula which check gazetted dates from a table then provide description its fine if its in between dates only 1 gazetted date comes otherwise it throw error exact fetch I know there is more rows then fetch how i can over come on this please help me
my code is
function CF_WEEKRESTFormula return CHAR is
descr varchar2(90);
gz_dt date;
begin
select gazzeted_date, description into gz_dt, descr
from gazzeted_days
where gazzeted_date between :DATE1 and :DATE2;
:ATT_DATE = GZ_DT THEN
RETURN DESCR;
it have other code mean else and exception conditions all settled only this part is doing wrong I know what mean of this it bring more results than 1 but how to overcome on this is just a problem so tired doing this if any one come forward for help lot of appreciated
full code is
function CF_WEEKRESTFormula return CHAR is
v_rest number;
gaz_dt number;
descr varchar2(90);
gz_dt date;
AT_P VARCHAR2(10);
BEGIN
SELECT week_rest
INTO v_rest
FROM EMPLOYEES
WHERE employee_id1=:employee_id1
AND :ATT_DATE between :DATE1 and :DATE2;
select gazzeted_date, description into gz_dt, descr
from gazzeted_days
where
gazzeted_date between :DATE1 and :DATE2;
IF TO_CHAR( :ATT_DATE, 'D' ) = v_rest THEN
RETURN 'WEEK REST';
ELSIF
:ATT_DATE = GZ_DT THEN
RETURN DESCR;
ELSE RETURN 'PRESENT';
END IF;
exception
when No_data_found
then return 'NO DATA FOUND';
END;

Related

Oracle APEX 20.1 Charts Error with “PL/SQL Function Body returning SQL Query”

I have a problem with new charts in Oracle APEX 20.1.
I need to do chart from PL/SQL Function Body returning SQL Query,
but it only works if I do it with some exact parameters. All variables return from the same submitted page.
If I use all variables as parameter then i get error: ORA-20999: PL/SQL function body did not return a value.
If I use variable :P2_OBJECT and parameter for :P2_YEAR and :P2_ANNUAL_TIME then i get error: ORA-20999: PL/SQL function body did not return a value.
If I use variable :P2_YEAR and parameter for :P2_OBJECT and :P2_ANNUAL_TIME then i get error: ORA-20999: Parsing returned query results in "ORA-20999: Failed to parse SQL query! ORA-06550: line 2, column 402: ORA-00936: missing expression".
If I use variable :P2_ANNUAL_TIME and parameter for :P2_OBJECT and :P2_YEAR then i get error: ORA-20999: Parsing returned query results in "ORA-20999: Failed to parse SQL query! ORA-06550: line 2, column 113: ORA-01741: illegal zero-length identifier".
but in a classic report the same function with variables works just fine...
this source for chart works:
declare l_sql varchar2(2000) ;
begin
select F_CHARTS(4020 , 2018 ,'TIM_MM',
:P2_LEVELS, :P2_SUB_LEVELS, :P2_SQL_CONDITION, :P2_WAREHOUSE, :P2_UNIT_OF_MEASURE)
into l_sql from dual;
return l_sql ;
end ;
but this does not:
declare
l_sql varchar2(2000) ;
begin
select F_CHARTS(:P2_OBJECT,:P2_YEAR,:P2_ANNUAL_TIME,
:P2_LEVELS, :P2_SUB_LEVELS, :P2_SQL_CONDITION, :P2_WAREHOUSE, :P2_UNIT_OF_MEASURE)
into l_sql from dual;
return l_sql ;
end ;
This is the function:
create or replace FUNCTION F_CHARTS(
cod in NUMBER,
year in NUMBER,
t_time in varchar2,
v_dims in varchar2,
p_subl in varchar2,
cod_filter in NUMBER,
w_warehouse in varchar2,
amount in NUMBER)
return varchar2 is s varchar2(4000);
g2 boolean := false;
l NUMBER := 0;
m VARCHAR2(1000) :='';
c VARCHAR2(40) :='';
w VARCHAR2(40) :='';
sql_cond VARCHAR2(4000) :='';
ope VARCHAR2(5) :='';
OGG_FACT_TAB VARCHAR2(40);
OGG_COL_GROUP VARCHAR2(40);
OGG_COL_SUBGR VARCHAR2(40);
OGG_COL_SUM VARCHAR2(200);
OGG_COL_TIME VARCHAR2(40);
OGG_ALIAS_SUM VARCHAR2(100);
OGG_DIMS_TAB VARCHAR2(40);
OGG_COL_KEY VARCHAR2(40);
OGG_COL_DES VARCHAR2(40);
OGG_ALIAS_TAB VARCHAR2(100);
OGG_AVERAGES VARCHAR2(1);
OGG_CLASS NUMBER;
OGG_COL_SUM2 VARCHAR2(200);
cursor times is SELECT QTM_DESCRIPTION,QTM_DES_VALUE,QTM_VALUE FROM Q_TIME WHERE QTM_FIELD_NAME=t_time ORDER BY QTM_SEQUENCE;
BEGIN
select OGG_CLASS,OGG_FACT_TAB,OGG_COL_GROUP,OGG_COL_SUM,OGG_COL_TIME,OGG_ALIAS_SUM,OGG_DIMS_TAB,OGG_COL_KEY,OGG_COL_DES,OGG_ALIAS_TAB,OGG_COL_SUBGR,TRIM(OGG_AVERAGE),TRIM(OGG_COL_SUM2) into OGG_CLASS,OGG_FACT_TAB,OGG_COL_GROUP,OGG_COL_SUM,OGG_COL_TIME,OGG_ALIAS_SUM,OGG_DIMS_TAB,OGG_COL_KEY,OGG_COL_DES,OGG_ALIAS_TAB,OGG_COL_SUBGR,OGG_AVERAGES,OGG_COL_SUM2 from Q_OBJECT where OGG_CODE = cod;
IF ( p_subl is not null and p_subl <> 'null' and TRIM(p_subl) is not null and p_subl <> to_char(cod)
and OGG_COL_SUBGR IS not NULL and TRIM(OGG_COL_SUBGR) is not null ) THEN
g2:=true;
IF ( OGG_CLASS = 1 AND amount = 1 AND OGG_COL_SUM2 IS NOT NULL ) THEN
OGG_COL_SUBGR:='''kg''';
OGG_COL_SUM:=OGG_COL_SUM2;
ELSE
OGG_COL_SUBGR:='f.'||OGG_COL_SUBGR;
END IF;
END IF;
IF (OGG_AVERAGES is not null and OGG_AVERAGES='S') THEN
ope:='avg';
ELSE
ope:='sum';
END IF;
m:=t_time;
FOR reco IN times LOOP
c:=reco.QTM_DESCRIPTION;
m:=m||','||to_char(reco.QTM_VALUE)||','''||reco.QTM_DES_VALUE||'''';
END LOOP;
m:=m||',''...'')';
s:='select null link, mm "'||c||'", ii "'||OGG_ALIAS_SUM||' '||to_char(year)||'" from (';
s:=s||'select '||t_time||' tt, decode(t.'||m||' mm';
s:=s||', trunc('||ope||' ('||OGG_COL_SUM||'))'||' ii';
s:=s||' from D_TIME t, '||OGG_FACT_TAB||' f';
s:=s||' where t.TIM_AAAA = '||year;
s:=s||' and f.'||OGG_COL_TIME||' = t.TIM_KEY';
IF (g2) THEN
s:=s||' and '||OGG_COL_SUBGR||' = '''||p_subl||'''';
END IF;
IF (OGG_FACT_TAB='R_MAG_SALES' and w_warehouse is not null and w_warehouse <> 'null') THEN
s:=s||' and f.MS_WAREHOUSE = '''||w_warehouse||'''';
END IF;
IF (v_dims is not null and v_dims <> 'null') THEN
s:=s||' and f.'||OGG_COL_GROUP||' = '''||v_dims||'''';
END IF;
BEGIN
SELECT NVL(TRIM(SQL_CONDITION),'.') INTO sql_cond FROM Q_SQL_CONDITION WHERE SQL_CODE=cod_filter;
EXCEPTION
WHEN NO_DATA_FOUND THEN
sql_cond:='.';
END;
IF (sql_cond<>'.') THEN
s:=s||' and ('||sql_cond||')';
END IF;
s:=s||' group by t.'||t_time;
s:=s||' union ';
s:=s||'select t2.'||t_time||', decode(t2.'||m||', 0 from D_TIME t2';
s:=s||' where t2.'||t_time||' <> 0 and t2.'||t_time||' not in';
s:=s||' (select unique t3.'||t_time||' from D_TIME t3,'||OGG_FACT_TAB||' f3';
s:=s||' where t3.TIM_AAAA = '||year;
s:=s||' and t3.TIM_KEY = f3.'||OGG_COL_TIME;
BEGIN
SELECT NVL(TRIM(SQL_CONDITION),'.') INTO sql_cond FROM Q_SQL_CONDITION WHERE SQL_CODE=cod_filter;
EXCEPTION
WHEN NO_DATA_FOUND THEN
sql_cond:='.';
END;
IF (sql_cond<>'.') THEN
s:=s||' and ('||sql_cond||')';
END IF;
IF (OGG_FACT_TAB='R_MAG_SALES' and w_warehouse is not null and w_warehouse <> 'null') THEN
s:=s||' and f3.MS_WAREHOUSE = '''||w_warehouse||'''';
END IF;
IF (v_dims is not null and v_dims <> 'null') THEN
s:=s||' and f3.'||OGG_COL_GROUP||' = '''||v_dims||''')';
ELSE
s:=s||')';
END IF;
s:=s||' group by t2.'||t_time;
s:=s||')';
s:=s||' order by tt';
RETURN (s);
END;
To better understand this is the result of the function:
select null link, mm "Month", ii " 2018" from
(select TIM_MM tt, decode(t.TIM_MM,1,'January',2,'February',3,'March',4,'April',5,'May',6,'June',7,'July',8,'August',9,'September',10,'October',11,'November',12,'December','...') mm, trunc(sum (MS_FINAL_EXISTENCE)) ii from
D_TIME t, R_MAG_SALES f
where t.TIM_AAAA = 2018 and f.MS_TIM_BALANCE_DATE = t.TIM_KEY and f.MS_UNIT_OF_MEASURE = 'PZ' and f.MS_COD_CATEGORY = '000001' group by t.TIM_MM
union select t2.TIM_MM, decode(t2.TIM_MM,1,'January',2,'February',3,'March',4,'April',5,'May',6,'June',7,'July',8,'August',9,'September',10,'October',11,'November',12,'December','...'), 0 from
D_TIME t2
where t2.TIM_MM <> 0
and t2.TIM_MM not in (select unique t3.TIM_MM from D_TIME t3,R_MAG_SALES f3 where t3.TIM_AAAA = 2018 and t3.TIM_KEY = f3.MS_TIM_BALANCE_DATE and f3.MS_COD_CATEGORY = '000001') group by t2.TIM_MM) order by tt
Can enybody please help me, I just do not see what is wrong...
When you write the query in IR/CR, the bind variables :P2_OBJECT,:P2_YEAR are null.
So, your builder assumes the query as
select F_CHARTS(NULL,NULL,....)
into l_sql from dual;
So the compilation of PLSQL block fails
You can write the query as
select f_charts ( COALESCE(:P2_OBJECT, 'X'), COALESCE(:P2_YEAR, 0)....) from dual.
SO, you are actually sending some actual values to f_charts
You can also handle this in function to assume some value if inputs are null.

i have run time error any body can check the function and what is missing

I created a function to check a table.
If the student exists, return Y.
I have a runtime error.
create or replace FUNCTION ssc_f_get_speical_need (P_STUDENT_NO IN NUMBER )
RETURN char
IS
l_exist number ;
begin
select s.STUDENT_NO
into l_exist
from SSC_WITH_SPECIAL_NEED s
where s.STUDENT_NO = P_STUDENT_NO ;
if (l_exist = P_STUDENT_NO) then
return 'Y' ;
else
return 'N' ;
end if;
end;
As STUDENT_ID is most probably a primary key column, it won't allow duplicates so the SELECT can't return TOO_MANY_ROWS. It might, on the other hand, return NO_DATA_FOUND if parameter P_STUDENT_ID contains value that doesn't exist in the table.
A simple way to fix it is to use an aggregate function (such as MAX, in my example) which will return
a value if it exists
NULL if it doesn't
but won't return NO_DATA_FOUND so you don't have to code the exception handler
For testing purposes, I created a dummy table based on Scott's EMP table.
create table ssc_with_special_need as
select empno student_no from emp
where rownum < 5;
Now, a function:
create or replace function ssc_f_get_special_need
(p_student_no in number)
return char
is
l_exist ssc_with_special_need.student_no%type;
begin
select max(s.student_no)
into l_exist
from ssc_with_special_need s
where s.student_no = p_student_no;
return case when l_exist is not null then 'Y'
else 'N'
end;
end;
/
Testing:
SQL> select * from ssc_with_special_need;
STUDENT_NO
----------
7369
7499
7521
7566
SQL> select ssc_f_get_special_need(1) result_1, --> doesn't exist in the table (return N)
2 ssc_f_get_special_need(7369) result_2 --> exists in the table (return Y)
3 from dual;
RESULT_1 RESULT_2
---------- ----------
N Y
SQL>
A better (proper) way is to handle possible exceptions; as I said, I won't handle TOO_MANY_ROWS as it shouldn't ever be raised if the STUDENT_ID is a primary key column.
create or replace function ssc_f_get_special_need
(p_student_no in number)
return char
is
l_exist ssc_with_special_need.student_no%type;
begin
select s.student_no
into l_exist
from ssc_with_special_need s
where s.student_no = p_student_no;
-- if the above SELECT returned a value, return 'Y' immediately
return 'Y';
exception
when no_data_found then
-- SELECT didn't find a value and raised an exception - return 'N'
return 'N';
end;
/
Testing: the result is just the same:
SQL> select ssc_f_get_special_need(1) result_1,
2 ssc_f_get_special_need(7369) result_2
3 from dual;
RESULT_1 RESULT_2
---------- ----------
N Y
SQL>
I wouldn't use WHEN OTHERS as it is, generally speaking, a bad habit: handle what you expect, let Oracle raise everything else (and handle it later, if necessary).
If you want to avoid NO_DATA_FOUND or TOO_MANY_ROWS exceptions then you can re-write your query to something similar to:
select decode(count(s.STUDENT_NO),0,'N','Y')
into l_result
from SSC_WITH_SPECIAL_NEED s
where s.STUDENT_NO = P_STUDENT_NO ;
So, your function will looks like:
create or replace function ssc_f_get_speical_need(p_student_no in number )
return char
is
l_result varchar2(1) ;
begin
select decode(count(s.student_no),0,'N','Y')
into l_result
from ssc_with_special_need s
where s.student_no = p_student_no ;
return l_result;
end;
Probally you get an error like "no data found" in case there is no Student with the given Student number.
Try to check your tabel if there is atleast one matching row.
CREATE OR REPLACE FUNCTION ssc_f_get_speical_need (p_student_no IN NUMBER)
RETURN CHAR
IS
l_exist NUMBER;
l_count NUMBER;
BEGIN
SELECT COUNT ( * )
INTO l_count
FROM ssc_with_special_need s
WHERE s.student_no = p_student_no;
IF l_count > 0
THEN
SELECT s.student_no
INTO l_exist
FROM ssc_with_special_need s
WHERE s.student_no = p_student_no;
IF l_exist = p_student_no
THEN
RETURN 'Y';
END IF;
END IF;
RETURN 'N';
END;
Add an exception in your function, just to test, if the error persists so the problem is in another where.
CREATE OR REPLACE FUNCTION....
BEGIN....
EXCEPTION WHEN OTHERS THEN --catch any error
RETURN 'E';
END
https://www.techonthenet.com/oracle/exceptions/when_others.php
Testing for existence, regardless of using primary key or not, I've always followed Tom Kyte's pattern for selecting from dual where exists.
create or replace function ssc_f_get_special_need
(p_student_no in number)
return char
is
l_exist pls_integer;
begin
select count(*)
into l_exist
from dual
where exists
(select s.student_no
from ssc_with_special_need s
where s.student_no = p_student_no);
return case when l_exist = 1 then 'Y' else 'N' end;
end;
/

oracle - convert many date formats to a single formatted date

I want to bring a string which contains a date to a single format date.
EX:
13-06-2012 to 13-JUN-12
13/06/2012 to 13-JUN-12
13-JUN-2012 to
13-JUN-12
13/jun-2012 to 13-JUN-12
...
I tried to delete all special characters and after that use a function to transform that string into a single format of date. My function return more exceptions, I don't know why...
The function:
CREATE OR REPLACE FUNCTION normalize_date (data_in IN VARCHAR2)
RETURN DATE
IS
tmp_month VARCHAR2 (3);
tmp_day VARCHAR2 (2);
tmp_year VARCHAR2 (4);
TMP_YEAR_NUMBER NUMBER;
result DATE;
BEGIN
tmp_day := SUBSTR (data_in, 1, 2);
tmp_year := SUBSTR (data_in, -4);
--if(REGEXP_LIKE(SUBSTR(data_in,3,2), '[:alpha:]')) then
if(SUBSTR(data_in,3,1) in ('a','j','i','f','m','s','o','n','d','A','J','I','F','M','S','O','N','D')) then
tmp_month := UPPER(SUBSTR (data_in, 3, 3));
else
tmp_month := SUBSTR (data_in, 3, 2);
end if;
DBMS_OUTPUT.put_line (tmp_year);
TMP_YEAR_NUMBER := TO_NUMBER (tmp_year);
IF (tmp_month = 'JAN')
THEN
tmp_month := '01';
END IF;
IF (tmp_month = 'FEB')
THEN
tmp_month := '02';
END IF;
IF (tmp_month = 'MAR')
THEN
tmp_month := '03';
END IF;
IF (tmp_month = 'APR')
THEN
tmp_month := '04';
END IF;
IF (tmp_month = 'MAY')
THEN
tmp_month := '05';
END IF;
IF (tmp_month = 'JUN')
THEN
tmp_month := '06';
END IF;
IF (tmp_month = 'JUL')
THEN
tmp_month := '07';
END IF;
IF (tmp_month = 'AUG')
THEN
tmp_month := '08';
END IF;
IF (tmp_month = 'SEP')
THEN
tmp_month := '09';
END IF;
IF (tmp_month = 'OCT')
THEN
tmp_month := '10';
END IF;
IF (tmp_month = 'NOV')
THEN
tmp_month := '11';
END IF;
IF (tmp_month = 'DEC')
THEN
tmp_month := '12';
END IF;
-- dbms_output.put_line(tmp_day || '~'||tmp_year || '~' ||tmp_month);
IF (LENGTH (tmp_day || tmp_year || tmp_month) <> 8)
THEN
result := TO_DATE ('31122999', 'DDMMYYYY');
RETURN result;
END IF;
-- dbms_output.put_line('before end');
result:=TO_DATE (tmp_day || tmp_month ||tmp_year , 'DDMMYYYY');
-- dbms_output.put_line('date result: '|| result);
RETURN result;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
NULL;
WHEN OTHERS
THEN
result := TO_DATE ('3012299', 'DDMMYYYY');
RETURN result;
RAISE;
END normalize_date;
Usage
SELECT customer_no,
str_data_expirare,
normalize_date (str_data_expirare_trim) AS data_expirare_buletin
FROM (SELECT customer_no,
str_data_expirare,
REGEXP_REPLACE (str_data_expirare, '[^a-zA-Z0-9]+', '')
AS str_data_expirare_trim
FROM (SELECT Q1.set_act_id_1,
Q1.customer_no,
NVL (SUBSTR (set_act_id_1,
INSTR (set_act_id_1,
'+',
1,
5)
+ 1,
LENGTH (set_act_id_1)),
'NULL')
AS str_data_expirare
FROM STAGE_CORE.IFLEX_CUSTOMERS Q1
WHERE Q1.set_act_id_1 IS NOT NULL
)
);
If you have a sound idea of all the possible date formats it might be easier to use brute force:
create or replace function clean_date
( p_date_str in varchar2)
return date
is
l_dt_fmt_nt sys.dbms_debug_vc2coll := sys.dbms_debug_vc2coll
('DD-MON-YYYY', 'DD-MON-YY', 'DD-MM-YYYY', 'MM-DD-YYYY', 'YYYY-MM-DD'
, 'DD/MM/YYYY', 'MM/DD/YYYY', 'YYYY/MM/DD', 'DD/MM/YY', 'MM/DD/YY');
return_value date;
begin
for idx in l_dt_fmt_nt.first()..l_dt_fmt_nt.last()
loop
begin
return_value := to_date(p_date_str, l_dt_fmt_nt(idx));
exit;
exception
when others then null;
end;
end loop;
if return_value is null then
raise no_data_found;
end if;
return return_value;
exception
when no_data_found then
raise_application_error(-20000, p_date_str|| ' is unknown date format');
end clean_date;
/
Be aware that modern versions of Oracle are quite forgiving with date conversion. This function handled dates in formats which aren't in the list, with some interesting consequences:
SQL> select clean_date('20160817') from dual;
CLEAN_DAT
---------
17-AUG-16
SQL> select clean_date('160817') from dual;
CLEAN_DAT
---------
16-AUG-17
SQL>
Which demonstrates the limits of automated data cleansing in the face of lax data integrity rules. The wages of sin is corrupted data.
#AlexPoole raises the matter of using the 'RR' format. This element of the date mask was introduced as a Y2K kludge. It's rather depressing that we're still discussing it almost two decades into the new Millennium.
Anyway, the issue is this. If we cast this string '161225' to a date what century does it have? Well, 'yymmdd' will give 2016-12-15. Fair enough, but what about '991225'? How likely is that the date we really want is 2099-12-15? This is where the 'RR' format comes into play. Basically it defaults the century: numbers 00-49 default to 20, 50-99 default to 19. This window was determined by the Y2K issue: in 2000 it was more likely that '98 referred to the recent past than the near future, and similar logic applied to '02. Hence the halfway point of 1950. Note this is a fixed point not a sliding window. As we move further from the year 2000 the less useful that pivot point becomes. Find out more.
Anyway, the key point is that 'RRRR' does not play nicely with other date formats: to_date('501212', 'rrrrmmdd') hurlsora-01843: not a valid month. So, use'RR'and test for it before using'YYYY'`. So my revised function (with some tidying up) looks like this:
create or replace function clean_date
( p_date_str in varchar2)
return date
is
l_dt_fmt_nt sys.dbms_debug_vc2coll := sys.dbms_debug_vc2coll
('DD-MM-RR', 'MM-DD-RR', 'RR-MM-DD', 'RR-DD-MM'
, 'DD-MM-YYYY', 'MM-DD-YYYY', 'YYYY-MM-DD', 'YYYY-DD-MM');
return_value date;
begin
for idx in l_dt_fmt_nt.first()..l_dt_fmt_nt.last()
loop
begin
return_value := to_date(p_date_str, l_dt_fmt_nt(idx));
exit;
exception
when others then null;
end;
end loop;
if return_value is null then
raise no_data_found;
end if;
return return_value;
exception
when no_data_found then
raise_application_error(-20000, p_date_str|| ' is unknown date format');
end clean_date;
/
The key point remains: there's a limit to how smart we can make this function when it comes to interpreting dates, so make sure you lead with the best fit. If you think most of your date strings fit day-month-year put that first; you will still get some wrong casts but less that if you lead with year-month-day.
The String-to-Date Conversion Rules allow additional formatting rules (without any other modifiers being applied). (Also see this question) So:
MM also matches MON and MONTH;
MON also matches MONTH (and vice versa);
YY also matches YYYY;
RR also matches RRRR; and
The punctuation is optional.
Which means you can do:
CREATE OR REPLACE FUNCTION parse_Date_String(
in_string VARCHAR2
) RETURN DATE DETERMINISTIC
IS
BEGIN
BEGIN
RETURN TO_DATE( in_string, 'DD-MM-YY' );
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
BEGIN
RETURN TO_DATE( in_string, 'MM-DD-YY' );
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
BEGIN
RETURN TO_DATE( in_string, 'YY-MM-DD' );
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
RETURN NULL;
END;
/
Query:
WITH dates ( value ) AS (
SELECT '010101' FROM DUAL UNION ALL
SELECT '02JAN01' FROM DUAL UNION ALL
SELECT '03JANUARY01' FROM DUAL UNION ALL
SELECT '04012001' FROM DUAL UNION ALL
SELECT '05JAN2001' FROM DUAL UNION ALL
SELECT '06JANUARY2001' FROM DUAL UNION ALL
SELECT 'JAN0701' FROM DUAL UNION ALL
SELECT 'JANUARY0801' FROM DUAL UNION ALL
SELECT 'JAN0901' FROM DUAL UNION ALL
SELECT 'JANUARY1001' FROM DUAL UNION ALL
SELECT '990111' FROM DUAL UNION ALL
SELECT '99JAN12' FROM DUAL UNION ALL
SELECT '99JANUARY13' FROM DUAL UNION ALL
SELECT '19990114' FROM DUAL UNION ALL
SELECT '2001-01-15' FROM DUAL UNION ALL
SELECT '2001JAN16' FROM DUAL UNION ALL
SELECT '2001JANUARY17' FROM DUAL UNION ALL
SELECT '20010118' FROM DUAL
)
SELECT value, parse_Date_String( value ) AS dt
FROM dates;
Output:
VALUE DT
------------- -------------------
010101 2001-01-01 00:00:00
02JAN01 2001-01-02 00:00:00
03JANUARY01 2001-01-03 00:00:00
04012001 2001-01-04 00:00:00
05JAN2001 2001-01-05 00:00:00
06JANUARY2001 2001-01-06 00:00:00
JAN0701 2001-01-07 00:00:00
JANUARY0801 2001-01-08 00:00:00
JAN092001 2001-01-09 00:00:00
JANUARY102001 2001-01-10 00:00:00
990111 2099-01-11 00:00:00
99JAN12 2099-01-12 00:00:00
99JANUARY13 2099-01-13 00:00:00
19990114 1999-01-14 00:00:00
2001-01-15 2001-01-15 00:00:00
2001JAN16 2001-01-16 00:00:00
2001JANUARY17 2001-01-17 00:00:00
20010118 0118-01-20 00:00:00
(Note: the date formats you are using are ambiguous, as the last example demonstrates. You can swap the order the formats are parsed in the function to get different results but if you have 010203 is it 01-FEB-2003, 02-JAN-2003, 03-FEB-2001 or even 01-FEB-0003?)
If you want it in the format DD-MON-YY (but why YY and not YYYY?) then just use:
TO_CHAR( parse_Date_String( value ), 'DD-MON-YY' )

Exact fetch returns more than requested number of rows

I'm trying to create a procedure to pass in the customer number and return the number of purchases and total value for purchases within the last year. If there are no purchases, return zero for number and total and also return the number of contacts the salesmen had with that customer in the last year.
I'm calling it as follows:
DECLARE
a_Var NUMBER;
b_Var NUMBER;
C_Var NUMBER;
D_Var NUMBER;
BEGIN
three_pr(001116,a_Var, b_Var);
IF a_Var > 0 THEN
DBMS_OUTPUT.PUT_LINE('the number of purchases :' || a_Var);
DBMS_OUTPUT.PUT_LINE('the total value of purchase :' || b_Var);
ELSE
SELECT ContactID,Count(contactID)
INTO
C_Var,D_Var
FROM DD_Contacts
WHERE DateofContact between to_date ('2012/01/01', 'yyyy/mm/dd')
AND to_date ('2012/12/31', 'yyyy/mm/dd')
Group By ContactID;
DBMS_OUTPUT.PUT_LINE (C_Var||D_Var);
END IF;
END;
/
When using the above code I get the error:
ORA-01422: exact fetch returns more than requested number of rows ORA-06512: at line 16
and here's the procedure:
CREATE or REPLACE PROCEDURE three_pr
(par_CustomerID IN NUMBER, par_sumpurchase OUT Number,par_totalvalue OUT Number)
IS
BEGIN
SELECT
COUNT(O.OrderID),SUM(Price*Quantity)
INTO par_sumpurchase,par_totalvalue
FROM DD_Orders O JOIN DD_OrderLine OL ON O.OrderID = OL.OrderID
WHERE DatePurchase between to_date ('2012/01/01', 'yyyy/mm/dd')
AND to_date ('2012/12/31', 'yyyy/mm/dd')
AND CustomerID = par_CustomerID;
END;
/
The procedure three_pr does not correctly handle the NO_DATA_FOUND exception when the query does not return results.
CREATE or REPLACE PROCEDURE three_pr
(par_CustomerID IN NUMBER, par_sumpurchase OUT Number,par_totalvalue OUT Number)
IS
BEGIN
BEGIN
SELECT
COUNT(O.OrderID),SUM(Price*Quantity)
INTO par_sumpurchase,par_totalvalue
FROM DD_Orders O JOIN DD_OrderLine OL ON O.OrderID = OL.OrderID
WHERE DatePurchase between to_date ('2012/01/01', 'yyyy/mm/dd')
AND to_date ('2012/12/31', 'yyyy/mm/dd')
AND CustomerID = par_CustomerID;
EXCEPTION
WHEN NO_DATA_FOUND THEN
par_sumpurchase := 0;
par_totalvalue := 0;
END;
END;
/

Loop to check all 14 days in the pay period

Name: Calc_Anniversary
Input: Pay_Date, Hire_Date, Termination_Date
Output: "Y" if is the anniversary of the employee's Hire_Date, "N" if it is not, and "T" if he has been terminated before his anniversary.
Description: Create local variables to hold the month and day of the employee's Date_of_Hire, Termination_Date, and of the processing date using the TO_CHAR function. First check to see if he was terminated before his anniversary. The anniversary could be on any day during the pay period, so there will be a loop to check all 14 days in the pay period to see if one was his anniversary.
CREATE OR replace FUNCTION Calc_anniversary(
incoming_anniversary_date IN VARCHAR2)
RETURN BOOLEAN
IS
hiredate VARCHAR2(20);
terminationdate VARCHAR(20);
employeeid VARCHAR2(38);
paydate NUMBER := 0;
BEGIN
SELECT Count(arndt_raw_time_sheet_data.pay_date)
INTO paydate
FROM arndt_raw_time_sheet_data
WHERE paydate = incoming_anniversary_date;
WHILE paydate <= 14 LOOP
SELECT To_char(employee_id, '999'),
To_char(hire_date, 'DD-MON'),
To_char(termination_date, 'DD-MON')
INTO employeeid, hiredate, terminationdate
FROM employees,
time_sheet
WHERE employees.employee_id = time_sheet.employee_id
AND paydate = pay_date;
IF terminationdate > hiredate THEN
RETURN 'T';
ELSE
IF To_char(SYSDATE, 'DD-MON') = To_char(hiredate, 'DD-MON')THEN
RETURN 'Y';
ELSE
RETURN 'N';
END IF;
END IF;
paydate := paydate + 1;
END LOOP;
END;
Tables I am using
CREATE TABLE Employees ( EMPLOYEE_ID INTEGER,
FIRST_NAME VARCHAR2(15),
LAST_NAME VARCHAR2(25),
ADDRESS_LINE_ONE VARCHAR2(35),
ADDRESS_LINE_TWO VARCHAR2(35),
CITY VARCHAR2(28),
STATE CHAR(2),
ZIP_CODE CHAR(10),
COUNTY VARCHAR2(10),
EMAIL VARCHAR2(16),
PHONE_NUMBER VARCHAR2(12),
SOCIAL_SECURITY_NUMBER VARCHAR2(11),
HIRE_DATE DATE,
TERMINATION_DATE DATE,
DATE_OF_BIRTH DATE,
SPOUSE_ID INTEGER,
MARITAL_STATUS CHAR(1),
ALLOWANCES INTEGER,
PERSONAL_TIME_OFF FLOAT,
CONSTRAINT pk_employee_id PRIMARY KEY (EMPLOYEE_ID),
CONSTRAINT fk_spouse_id FOREIGN KEY (SPOUSE_ID) REFERENCES EMPLOYEES (EMPLOYEE_ID))
/
CREATE TABLE Arndt_Raw_Time_Sheet_data ( EMPLOYEE_ID INTEGER,
PAY_DATE DATE,
HOURS_WORKED FLOAT,
SALES_AMOUNT FLOAT,
CONSTRAINT pk_employee_id_pay_date_time PRIMARY KEY (EMPLOYEE_ID, PAY_DATE),
CONSTRAINT fk_employee_id_time FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEES (EMployee_ID));
error FUNCTION Calc_Anniversary compiled
Warning: execution completed with warning
Functions have to return something. It is normal for the RETURN statement to be the last statement in a function.
You have chosen not to do this, and that's why you're getting an error. All your RETURN statements are embedded in conditional branches, so if your logic never executes the loop you will never execute a RETURN.
Your loop logic is confused. You are populating your paydate as a count (so why is it called ""paydate"?) but your initialisation query compares 'paydate' to your parameter incoming_anniversary_date which is a date. Perhaps you meant to compare it to the tabel column pay_date? So who knows what your code is actually doing?
Anyway, the most important thing is to introduce some best practice into your function: you need to populate a variable and restrict yourself to just the one RETURN statement.
return_value char(1);
BEGIN
return_value := 'X';
....
WHILE paydate <= 14 LOOP
....
IF terminationdate > hiredate THEN
return_value := 'T';
ELSE
IF To_char(SYSDATE, 'DD-MON') = To_char(hiredate, 'DD-MON')THEN
return_value := 'Y';
ELSE
return_value := 'N';
END IF;
END IF;
...
END LOOP;
RETURN return_value;
END;
Also, this is wrong:
IF terminationdate > hiredate THEN
You converted those dates to strings, which means that '23-JAN' > '22-DEC'. This is probably not the result you intend.
Oh, and rename your variable paydate to something a bit less confusing, like l_count.
create or replace
FUNCTION Calc_Anniversary(employee IN VARCHAR2)
RETURN VARCHAR2
IS
counter INTEGER;
term_date VARCHAR2(15);
h_date VARCHAR2(15);
p_date DATE;
BEGIN
SELECT TO_CHAR(hire_date, 'DD-MON'), TO_CHAR(termination_date, 'DD-MON'), pay_date INTO h_date, term_date, p_date
FROM employees e, diaz_raw_time_sheet_data d
WHERE e.employee_id = d.employee_id
AND e.employee_id = employee;
FOR counter IN 0 .. 14 LOOP
IF term_date > h_date THEN
RETURN 'T';
ELSIF TO_CHAR(p_date,'DD-MON') = h_date THEN
RETURN 'Y';
END IF;
p_date := p_date - 1;
END LOOP;
IF h_date <> TO_CHAR(p_date, 'DD-MON') THEN
RETURN 'N';
END IF;
END;

Resources