create or replace function fun_getadded(buildId    nvarchar2,
                                        gatewayId  nvarchar2,
                                        hour_start date,
                                        hour_end   date,
                                        itemId     nvarchar2)
  return nvarchar2 is
  maxtime               date;
  currentvalue          nvarchar2(50);
  current_real_value    number;
  current_virtual_value number;
  lastvalue             nvarchar2(50);
  rslt                  number;
begin
  lastvalue    := '0.00';
  currentvalue := '0.00';  begin
      execute immediate   
      'select max(fv.gettime)
      from t_d_functionvalue fv
      join t_d_function f on f.buildid || ''-'' || f.gatewayid || ''-'' ||
                             f.meterid || ''-'' || f.functionId = fv.functionid
      left join t_d_functionsplit fs on fs.functionid = f.id
      where fv.gettime >=' || hour_start ||
       ' and fv.gettime < ' || hour_end ||
       ' and fv.iserror = ''0''
        and (fv.functiontype =''' || itemId || ''' or fs.type =''' || itemId || ''')
        and fv.functionId like ''' || buildId || '-' || gatewayId || '%''' into maxtime;
  end;
end fun_getadded;
请问execute immediate使用上有什么错误吗  执行的时候总是报:"无效的SQL语句"

解决方案 »

  1.   

     execute immediate   
          'select max(fv.gettime)
          from t_d_functionvalue fv
          join t_d_function f on f.buildid || ''-'' || f.gatewayid || ''-'' ||
                                 f.meterid || ''-'' || f.functionId = fv.functionid
          left join t_d_functionsplit fs on fs.functionid = f.id
          where fv.gettime >=' || hour_start ||
           ' and fv.gettime < ' || hour_end ||
           ' and fv.iserror = ''0''
            and (fv.functiontype =''' || itemId || ''' or fs.type =''' || itemId || ''')
            and fv.functionId like ''' || buildId || '-' || gatewayId || '%''' into maxtime'--这里少了个单引号;
      

  2.   

    字段的拼接的地方这样改:
    on f.buildid || '||'-'||' || f.gatewayid || '||'-'||' ||
                                 f.meterid || '||'-'||' || f.functionId 
      

  3.   


    拼接的 以及没有return 的错误最好是把你的三个表的结构以及代表性的数据弄出来 别人好帮个你解决
      

  4.   

     where fv.gettime >=' || hour_start ||' 
       and fv.gettime < ' || hour_end ||' 改成 where fv.gettime >=''' || hour_start ||'''
       and fv.gettime < ''' || hour_end ||''' 最好to_char成指定格式
      

  5.   

    你先把  字符串拼接起来 然后先运行一下看看不是用execute的时候的正确性的。
      

  6.   

    v_sql:= '';  ---你的sqldbms_output.putline(v_sql);
    EXECUTE immediate v_sql;然后在看打印出来的sql有啥问题,就知道了也可以把sql 写入到一个日志表调试一下的嘛
      

  7.   

    你的拼接没问题,你要看一下,你的sql执行之后最大的时间有几个值, into 一个变量,是只能接受一个值的吧,cursor才能接受多个值,或者数组
      

  8.   

    楼上的兄弟,你定义的v_sql变量是有大小限制的,像lz写的格式,不知道超过varchar2的范围没,要小心哟
      

  9.   

    将SQL取出来执行一下,不就知道问题在那了吗?
      

  10.   

    嗯,支持用DBMS_OUTPUT调试,如果超过DBMS_OUTPUT输出大小,可以重新设置OUTPUT SIZE或直接输出语句到文件上。
      

  11.   

    SQL为啥有问题应当先打印出来看看SQL是什么,这么看大家都是晕的,谁知道你拼完后SQL啥样子的呢,不过我一眼看过去看到一个错误,不知道有没有其他的错误哈:时间比较,你尽然把DATE类型拼到字符串后面,你真强,DATE一旦变成字符串,将会按照默认的nls_date_format格式,这个拼完后肯定是没法和日期格式比较的,这里肯定是不行了,有没有其他错误你自己找吧,一眼也看不出来。
      

  12.   

    SQL拼接中日期型处理有问题
    1、拼接的时候需要将DATE型转为VARCHAR2(用TO_CHAR)
    2、执行的时候需要将VARCHAR2转回DATE(用TO_DATE)
    这句:
    fv.gettime >=' || hour_start || ' and fv.gettime < ' || hour_end
    改为:
    fv.gettime >=TO_DATE(''' || TO_CHAR(hour_start,'YYYYMMDD')
      || ''',''YYYYMMDD'') and fv.gettime < TO_DATE(''' || TO_CHAR(hour_end,'YYYYMMDD')||''')'
      

  13.   

    上面第二个后面少了TO_DATE函数的后面格式串,参照第一个写
      

  14.   

    应该是动态的sql除了问题你检查一下。
      

  15.   

    sql 拼接有问题,建议先用变量接收。后执行
    v_SQL       varchar2(2000);
    v_SQL :='select max(fv.gettime)
          from t_d_functionvalue fv
          join t_d_function f on f.buildid || ''-'' || f.gatewayid || ''-'' ||
                                 f.meterid || ''-'' || f.functionId = fv.functionid
          left join t_d_functionsplit fs on fs.functionid = f.id
          where fv.gettime >=' || hour_start ||
           ' and fv.gettime < ' || hour_end ||
           ' and fv.iserror = ''0''
            and (fv.functiontype =''' || itemId || ''' or fs.type =''' || itemId || ''')
            and fv.functionId like ''' || buildId || '-' || gatewayId || '%'''
    execute immediate v_SQL into maxtime;
    这样发现错误方便DEbug
      

  16.   


    sqlStr := 'select max(fv.gettime)      
          from t_d_functionvalue fv
          join t_d_function f on f.buildid || ''-'' || f.gatewayid || ''-'' ||
                                 f.meterid || ''-'' || f.functionId =
                                 fv.functionid
          left join t_d_functionsplit fs on fs.functionid = f.id
         where fv.gettime >=to_date(''' ||
                 to_char(hour_start, 'yyyy-mm-dd hh24:mi:ss') || ''',''yyyy-mm-dd hh24:mi:ss'')
           and fv.gettime <to_date(''' ||
                 to_char(hour_end, 'yyyy-mm-dd hh24:mi:ss') ||''',''yyyy-mm-dd hh24:mi:ss'')
           and fv.iserror = ''0''
           and (fv.functiontype =''' || itemId ||
                 ''' or fs.type =''' || itemId || ''')
           and fun_GetBuildIdByUniqueId(fv.functionId) = ''' ||
                 buildId || '''
           and fun_GetGateWayIdByUniqueId(fv.functionId)=''' ||
                 gatewayId || '''';
      
        execute immediate sqlStr into maxtimeOK  好久没上CSDN了  没想到这么多人回答