ORA-06512 EXECUTE IMMEDIATE (merged 2 threads) [message #313710] |
Mon, 14 April 2008 04:54 |
sonikumari
Messages: 74 Registered: May 2006 Location: Mumbai
|
Member |
|
|
Hi,
I am trying to execute a query through EXECUTE IMMEDIATE.
This query runs when written as :-
SELECT MAX(TO_CHAR(f_ra_KWH1_reading_taken,v_dateformat)) INTO v_date
FROM t_rdg_abs_KWH1 WHERE f_ra_KWH1_reading_taken <= TO_DATE(p_start_date,v_dateformat)
AND f_ra_KWH1_validated = 1 AND f_ra_KWH1_fk_mp_id = TO_NUMBER(p_meter_profile_id)';
I have to run this query on runtime putting runtime variable:-
I looked for variables data through dbms_output and they are coming correctly to the variable.
But its not working when put into strsql as a string.
strsql :=
'SELECT MAX(TO_CHAR(f_ra_KWH1_reading_taken,v_dateformat))
FROM t_rdg_abs_KWH1
WHERE f_ra_KWH1_reading_taken <= TO_DATE (p_start_date,v_dateformat)
AND f_ra_KWH1_validated = 1
AND f_ra_KWH1_fk_mp_id = TO_NUMBER(p_meter_profile_id)';
Execute immediate strsql INTO v_date;
Please advice as why this is not working when taken in a string, is there some rule with execute immediate with strings sql.
Thanks,
Soni
[Updated on: Mon, 14 April 2008 04:55] Report message to a moderator
|
|
|
|
Re: Execute immediate [message #313713 is a reply to message #313710] |
Mon, 14 April 2008 05:00 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
Quote: | But its not working when put into strsql as a string.
| Is , generally speaking, not the most helpful comment. Try to be a bit more desriptive next time. Your issue lies in putting the parameter directly into the string. You have obviously read up in NDS as your code is pretty close to your requirements. Take a look at the USING clause of the execute immediate statement, it should give you an idea. any problems, post back
|
|
|
|
|
|
Re: Execute immediate [message #313750 is a reply to message #313749] |
Mon, 14 April 2008 07:28 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
Quote: | As per pablolee's advice I have bound the variables.
|
Nope, you have not bound the variables. Please look up the documentation on the USING clause.
|
|
|
|
Re: Execute immediate [message #313755 is a reply to message #313749] |
Mon, 14 April 2008 07:49 |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
sonikumari wrote on Mon, 14 April 2008 08:24 |
strsql := 'SELECT MAX(TO_CHAR(f_ra_'||v_dut_desc||'_reading_taken,'||v_dateformat||'))
|
MAX of a TO_CHAR of a DATE will give results as such:
12/01/1971 is greater than 04/14/2008.
04/14/2007 is greater than 03/31/2054.
|
|
|
Re: Execute immediate [message #313756 is a reply to message #313754] |
Mon, 14 April 2008 07:50 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
Sorry, no. It will take you very little time to read up on the use of the USING clause. Your lack of time is not our concern. The link supplied will take you a matter of minutes to read through.
|
|
|
|
|
|
Re: Execute immediate [message #313770 is a reply to message #313765] |
Mon, 14 April 2008 08:39 |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
sonikumari wrote on Mon, 14 April 2008 09:32 | Hi,
I read the pages in the link given and tried in my script.
strsql := 'SELECT MAX(TO_CHAR(f_ra_kwh1_reading_taken,:1))
|
Continue to be dense and ignorant to what people tell you and you'll get nowhere in life. MAX of a TO_CHAR of a DATE shows that you do not have the skills of a programmer and after being told so, you continue to do it, which shows you also do not have the skills to comprehend responses.
TEST1 FOO>create table foo (foo datE);
Table created.
TEST1 FOO>insert into foo values (to_date('12/31/1955','mm/dd/yyyy'));
1 row created.
TEST1 FOO>insert into foo values (to_date('01/01/2008','mm/dd/yyyy'));
1 row created.
TEST1 FOO>select max(to_char(foo,'mm/dd/yyyy')) from foo;
MAX(TO_CHA
----------
12/31/1955
[Updated on: Mon, 14 April 2008 08:44] Report message to a moderator
|
|
|
|
|
|
|
Re: Execute immediate [message #313779 is a reply to message #313776] |
Mon, 14 April 2008 09:10 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
You do NOT need to worry about putting apostrophes into your dynamic string (I would be more concerned as to why you are performing this task with NDS at all but let's leave that can of worm shut for the moment. All you need to concern yourself with is the correct structure of your EXECUTE IMMEDIATE statement (Joys point about the pointless to_char notwithstanding - You WILL have to deal with that).
Look at:
EXECUTE IMMEDIATE yourstring INTO your_variable(s) USING your_other_variable(s)
[Updated on: Mon, 14 April 2008 09:12] Report message to a moderator
|
|
|
ORA-06512: EXECUTE IMMEDIATE [message #313921 is a reply to message #313710] |
Tue, 15 April 2008 03:17 |
sonikumari
Messages: 74 Registered: May 2006 Location: Mumbai
|
Member |
|
|
Hi,
I am trying to concatenate a string with variable value and then execute it using EXECUTE IMMEDIATE.
The output string is coming correct and it runs in sql when run individually.
But the procedure as a whole is giving the error :-
The result string is :
SELECT TO_CHAR(MAX(f_ra_KWH1_reading_taken),'MM/DD/YYYY HH:MI:SS AM') FROM
t_rdg_abs_KWH1 WHERE f_ra_KWH1_reading_taken <= TO_DATE('1/23/2008 3:00:00
PM','MM/DD/YYYY HH:MI:SS AM') AND f_ra_KWH1_validated = 1 AND
f_ra_KWH1_fk_mp_id = TO_NUMBER(6748);
BEGIN pk_substitution.sp_substitute('1/23/2008 3:00:00 PM','1/23/2008 4:30:00 AM',6748); END;
*
ERROR at line 1:
ORA-00911: invalid character
ORA-06512: at "NOC_TEST.PK_SUBSTITUTION", line 138
ORA-06512: at "NOC_TEST.PK_SUBSTITUTION", line 55
ORA-06512: at line 1
My string is :-
strsql := 'SELECT TO_CHAR(MAX(f_ra_'||v_dut_desc||'_reading_taken),''MM/DD/YYYY HH:MI:SS AM'')'
||' FROM t_rdg_abs_'||v_dut_desc||
' WHERE f_ra_'||v_dut_desc||'_reading_taken <= '
||'TO_DATE('''||p_start_date||''',''MM/DD/YYYY HH:MI:SS AM'')'
||' AND f_ra_'||v_dut_desc||'_validated = 1 '
||' AND f_ra_'||v_dut_desc||'_fk_mp_id = TO_NUMBER('||p_meter_profile_id||')'||';';
The dbms output for strsql runs but its giving error when I execute the procedure.
At Line 138 I have the code :-
EXECUTE IMMEDIATE strsql INTO d_start_tc;
d_start_tc is:-
d_start_tc VARCHAR2(30);
Please suggest as where I am going wrong.
Thanks,
Soni
|
|
|