Home » SQL & PL/SQL » SQL & PL/SQL » SQL (Oracle 11g)
SQL [message #620591] |
Mon, 04 August 2014 09:02 |
ORAGENASHOK
Messages: 240 Registered: June 2006 Location: Chennai
|
Senior Member |
|
|
create table emp
( emp_id number,
emp_name varchar2(20),
salary number
)
create table dept
(
dept_id number,
emp_id number,
dept_name varchar2(50),
assigned_dtm_utc date,
estimated_dtm_utc date,
assigned_dtm_local date,
estimated_dtm_local date
)
insert into emp values ( 1,'Ashok','50000')
insert into emp values ( 2,'Ram','60000')
insert into emp values ( 3,'krish','70000')
insert into emp values ( 4,'raj','80000')
insert into emp values ( 5,'mohan','90000')
insert into dept values ( 1,1,'IT',to_date(sysdate,'dd-mon-yyyy hh24:mi:ss'),to_date(sysdate,'dd-mon-yyyy hh24:mi:ss'),to_date(sysdate,'dd-mon-yyyy hh24:mi:ss'),
to_date(sysdate,'dd-mon-yyyy hh24:mi:ss'));
insert into dept values ( 2,2,'IT',to_date(sysdate,'dd-mon-yyyy hh24:mi:ss'),to_date(sysdate,'dd-mon-yyyy hh24:mi:ss'),to_date(sysdate,'dd-mon-yyyy hh24:mi:ss'),
to_date(sysdate,'dd-mon-yyyy hh24:mi:ss'));
insert into dept values ( 3,3,'IT',to_date(sysdate,'dd-mon-yyyy hh24:mi:ss'),to_date(sysdate,'dd-mon-yyyy hh24:mi:ss'),to_date(sysdate,'dd-mon-yyyy hh24:mi:ss'),
to_date(sysdate,'dd-mon-yyyy hh24:mi:ss'));
create or replace
procedure proc1
(
i_flg varchar2,
i_flg1 varchar2,
i_date date
)
AS
v_assigned_dtm_utc varchar2(100) := 'assigned_dtm_utc' ;
v_estimated_dtm_utc varchar2(100) := 'estimated_dtm_utc' ;
v_assigned_dtm_local varchar2(100) := 'assigned_dtm_local' ;
v_estimated_dtm_local varchar2(100) := 'estimated_dtm_local' ;
v_assigned_dtm date;
v_estimated_dtm date;
v_flg varchar2(1);
v_flg1 varchar2(1);
v_date date;
v_empid number ;
v_exact_match varchar2(1) ;
--i_date date := sysdate ;
Begin
if ( i_flg = 'D') and ( i_flg1 = 'L' ) then
v_assigned_dtm := v_assigned_dtm_local ;
v_estimated_dtm := v_estimated_dtm_local ;
v_date := i_date ;
elsif ( i_flg = 'D') and ( i_flg1 = 'U' ) then
v_assigned_dtm := v_assigned_dtm_utc ;
v_estimated_dtm := v_estimated_dtm_utc ;
v_date := i_date ;
End if ;
select b.emp_id,
decode(
decode( trunc(v_assigned_dtm), TRUNC(i_date),'Y','N'),'Y','Y',
decode( trunc(v_estimated_dtm), TRUNC(i_date),'Y','N'),'N'
) exact_match
into v_empid, v_exact_match
from emp b, dept a
where
B.emp_id = a.dept_id and
(TRUNC(v_assigned_dtm) = TRUNC(i_date) OR
TRUNC(v_estimated_dtm) = TRUNC(i_date)) ;
End proc1;
Can i assign variables at left hand side on WHERE class?
i will post my errors shortly
Getting
Error(49,10): PL/SQL: ORA-00932: inconsistent datatypes: expected DATE got NUMBER
[Updated on: Mon, 04 August 2014 09:07] Report message to a moderator
|
|
|
Re: SQL [message #620592 is a reply to message #620591] |
Mon, 04 August 2014 09:09 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
ORAGENASHOK wrote on Mon, 04 August 2014 19:32to_date(sysdate,'dd-mon-yyyy hh24:mi:ss')
Why to_date on sysdate?
Quote:
i will post my errors shortly
Ok, will wait for that.
And how about giving feedback on your previous topic?
|
|
|
Re: SQL [message #620593 is a reply to message #620592] |
Mon, 04 August 2014 09:13 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Ok ,I see you have updated your post with the error message. But you did not show it properly, else you could debug it yourself.
This :
Is the issue, since v_estimated_dtm is NOT a DATE data type which you use in your decode statement alongwith a DATE type.
Use, to_date to convert the date literal to DATE. And to_char to display it in the required format. You seem to not understand how date works and you are doing just the opposite.
[Updated on: Mon, 04 August 2014 09:15] Report message to a moderator
|
|
|
Re: SQL [message #620594 is a reply to message #620593] |
Mon, 04 August 2014 09:25 |
ORAGENASHOK
Messages: 240 Registered: June 2006 Location: Chennai
|
Senior Member |
|
|
Lalit, cant understand
Am in currently this development will resume to previous topic shortly
|
|
|
|
Re: SQL [message #620598 is a reply to message #620594] |
Mon, 04 August 2014 09:37 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Re-read your code. Properly.
Let's start with this:
v_assigned_dtm := v_assigned_dtm_local ;
What is the datatype of v_assigned_dtm?
What is the datatype of v_assigned_dtm_local?
What is the value of v_assigned_dtm_local?
How did you think that was going to do anything other than error out?
|
|
|
Re: SQL [message #620600 is a reply to message #620598] |
Mon, 04 August 2014 09:48 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Oops, BS and CM, my bad sorry. I didn't notice those incorrect assignments and jumped to decode statement. But yes the root cause is the incorrect data types involved.
I see OP doing just the opposite, initially he did to_date on sysdate. Then as you guys pointed out, he assigned string to date. And then carried the same mistake later in decode statement.
@OP, please read this nice article http://edstevensdba.wordpress.com/2011/04/07/nls_date_format/
|
|
|
|
Re: SQL [message #620639 is a reply to message #620607] |
Mon, 04 August 2014 12:28 |
ORAGENASHOK
Messages: 240 Registered: June 2006 Location: Chennai
|
Senior Member |
|
|
I have miss placed the code its my mistake.
create or replace
procedure proc1
(
i_flg varchar2,
i_flg1 varchar2,
i_date date
)
AS
v_assigned_dtm_utc varchar2(100) := 'assigned_dtm_utc' ;
v_estimated_dtm_utc varchar2(100) := 'estimated_dtm_utc' ;
v_assigned_dtm_local varchar2(100) := 'assigned_dtm_local' ;
v_estimated_dtm_local varchar2(100) := 'estimated_dtm_local' ;
v_assigned_dtm varchar2(100);
v_estimated_dtm varchar2(100);
v_flg varchar2(1);
v_flg1 varchar2(1);
v_date date;
v_empid number ;
v_exact_match varchar2(1) ;
--i_date date := sysdate ;
Begin
if ( i_flg = 'D') and ( i_flg1 = 'L' ) then
v_assigned_dtm := v_assigned_dtm_local ;
v_estimated_dtm := v_estimated_dtm_local ;
v_date := i_date ;
elsif ( i_flg = 'D') and ( i_flg1 = 'U' ) then
v_assigned_dtm := v_assigned_dtm_utc ;
v_estimated_dtm := v_estimated_dtm_utc ;
v_date := i_date ;
End if ;
select b.emp_id,
decode(
decode( trunc(v_assigned_dtm), TRUNC(i_date),'Y','N'),'Y','Y',
decode( trunc(v_estimated_dtm), TRUNC(i_date),'Y','N'),'N'
) exact_match
into v_empid, v_exact_match
from emp b, dept a
where
B.emp_id = a.dept_id and
(TRUNC(v_assigned_dtm) = TRUNC(i_date) OR
TRUNC(v_estimated_dtm) = TRUNC(i_date)) ;
End proc1;
[Updated on: Mon, 04 August 2014 12:31] Report message to a moderator
|
|
|
|
|
Re: SQL [message #620665 is a reply to message #620591] |
Tue, 05 August 2014 03:38 |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
@cookiemonster: It seems he change data types of some variables from DATE to VARCHAR2, so now he is assigning content of VARCHAR2 variable to another VARCHAR2 variable. Very useful indeed.
@ORAGENASHOK:
It seems to me (note that this is just a guess as your problem description is close to zero; it would be nice if you stated what is that code supposed to do) that you try to use column which name is stored in the variable. It is not possible in static SQL, you would have to abuse dynamic SQL.
Or, in this case, simply statically determine that columns with the same conditions you used in PL/SQL, something like:
with temp_table_with_comp_dates as
( select case when i_flg = 'D' and i_flg1 = 'L' then assigned_dtm_local
when i_flg = 'D' and i_flg1 = 'U' then assigned_dtm_utc
end assigned_dtm,
case when i_flg = 'D' and i_flg1 = 'L' then estimated_dtm_local
when i_flg = 'D' and i_flg1 = 'U' then estimated_dtm_utc
end estimated_dtm,
<whichever other columns used>
from <the table containing these DATE columns> )
select <use assigned_dtm and estimated_dtm instead those variables>
into v_empid, v_exact_match
from help_table_with_comp_dates, <other table(s) not included above>
where <join and filtering conditions; again use use assigned_dtm and estimated_dtm>;
(note that with the detailness of the question you posted, this is the best educated guess I could make)
|
|
|
Re: SQL [message #620669 is a reply to message #620665] |
Tue, 05 August 2014 03:52 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
flyboy wrote on Tue, 05 August 2014 09:38@cookiemonster: It seems he change data types of some variables from DATE to VARCHAR2, so now he is assigning content of VARCHAR2 variable to another VARCHAR2 variable. Very useful indeed.
That's what I get for skim reading. Of course that change just moves the problem.
|
|
|
Goto Forum:
Current Time: Fri Apr 26 03:47:22 CDT 2024
|