I need to pass a query in form of string to DBMS_XMLQUERY.GETXML package...the parameters to the query are date and varchar ..please help me..

I need to pass a query in form of string to DBMS_XMLQUERY.GETXML package...the parameters to the query are date and varchar ..please help me build the string .Below is the query and the out put. ( the string is building fine except the parameters are with out quotes)
here is the procedure
create or replace
procedure temp(
    P_MTR_ID VARCHAR2,
    P_FROM_DATE    IN DATE ,
    P_THROUGH_DATE IN DATE ) AS
    L_XML CLOB;
    l_query VARCHAR2(2000);
BEGIN
l_query:=  'SELECT
    a.s_datetime DATETIME,
    a.downdate Ending_date,
    a.downtime Ending_time,
    TO_CHAR(ROUND(a.downusage,3),''9999999.000'') kWh_Usage,
    TO_CHAR(ROUND(a.downcost,2),''$9,999,999.00'') kWh_cost,
    TO_CHAR(ROUND(B.DOWNUSAGE,3),''9999999.000'') KVARH
  FROM
    (SELECT s_datetime + .000011574 s_datetime,
      TO_CHAR(S_DATETIME ,''mm/dd/yyyy'') DOWNDATE,
      DECODE(TO_CHAR(s_datetime+.000011574 ,''hh24:'
      ||'mi''), ''00:'
      ||'00'',''24:'
      ||'00'', TO_CHAR(s_datetime+.000011574,''hh24:'
      ||'mi'')) downtime,
      s_usage downusage,
      s_cost downcost
    FROM summary_qtrhour
    WHERE s_mtrid = '
    ||P_MTR_ID||
   ' AND s_mtrch   = ''1''
    AND s_datetime BETWEEN TO_DATE('
    ||P_FROM_DATE||
    ',''DD-MON-YY'') AND (TO_DATE('
    ||P_THROUGH_DATE||
    ',''DD-MON-YY'') + 1)
    ) a,
    (SELECT s_datetime + .000011574 s_datetime,
      s_usage downusage
    FROM summary_qtrhour
    WHERE s_mtrid = '
    ||P_MTR_ID||
    ' AND s_mtrch   = ''2''
    AND s_datetime BETWEEN TO_DATE('
    ||P_FROM_DATE||
    ',''DD-MON-YY'') AND (TO_DATE('
    ||P_THROUGH_DATE||
    ','' DD-MON-YY'') + 1)
    ) B
  where a.S_DATETIME = B.S_DATETIME(+)';
SELECT DBMS_XMLQUERY.GETXML('L_QUERY') INTO L_XML   FROM DUAL;
INSERT INTO NK VALUES (L_XML);
DBMS_OUTPUT.PUT_LINE('L_QUERY IS :'||L_QUERY);
END;
OUTPUT parameters are in bold (the issue is they are coming without single quotes otherwise th equery is fine
L_QUERY IS :SELECT
    a.s_datetime DATETIME,
    a.downdate Ending_date,
    a.downtime Ending_time,
    TO_CHAR(ROUND(a.downusage,3),'9999999.000') kWh_Usage,
    TO_CHAR(ROUND(a.downcost,2),'$9,999,999.00') kWh_cost,
    TO_CHAR(ROUND(B.DOWNUSAGE,3),'9999999.000') KVARH
  FROM
    (SELECT s_datetime + .000011574 s_datetime,
      TO_CHAR(S_DATETIME ,'mm/dd/yyyy') DOWNDATE,
      DECODE(TO_CHAR(s_datetime+.000011574 ,'hh24:mi'), '00:00','24:00', TO_CHAR(s_datetime+.000011574,'hh24:mi')) downtime,
      s_usage downusage,
      s_cost downcost
    FROM summary_qtrhour
    WHERE s_mtrid = N3165 AND s_mtrch   = '1'
    AND s_datetime BETWEEN TO_DATE(01-JAN-13,'DD-MON-YY') AND (TO_DATE(31-JAN-13,'DD-MON-YY') + 1)
    ) a,
    (SELECT s_datetime + .000011574 s_datetime,
      s_usage downusage
    FROM summary_qtrhour
    WHERE s_mtrid = N3165 AND s_mtrch   = '2'
    AND s_datetime BETWEEN TO_DATE(01-JAN-13,'DD-MON-YY') AND (TO_DATE(31-JAN-13,' DD-MON-YY') + 1)
    ) B
  where a.S_DATETIME = B.S_DATETIME(+)

The correct way to handle this is to use bind variables.
And use DBMS_XMLGEN instead of DBMS_XMLQUERY :
create or replace procedure temp (
  p_mtr_id       in varchar2
, p_from_date    in date
, p_through_date in date
is
  l_xml   CLOB;
  l_query VARCHAR2(2000);
  l_ctx   dbms_xmlgen.ctxHandle;
begin
  l_query:=  'SELECT
    a.s_datetime DATETIME,
    a.downdate Ending_date,
    a.downtime Ending_time,
    TO_CHAR(ROUND(a.downusage,3),''9999999.000'') kWh_Usage,
    TO_CHAR(ROUND(a.downcost,2),''$9,999,999.00'') kWh_cost,
    TO_CHAR(ROUND(B.DOWNUSAGE,3),''9999999.000'') KVARH
  FROM
    (SELECT s_datetime + .000011574 s_datetime,
      TO_CHAR(S_DATETIME ,''mm/dd/yyyy'') DOWNDATE,
      DECODE(TO_CHAR(s_datetime+.000011574 ,''hh24:'
      ||'mi''), ''00:'
      ||'00'',''24:'
      ||'00'', TO_CHAR(s_datetime+.000011574,''hh24:'
      ||'mi'')) downtime,
      s_usage downusage,
      s_cost downcost
    FROM summary_qtrhour
    WHERE s_mtrid = :P_MTR_ID
    AND s_mtrch   = ''1''
    AND s_datetime BETWEEN TO_DATE(:P_FROM_DATE,''DD-MON-YY'')
                       AND (TO_DATE(:P_THROUGH_DATE,''DD-MON-YY'') + 1)
    ) a,
    (SELECT s_datetime + .000011574 s_datetime,
      s_usage downusage
    FROM summary_qtrhour
    WHERE s_mtrid = :P_MTR_ID
    AND s_mtrch   = ''2''
    AND s_datetime BETWEEN TO_DATE(:P_FROM_DATE,''DD-MON-YY'')
                       AND (TO_DATE(:P_THROUGH_DATE,'' DD-MON-YY'') + 1)
    ) B
  where a.S_DATETIME = B.S_DATETIME(+)';
  l_ctx := dbms_xmlgen.newContext(l_query);
  dbms_xmlgen.setBindValue(l_ctx, 'P_MTR_ID', p_mtr_id);
  dbms_xmlgen.setBindValue(l_ctx, 'P_FROM_DATE', to_char(p_from_date, 'DD-MON-YY'));
  dbms_xmlgen.setBindValue(l_ctx, 'P_THROUGH_DATE', to_char(p_through_date, 'DD-MON-YY'));
  l_xml := dbms_xmlgen.getXML(l_ctx);
  dbms_xmlgen.closeContext(l_ctx);
  insert into nk values (l_xml);
end;

Similar Messages

Maybe you are looking for

  • Help Please! Genius AND WiFi not working on iPod

    Hi, I'm using a first gen touch on the latest firmware. even though i have genius on iTunes, it will not transfer onto the iPod. There simply is no genius button. How can I get this working? Also, on an unrelated note, my WiFi is completely shot. The

  • Apple TV not showing up in device list

    I just got a new iMac and had everything trasfer over to it from my old MacMini. Now Apple TV will not show up in the iMac's iTunes' device list.

  • Servlets in Jar files  in WLS 6.1 ?

    Trying to port a WLS 5.1 servlet into 6.1 (an entirely different           animal it appears)           I have added an application to my config.xml file as below as well as           trying to start it           PasswordPolicy="wl_default_password_p

  • MS Project Client import to cProjects

    Hi all, does anybody have experience with the MS Project Client import to cProjects with regard to resource management? In my MS P file, I've"specified the fields "resource names" which should convert to "project roles" in cPro and "code" which shoul

  • Dust Under the Screen and Case Cracks

    Hey guys, I am the proud owner of an iPhone 3G S, and love the thing to pieces. I've had it for 8 months now, however over the past few weeks, I have become slightly disappointed with it. Firstly, a small crack appeared above the 'Silent On/Off' swit