在Oracle中创建一个存储过程,把日期字段做为参数传递,返回结果集,如下2种方法:
一、
CREATE OR REPLACE PROCEDURE BAANERP.proc_datetime (
   p_cursor   IN OUT   TYPES.cursortype,
   dt                  DATE
)
AS
BEGIN
   OPEN p_cursor FOR    
   'SELECT * from 表名 where 日期字段>to_date('||dt||',YYYY-MM-DD HH24:MI:SS)';   
END proc_datetime;
/编译通过,报错信息:文字与格式字符串不匹配 *************************************************************************************************CREATE OR REPLACE PROCEDURE BAANERP.proc_datetime (
    p_cursor   IN OUT   TYPES.cursortype,
   dt         date,
   Pcount     out number
)
AS
    v_sql      VARCHAR2(1000);
BEGIN  
         v_sql:='SELECT * from tcprpd400 where t$psdt>to_date('||dt||',YYYY-MM-DD HH24:MI:SS)'; 
         execute immediate v_sql into Pcount;
          v_sql:='select * from tcprpd400';
          OPEN p_cursor FOR v_sql;                    
END proc_datetime;
/编译通过,报错信息同方法一*********************************************************************************************************急求解,万分感谢!

解决方案 »

  1.   

    两个错,
    1 to_date里要有引号
    2 直接写dt是隐式转换,可能和你要的结果不一样应该写成:
    'SELECT * from 表名 where 日期字段 >to_date( ''' | |
    to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') | | ''',YYYY-MM-DD HH24:MI:SS'') '
      

  2.   

    修改如下:
    CREATE OR REPLACE PROCEDURE BAANERP.proc_datetime (
       p_cursor   IN OUT   TYPES.cursortype,
       dt         OUT DATE
    )
    AS
    BEGIN
       OPEN p_cursor FOR    
       --'SELECT * from tcprpd400090 where t$psdt>to_date('||dt||',YYYY-MM-DD HH24:MI:SS)';
       
    'SELECT * from 表名 where 日期字段  >to_date('''||to_char(||dt||, 'YYYY-MM-DD HH24:MI:SS ')||''',YYYY-MM-DD HH24:MI:SS'')';   
    END proc_datetime;报错信息:
    10/59   PLS-00103: 出现符号 "|"在需要下列之一时:
              ( ) - + case mod new not null
              others <an identifier> <a double-quoted delimited-identifier>
              <a bind variable> table avg count current exists max min
              prior sql stddev sum variance execute multiset the both
              leading trailing forall merge year month DAY_ hour minute
              second timezone_hour timezone_minute timezone_region
              timezone_abbr time timestamp interval date
              <a string literal with character set specification>
              <a number> <a single-quoted SQL s请问该如何修改!
      

  3.   

    to_char( dt,  'YYYY-MM-DD HH24:MI:SS  '),不要||
      

  4.   

    感谢楼上的解答!!!还是有问题,感谢再次解答,修改如下:CREATE OR REPLACE PROCEDURE BAANERP.proc_datetime (
       p_cursor   IN OUT   TYPES.cursortype,
       dt         OUT DATE
    )
    AS
    BEGIN
       OPEN p_cursor FOR    
       --'SELECT * from tcprpd400090 where t$psdt>to_date('||dt||',YYYY-MM-DD HH24:MI:SS)';
       
    'SELECT * from 表名 where 日期字段  >to_date('''||dt||''',YYYY-MM-DD HH24:MI:SS'')';   
    END proc_datetime;
    /报错信息:文字与字符串格式不匹配
      

  5.   

    修改成:
    一、'SELECT * from 表名 where 日期字段  >to_date('to_char(dt,'YYYY-MM-DD HH24:MI:SS'),',YYYY-MM-DD HH24:MI:SS'')'; 二、'SELECT * from 表名 where 日期字段  >to_date('''to_char(dt,'YYYY-MM-DD HH24:MI:SS'),''',YYYY-MM-DD HH24:MI:SS'')'; 编译不通过,报错信息如下:
    10/47   PLS-00103: 出现符号 "TO_CHAR"在需要下列之一时:
              * & = - + ; < / > at in
              is mod remainder not rem <an exponent (**)> <> or != or ~= >=
              <= <> and or like LIKE2_ LIKE4_ LIKEC_ between using ||
              multiset member SUBMULTISET_
              符号 "*" 被替换为 "TO_CHAR" 后继续。
    10/83   PLS-00103: 出现符号 ","在需要下列之一时:
              . ( * % & = - + ; < / > at
              in is mod remainder not rem <an exponent (**)> <> or != or ~=
              >= <= <> and or like LIKE2_ LIKE4_ LIKEC_ between using ||
              member SUBMULTISET_
              符号 "*在 "," 继续之前已插入。
      

  6.   

    dt已经是date了,不需要to_date
    'SELECT * from 表名 where 日期字段   >  '|| dt
      

  7.   

    修改成:
    一、
    'SELECT * from tcprpd400090 where t$psdt>to_date('''||to_char(dt,'YYYY-MM-DD HH24:MI:SS')||''',YYYY-MM-DD HH24:MI:SS'')';
    二、
    'SELECT * from tcprpd400090 where t$psdt>to_date('||to_char(dt,'YYYY-MM-DD HH24:MI:SS')||',YYYY-MM-DD HH24:MI:SS'')';报错信息:文字与字条串格式不匹配
      

  8.   

    CREATE OR REPLACE PROCEDURE BAANERP.proc_datetime (
       p_cursor   IN OUT   TYPES.cursortype,
       v_date              CHAR
    )
    AS
       str   VARCHAR2 (2000) := '';
    BEGIN
       OPEN p_cursor FOR    'SELECT * from tcprpd400090 where t$plsd>to_date('
                         || v_date
                         || ',''yyyy-MM-dd HH24:MI:SS'')';
    END proc_datetime;
    /
      

  9.   

    给你个例子看看吧
    CREATE OR REPLACE Procedure Pro_Mygoods As
    Store  Varchar2(20);
    Gcode  Varchar2(20);
    Begind Varchar2(40);
    Endd   Varchar2(40);
      login Varchar(20);
      ssMonth Varchar(4);
    Cursor Idsku Is
    Select Distinct loginid,ss_month, c_Store_Id, Sku, To_Char(Begin_Date, 'yyyy-mm-dd'), To_Char(End_Date, 'yyyy-mm-dd') From Mr_Idsku Order By  loginid,ss_month,c_store_id,sku;
    Begin
      Delete From jx_mygoods;
    Open Idsku;
    Loop
    Fetch Idsku
    Into login,ssmonth,Store, Gcode, Begind, Endd;
    Exit When Idsku% Notfound;
    Insert Into Jx_Mygoods(loginid,SS__MONTH,c_store_id,c_gcode,c_begin,c_end,c_sale,c_name)
    (Select login,ssmonth,c_Store_Id, c_Gcode, To_Date(begind, 'yyyy-mm-dd'), To_Date(endd, 'yyyy-mm-dd'), Sum(c_Sale),c_name
     From Jx_Day_Js
     Where c_Gcode = gcode And c_Store_Id Like '%'||Store||'%' And To_Char(c_Dt, 'yyyy-mm-dd') >= begind And
     To_Char(c_Dt, 'yyyy-mm-dd') <= endd
     Group By login,ssmonth,c_Store_Id, c_Gcode,C_NAME);
    Commit;
    End Loop;
    Close Idsku;
    End;