直接改成变量就行了,关键你变量是怎么传入呢。在哪用呢,查询窗口中,还是程序中,还是存储过程、函数中在查询窗口直接执行的
用了 declare声明变量,报SQL错误

解决方案 »

  1.   

    直接改成变量就行了,关键你变量是怎么传入呢。在哪用呢,查询窗口中,还是程序中,还是存储过程、函数中在查询窗口直接执行的
    用了 declare声明变量,报SQL错误
    我用plsql developer。查询窗口使用下面sql试试
     select h.cbilltypecode, h.vnote, h.pk_corp, h.ts, '上游单据关闭' yuanyin
       from nc_ic_general_h h
      where h.cgeneralhid in (select datahid
                                from nc_synclog l
                               where l.synclog like '%关闭%'
                                 and l.ts >= &开始时间
                                 and l.ts <= &结束时间
                                 and l.unitcode = &类型
                                 and l.syncflag = '2')
     union allselect h.cbilltypecode, h.vnote, h.pk_corp, h.ts, '上游单据不存在' yuanyin
       from nc_ic_general_h h
      where h.cgeneralhid in (select datahid
                                from nc_synclog l
                               where l.synclog like '%不存在%'
                                 and l.ts >= &开始时间
                                 and l.ts <= &结束时间
                                 and l.unitcode = &类型
                                 and l.syncflag = '2')
      

  2.   

    这个可以使用动态SQL吧,不过动态SQL用来查询好麻烦的
    大体写了下
    create global temporary table temp(cbilltypecode VARCHAR2(20),vnote VARCHAR2(20),pk_corp VARCHAR2(20),ts VARCHAR2(20),yuanyin VARCHAR2(20)) ON COMMIT DELETE ROWS;
    declare 
    str_beg varchar2(20);
    str_end varchar2(20);
    str_uc varchar2(20);
    str_sql varchar(1000);
    begin
      str_beg:='2014-03-26 00:00:00';
      str_end:='2014-04-15 00:00:00';
      str_uc:='1002';
      str_sql='insert into temp select h.cbilltypecode, h.vnote, h.pk_corp, h.ts, ''上游单据关闭'' yuanyin   
      from nc_ic_general_h h  
      where h.cgeneralhid in (select datahid
                              from nc_synclog l                           
                              where l.synclog like ''%关闭%''                            
                              and l.ts >=:1                            
                              and l.ts <=:2                          
                              and l.unitcode =:3                           
                              and l.syncflag = '2') 
      union all
      select h.cbilltypecode, h.vnote, h.pk_corp, h.ts, ''上游单据不存在'' yuanyin   
      from nc_ic_general_h h  
      where h.cgeneralhid in (select datahid                            
                              from nc_synclog l                           
                              where l.synclog like ''%不存在%''                            
                              and l.ts >=:1                            
                              and l.ts <=:2                          
                              and l.unitcode =:3                           
                              and l.syncflag = '2') ';
    execute immediate str_sqlusing str_beg,str_end,str_uc;
    end;SELECT * FROM temp;
    其他简单方法我也不知道了,不建议使用这个。
    相比之下,sqlserver中满足你这需求就很简单了。
      

  3.   

    SQL有点错,有几个单引号没转义。
      

  4.   

    create global temporary table temp(cbilltypecode VARCHAR2(20),vnote VARCHAR2(20),pk_corp VARCHAR2(20),ts VARCHAR2(20),yuanyin VARCHAR2(20)) ON COMMIT DELETE ROWS;
    declare 
    str_beg varchar2(20);
    str_end varchar2(20);
    str_uc varchar2(20);
    str_sql varchar2(1000);
    begin
      str_beg:='2014-03-26 00:00:00';
      str_end:='2014-04-15 00:00:00';
      str_uc:='1002';
      str_sql='insert into temp select h.cbilltypecode, h.vnote, h.pk_corp, h.ts, ''上游单据关闭'' yuanyin   
      from nc_ic_general_h h  
      where h.cgeneralhid in (select datahid
                              from nc_synclog l                           
                              where l.synclog like ''%关闭%''                            
                              and l.ts >=:1                            
                              and l.ts <=:2                          
                              and l.unitcode =:3                           
                              and l.syncflag = ''2'') 
      union all
      select h.cbilltypecode, h.vnote, h.pk_corp, h.ts, ''上游单据不存在'' yuanyin   
      from nc_ic_general_h h  
      where h.cgeneralhid in (select datahid                            
                              from nc_synclog l                           
                              where l.synclog like ''%不存在%''                            
                              and l.ts >=:1                            
                              and l.ts <=:2                          
                              and l.unitcode =:3                           
                              and l.syncflag = ''2'') ';
    execute immediate str_sql using str_beg,str_end,str_uc;
    end;
      

  5.   

    直接改成变量就行了,关键你变量是怎么传入呢。在哪用呢,查询窗口中,还是程序中,还是存储过程、函数中在查询窗口直接执行的
    用了 declare声明变量,报SQL错误
    我用plsql developer。查询窗口使用下面sql试试
     select h.cbilltypecode, h.vnote, h.pk_corp, h.ts, '上游单据关闭' yuanyin
       from nc_ic_general_h h
      where h.cgeneralhid in (select datahid
                                from nc_synclog l
                               where l.synclog like '%关闭%'
                                 and l.ts >= &开始时间
                                 and l.ts <= &结束时间
                                 and l.unitcode = &类型
                                 and l.syncflag = '2')
     union allselect h.cbilltypecode, h.vnote, h.pk_corp, h.ts, '上游单据不存在' yuanyin
       from nc_ic_general_h h
      where h.cgeneralhid in (select datahid
                                from nc_synclog l
                               where l.synclog like '%不存在%'
                                 and l.ts >= &开始时间
                                 and l.ts <= &结束时间
                                 and l.unitcode = &类型
                                 and l.syncflag = '2')
    原来是这么用的……
    我还以为一定要和SQL SERVER一样先声明变量
      

  6.   


    额,报的缺失右括号,还少了什么吗……
    我用union之前的单条执行,也是确实右括号,错误的红线在变量后边
      

  7.   

    已解决,字符型的话,&变量用单引号包起来