没有正确关闭sql命令

时间:2017-08-01 11:40:19

标签: sql oracle plsql

我有这段代码:

def_where:=def_where||'  TO_CHAR(date_of_input,''MM'') like '''||'to_char(date_of_input,''MM'')=nvl(:DSP_month,to_char(date_of_input,''MM''))'||'%'  ||'to_char(date_of_input,''RRRR'')=nvl(:DSP_year,to_char(date_of_input,''RRRR''))'||'%'''; 

我得到错误sql命令未正确结束。

1 个答案:

答案 0 :(得分:0)

def_where建议,这是where子句的动态构建查询的一部分。但是生成的字符串毫无意义,它应该是这样的:

declare 
    def_where varchar2(32767) := '';
begin
    def_where  :=  def_where 
        || ' to_char(date_of_input, ''MM'') '
        || ' = nvl(:DSP_month, to_char(date_of_input, ''MM'')) and' 
        || ' to_char(date_of_input, ''RRRR'') '
        || ' = nvl(:DSP_year, to_char(date_of_input, ''RRRR''))';

  dbms_output.put_line(def_where);

end;

查看从dbms_output获得的内容,如果需要,请更正此语法。你也可以重写你的字符串来获得这个:

(extract(month from date_of_input) = :DSP_month or :DSP_month is null) and 
(extract(year  from date_of_input) = :DSP_year  or :DSP_year  is null)