oracle 如何在PL sql中通过varchar查询进行查询而不丢失日期信息?

e0bqpujr  于 2022-11-03  发布在  Oracle
关注(0)|答案(2)|浏览(135)

我正在创建一个存储函数,该函数应该通过一个varchar进行查询,但我丢失了Date变量中的小时数。
这是一个工作查询,应该会给予下列记录。

SELECT
                RESERVATIONS.NUMERO,
                RESERVATIONS.DATE_DEBUT_PRECIS,
                RESERVATIONS.DATE_FIN_PRECIS
                FROM RESERVATIONS, LIGNES_RESERVATIONS, OBJETS, CLIENTS
                WHERE
                LIGNES_RESERVATIONS.OBJ_NUMERO = 261 AND
                LIGNES_RESERVATIONS.OBJ_SOCIETES_ID = 5 AND
                LIGNES_RESERVATIONS.SOCIETES_ID = 5 AND
                OBJETS.NUMERO = LIGNES_RESERVATIONS.OBJ_NUMERO AND
                OBJETS.SOCIETES_ID = LIGNES_RESERVATIONS.OBJ_SOCIETES_ID AND
                OBJETS.SOCIETES_ID = 5 AND
                RESERVATIONS.SOCIETES_ID = 5 AND
                RESERVATIONS.DEMANDE = 0 AND
                RESERVATIONS.ANNULER = 0 AND
                LIGNES_RESERVATIONS.RES_NUMERO = RESERVATIONS.NUMERO AND
                LIGNES_RESERVATIONS.RES_SOCIETES_ID = RESERVATIONS.SOCIETES_ID AND
                CLIENTS.NUMERO = RESERVATIONS.CLI_NUMERO AND
                CLIENTS.SOCIETES_ID = RESERVATIONS.CLI_SOCIETES_ID AND
                CLIENTS.SOCIETES_ID = 5 AND
                (TO_DATE('03.10.2022 23:00', 'dd.mm.YYYY hh24:mi') > RESERVATIONS.DATE_DEBUT_PRECIS AND TO_DATE('03.10.2022 07:00', 'dd.mm.YYYY hh24:mi') < RESERVATIONS.DATE_FIN_PRECIS)

编号日期_发货日期_结束日期94065 2022年10月3日2022年10月3日
93995年10月22日
问题是请求中给定的日期和时间来自变量。
下面是我在函数中进行查询的方式:

sql_stmt VARCHAR2(2000) := 'SELECT
                RESERVATIONS.NUMERO,
                RESERVATIONS.DATE_DEBUT_PRECIS,
                RESERVATIONS.DATE_FIN_PRECIS
                FROM RESERVATIONS, LIGNES_RESERVATIONS, OBJETS, CLIENTS
                WHERE
                LIGNES_RESERVATIONS.OBJ_NUMERO = '||P_OBJET||' AND
                LIGNES_RESERVATIONS.OBJ_SOCIETES_ID = '||P_SOCIETE||' AND
                LIGNES_RESERVATIONS.SOCIETES_ID = '||P_SOCIETE||' AND
                OBJETS.NUMERO = LIGNES_RESERVATIONS.OBJ_NUMERO AND
                OBJETS.SOCIETES_ID = LIGNES_RESERVATIONS.OBJ_SOCIETES_ID AND
                OBJETS.SOCIETES_ID = '||P_SOCIETE||' AND
                RESERVATIONS.SOCIETES_ID = '||P_SOCIETE||' AND
                RESERVATIONS.DEMANDE = 0 AND
                RESERVATIONS.ANNULER = 0 AND
                LIGNES_RESERVATIONS.RES_NUMERO = RESERVATIONS.NUMERO AND
                LIGNES_RESERVATIONS.RES_SOCIETES_ID = RESERVATIONS.SOCIETES_ID AND
                CLIENTS.NUMERO = RESERVATIONS.CLI_NUMERO AND
                CLIENTS.SOCIETES_ID = RESERVATIONS.CLI_SOCIETES_ID AND
                CLIENTS.SOCIETES_ID = '||P_SOCIETE||' AND
                '|| P_DATE_FIN ||' > RESERVATIONS.DATE_DEBUT_PRECIS AND '|| P_DATE_DEBUT ||' < RESERVATIONS.DATE_FIN_PRECIS';

但是,我的查询如下所示

SELECT
                RESERVATIONS.NUMERO,
                RESERVATIONS.DATE_DEBUT_PRECIS,
                RESERVATIONS.DATE_FIN_PRECIS
                FROM RESERVATIONS, LIGNES_RESERVATIONS, OBJETS, CLIENTS
                WHERE
                LIGNES_RESERVATIONS.OBJ_NUMERO = 261 AND
                LIGNES_RESERVATIONS.OBJ_SOCIETES_ID = 5 AND
                LIGNES_RESERVATIONS.SOCIETES_ID = 5 AND
                OBJETS.NUMERO = LIGNES_RESERVATIONS.OBJ_NUMERO AND
                OBJETS.SOCIETES_ID = LIGNES_RESERVATIONS.OBJ_SOCIETES_ID AND
                OBJETS.SOCIETES_ID = 5 AND
                RESERVATIONS.SOCIETES_ID = 5 AND
                RESERVATIONS.DEMANDE = 0 AND
                RESERVATIONS.ANNULER = 0 AND
                LIGNES_RESERVATIONS.RES_NUMERO = RESERVATIONS.NUMERO AND
                LIGNES_RESERVATIONS.RES_SOCIETES_ID = RESERVATIONS.SOCIETES_ID AND
                CLIENTS.NUMERO = RESERVATIONS.CLI_NUMERO AND
                CLIENTS.SOCIETES_ID = RESERVATIONS.CLI_SOCIETES_ID AND
                CLIENTS.SOCIETES_ID = 5 AND
                03.10.2022 > RESERVATIONS.DATE_DEBUT_PRECIS AND 03.10.2022 < RESERVATIONS.DATE_FIN_PRECIS

正如我们所看到的,查询中没有小时数规范,因此我尝试通过这样做来强制将其包含在查询中:“TO_CHAR(P_DATE_FIN,'dd.mm.YYYY hh24:mi')"。但是,它不起作用,我无法从查询中获得任何结果,因此我尝试将其转换回查询中的日期值,如下所示:“结束日期('''|| TO_CHAR(P_DATE_FIN,'dd.mm.YYYY hh24:mi')”(TO_DATE函数应该在查询期间执行,但它刚刚使我的数据库崩溃。

jei2mxaa

jei2mxaa1#

您是否可以尝试使用DBMS_SQL来解析以下类型的查询:

DECLARE
  lv_sql    VARCHAR2(500);
  l_objet   VARCHAR2(200);
  l_societe VARCHAR2(200);
  l_dt_deb  DATE;
  l_dt_fin  DATE;
  l_numero  VARCHAR2(200);
  l_debut_precis DATE;
  l_fin_precis   DATE;
  ln_cursor_id NUMBER;
  ln_rows_processed;
BEGIN
  l_objet   := p_objet;
  l_societe := p_societe;

  SELECT TO_DATE(P_DATE_FIN, 'DD.MM.YYYY HH24:MI'),TO_DATE(P_DATE_DEBUT, 'DD.MM.YYYY HH24:MI')
    INTO l_dt_fin, l_dt_deb
    FROM dual;

  lv_sql:='SELECT RESERVATIONS.NUMERO,
           RESERVATIONS.DATE_DEBUT_PRECIS,
           RESERVATIONS.DATE_FIN_PRECIS
           FROM RESERVATIONS, LIGNES_RESERVATIONS, OBJETS, CLIENTS
           WHERE
           LIGNES_RESERVATIONS.OBJ_NUMERO = :objet AND
           LIGNES_RESERVATIONS.OBJ_SOCIETES_ID = :societe AND
           LIGNES_RESERVATIONS.SOCIETES_ID = LIGNES_RESERVATIONS.OBJ_SOCIETES_ID AND
           OBJETS.NUMERO = LIGNES_RESERVATIONS.OBJ_NUMERO AND
           OBJETS.SOCIETES_ID = LIGNES_RESERVATIONS.OBJ_SOCIETES_ID AND
           RESERVATIONS.SOCIETES_ID = LIGNES_RESERVATIONS.OBJ_SOCIETES_ID AND
           RESERVATIONS.DEMANDE = 0 AND
           RESERVATIONS.ANNULER = 0 AND
           LIGNES_RESERVATIONS.RES_NUMERO = RESERVATIONS.NUMERO AND
           LIGNES_RESERVATIONS.RES_SOCIETES_ID = RESERVATIONS.SOCIETES_ID AND
           CLIENTS.NUMERO = RESERVATIONS.CLI_NUMERO AND
           CLIENTS.SOCIETES_ID = RESERVATIONS.CLI_SOCIETES_ID 
           AND :date_fin > RESERVATIONS.DATE_DEBUT_PRECIS AND :date_debut < RESERVATIONS.DATE_FIN_PRECIS';

  in_cursor_id:=DBMS_SQL.OPEN_CURSOR;

  DBMS_SQL.PARSE(ln_cursor_id, lv_sql, DBMS_SQL.NATIVE);

  DBMS_SQL.BIXD_VARLABLE(ln_cursor_id:'objet',l_objet);
  DBMS_SQL.BIXD_VARLABLE(ln_cursor_id:'societe',l_societe);
  DBMS_SQL.BIXD_VARLABLE(ln_cursor_id:'date_fin',l_dt_fin);
  DBMS_SQL.BIXD_VARLABLE(ln_cursor_id:'date_debut',l_dt_deb);

  DBMS_SQL.DEFINE_COLUMN(ln_cursor_id,1,l_numero);
  DBMS_SQL.DEFINE_COLUMN(ln_cursor_id,2,l_debut_precis);
  DBMS_SQL.DEFINE_COLUMN(ln_cursor_id,3,l_fin_precis);

  ln_rows_processed := DBMS_SQL.EXECUTE(ln_cursor_id);

  LOOP
    IF DBMS_SQL.FETCH_ROWS(ln_cursor_id)=0 THEN
      EXIT;
    ELSE
      DBMS_SQL.COLUMN_VALUE(ln_cursor_id,1,l_numero); 
      DBMS_SQL.COLUMN_VALUE(ln_cursor_id,2,l_debut_precis);
      DBMS_SQL.COLUMN_VALUE(ln_cursor_id,3,l_fin_precis);

      DBMS_OUTPUT.put_line(l_numero ||'|'|| TO_CHAR(l_debut_precis) ||'|'|| TO_CHAR(l_fin_precis));
    END IF;
  END LOOP;
  DBMS_SQL.CLOSE_ClIRSOR(ln_cursor_id);
END;

PS.我从查询中删除了重复的条件。

r7xajy2e

r7xajy2e2#

我刚刚更改了nls_date_format

execute immediate 'alter session set nls_date_format=''dd.mm.YYYY hh24:mi''';
sql_stmt := 'SELECT
                RESERVATIONS.NUMERO,
                RESERVATIONS.DATE_DEBUT_PRECIS,
                RESERVATIONS.DATE_FIN_PRECIS
                FROM RESERVATIONS, LIGNES_RESERVATIONS, OBJETS, CLIENTS
                WHERE
                LIGNES_RESERVATIONS.OBJ_NUMERO = '||P_OBJET||' AND
                LIGNES_RESERVATIONS.OBJ_SOCIETES_ID = '||P_SOCIETE||' AND
                LIGNES_RESERVATIONS.SOCIETES_ID = '||P_SOCIETE||' AND
                OBJETS.NUMERO = LIGNES_RESERVATIONS.OBJ_NUMERO AND
                OBJETS.SOCIETES_ID = LIGNES_RESERVATIONS.OBJ_SOCIETES_ID AND
                OBJETS.SOCIETES_ID = '||P_SOCIETE||' AND
                RESERVATIONS.SOCIETES_ID = '||P_SOCIETE||' AND
                RESERVATIONS.DEMANDE = 0 AND
                RESERVATIONS.ANNULER = 0 AND
                LIGNES_RESERVATIONS.RES_NUMERO = RESERVATIONS.NUMERO AND
                LIGNES_RESERVATIONS.RES_SOCIETES_ID = RESERVATIONS.SOCIETES_ID AND
                CLIENTS.NUMERO = RESERVATIONS.CLI_NUMERO AND
                CLIENTS.SOCIETES_ID = RESERVATIONS.CLI_SOCIETES_ID AND
                CLIENTS.SOCIETES_ID = '||P_SOCIETE||' AND
                ''' || P_DATE_FIN ||''' > RESERVATIONS.DATE_DEBUT_PRECIS AND ''' || TO_CHAR(P_DATE_DEBUT, 'dd.mm.YYYY hh24:mi') ||''' < RESERVATIONS.DATE_FIN_PRECIS';

相关问题