没有结果从PL/SQL查询,但应该有


No results from PL/SQL query, but there should be.

我知道有几个问题关于这个错误,也为php。
我试过了大部分的答案,但似乎都不起作用。

这是我的查询:

"select
    id,
    employeenr,
    name,
    section,
    description,
    duration,
    to_date(substr(startdate,0,12)||' '||starttime, 'DD-MM-YYYY HH24:MI:SS') starttime,
    to_date(substr(enddate,0,12)||' '||endtime, 'DD-MM-YYYY HH24:MI:SS') endtime,
    worktime,
    statement,
    remark
    from
    data
    where   1=1
    ".$vw."
    AND startdate between to_date(':df 00:00:00', 'DD-MM-YYYY HH24:MI:SS') and to_date(':dt 23:59:59', 'DD-MM-YYYY HH24:MI:SS')
    ".$vw1."
    order by nvl(employeenr,0), startdate";
$parameters = array(':df' => $date_from, ':dt' => $date_to);

Echo var vw:

AND ( employeenr between '100000' and '199999'OR employeenr between '400000' and '499999'OR employeenr between '700000' and '799999'OR (employeenr is not null AND employeenr between '100000' and '199999'OR employeenr between '400000' and '499999'OR employeenr between '700000' and '799999'))
$date_from and $date_to:
$date_from = $_POST["date_from"]; // jQuery datepicker value
$date_to = $_POST["date_to"]; // jQuery datepicker value

我做错了什么?

$Parameters后来如何使用?看起来好像您想将它用作绑定变量数组,以便稍后传递给Oracle。如果是这样,那么您使用错误,因为字符串不是保存变量的值,而是包含SQL。这不是绑定变量在Oracle中的工作方式。

你必须给Oracle一个完整的查询来解析,只有查询中的可以是未知的(日期,数字或字符串)。这些你可以稍后通过。您不能解析一半的查询,然后在变量中添加更多的SQL。

所以AND ?不能被解析,因为这里没有值是有意义的。另一方面,像AND X = ?这样的东西可以被解析,因为Oracle希望在稍后执行查询时获得比较列X的值。

新问题,新答案

仍然不使用绑定变量作为变量(日期,数字或字符串),但尝试用它替换字符串的部分。这行不通。

':df 00:00:00'是一个字符串,包含冒号后面跟着'd'和'f'等。

对完整字符串使用绑定变量,即:

AND startdate >= to_date(:date_from, 'DD-MM-YYYY')
AND startdate < to_date(:date_to, 'DD-MM-YYYY') + 1

AND TRUNC(startdate) BETWEEN to_date(:date_from, 'DD-MM-YYYY') 
                         AND to_date(:date_to, 'DD-MM-YYYY')

AND startdate between to_date(:date_from || ' 00:00:00', 'DD-MM-YYYY HH24:MI:SS') 
                  and to_date(:date_to || ' 23:59:59', 'DD-MM-YYYY HH24:MI:SS')

SOLUTION

我已经能够通过在查询的SELECT区域中剥离子字符串来解决我的问题。

$sql_report = "
        select
        id,
        employeenr,
        name,
        section,
        description,
        duration,
        startdate,
        starttime,
        enddate,
        endtime,
        worktime,
        statement,
        remark
        from
        data
        where   1=1
        ".$vw."
        AND startdate BETWEEN to_date('$date_from', 'DD-MM-YYYY') AND to_date('$date_to', 'DD-MM-YYYY')
            ".$vw1."
        order by nvl(employeenr,0), startdate";
$parameters = array();