通过选择查询将日期和时间保存在变量中

发布于 2025-01-10 20:55:42 字数 548 浏览 0 评论 0原文

朋友,

我有一个存储过程,我需要根据另一个表的日期和时间验证日期和时间。

如何将 select * 的日期和时间作为变量传递,就像这个只传递日期但小时和分钟丢失一样。

如果该字段在两个表中保存了日期和时间,则该字段被验证。

感谢您的帮助。

PROCEDURE SPU_CUENTA
(
p_nro in varchar2,
   pr_Ret OUT number
) is
 vfecha date;
 vcount int;
begin


select COUNT(DFEC_SISTEMA) into vcount from TAB Where c=1;

IF vcount>0 THEN 
select DFEC_SISTEMA into vfecha from TAB Where c=1;

   EXECUTE IMMEDIATE  'SELECT COUNT(DFEC_ANULA) FROM tablab WHERE DFEC_ANULA>'''||vfecha||'''' into pr_Ret;
   
   END IF;

end;

friends,

I have a store procedure where I need to validate the date and time with respect to the date and time of another table.

How can I pass the date and time of a select * as a variable, just like this one only passes the date but the hours and minutes are lost.

the field is validated if it saves the date and time in the two tables.

thanks for any help.

PROCEDURE SPU_CUENTA
(
p_nro in varchar2,
   pr_Ret OUT number
) is
 vfecha date;
 vcount int;
begin


select COUNT(DFEC_SISTEMA) into vcount from TAB Where c=1;

IF vcount>0 THEN 
select DFEC_SISTEMA into vfecha from TAB Where c=1;

   EXECUTE IMMEDIATE  'SELECT COUNT(DFEC_ANULA) FROM tablab WHERE DFEC_ANULA>'''||vfecha||'''' into pr_Ret;
   
   END IF;

end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情仇皆在手 2025-01-17 20:55:42

为什么要使用立即执行?为什么不这样做:

SELECT COUNT(DFEC_ANULA) into pr_Ret FROM tablab WHERE DFEC_ANULA > vfecha;

或者将所有内容合并到一个查询中:

SELECT COUNT(DFEC_ANULA) into pr_Ret 
  FROM tablab 
 WHERE DFEC_ANULA > (select DFEC_SISTEMA from TAB Where c=1);

Why are you using EXECUTE IMMEDIATE? Why not this:

SELECT COUNT(DFEC_ANULA) into pr_Ret FROM tablab WHERE DFEC_ANULA > vfecha;

or combine everything into one query:

SELECT COUNT(DFEC_ANULA) into pr_Ret 
  FROM tablab 
 WHERE DFEC_ANULA > (select DFEC_SISTEMA from TAB Where c=1);
水溶 2025-01-17 20:55:42

在当前的动态语句中,您有:

EXECUTE IMMEDIATE  'SELECT ... WHERE DFEC_ANULA>'''||vfecha||'''' into pr_Ret

您正在连接日期变量的字符串表示形式。如果您的 NLS_DATE_FORMAT 设置为 DD-MON-RR(并且您的日期语言为英语),那么对于今天的日期,其计算结果为:

EXECUTE IMMEDIATE  'SELECT ... WHERE DFEC_ANULA>'''|| '02-MAR-22' ||'''' into pr_Ret

解析

EXECUTE IMMEDIATE  'SELECT ... WHERE DFEC_ANULA>''02-MAR-22''' into pr_Ret

该语句时,您将依赖于隐式再次转换,所以:

SELECT ... WHERE DFEC_ANULA>'02-MAR-22'

然后

SELECT ... WHERE DFEC_ANULA>TO_DATE('02-MAR-22')

也使用您的 NLS_DATE_FORMAT。

这就是“小时和分钟丢失”的原因 - 您正在使用忽略它们的格式模型进行隐式转换,因此您最终会在午夜得到最终评估的日期值。

如果您确实想要并且需要使用动态 SQL,那么您应该使用绑定变量:

EXECUTE IMMEDIATE  'SELECT COUNT(DFEC_ANULA) FROM tablab WHERE DFEC_ANULA>:vfecha'
  into pr_Ret
  using vfecha;

但正如 @pmdba 已经说过的,您根本不需要为此使用动态 SQL - 查询以及所有表和列都是已知的在编译时,只有变量值在运行时发生变化;所以只需使用静态 SQL:

SELECT COUNT(DFEC_ANULA) into pr_Ret FROM tablab WHERE DFEC_ANULA > vfecha;

In your current dynamic statement, where you have:

EXECUTE IMMEDIATE  'SELECT ... WHERE DFEC_ANULA>'''||vfecha||'''' into pr_Ret

you are concatenating in the string representation of your date variable. If your NLS_DATE_FORMAT is set to DD-MON-RR (and your date language is English) then for today's date that would evaluate to:

EXECUTE IMMEDIATE  'SELECT ... WHERE DFEC_ANULA>'''|| '02-MAR-22' ||'''' into pr_Ret

which is:

EXECUTE IMMEDIATE  'SELECT ... WHERE DFEC_ANULA>''02-MAR-22''' into pr_Ret

When that statement is parsed you're then relying on implicit conversion again, so:

SELECT ... WHERE DFEC_ANULA>'02-MAR-22'

is

SELECT ... WHERE DFEC_ANULA>TO_DATE('02-MAR-22')

which also then uses your NLS_DATE_FORMAT.

That's why "the hours and minutes are lost" - you are doing implicit conversions using a format model that ignores them, so you end up with final evaluated date value at midnight.

If you really wanted and needed to use dynamic SQL then you should use a bind variable:

EXECUTE IMMEDIATE  'SELECT COUNT(DFEC_ANULA) FROM tablab WHERE DFEC_ANULA>:vfecha'
  into pr_Ret
  using vfecha;

But as @pmdba already said, you don't need to use dynamic SQL for this at all - the query and all of the tables and columns are known at compile time, only a variable value changes at runtime; so just use static SQL:

SELECT COUNT(DFEC_ANULA) into pr_Ret FROM tablab WHERE DFEC_ANULA > vfecha;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文