我正在创建一个存储函数,该函数应该通过一个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函数应该在查询期间执行,但它刚刚使我的数据库崩溃。
2条答案
按热度按时间jei2mxaa1#
您是否可以尝试使用
DBMS_SQL
来解析以下类型的查询:PS.我从查询中删除了重复的条件。
r7xajy2e2#
我刚刚更改了nls_date_format