通过选择查询将日期和时间保存在变量中
朋友,
我有一个存储过程,我需要根据另一个表的日期和时间验证日期和时间。
如何将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要使用
立即执行
?为什么不这样做:或者将所有内容合并到一个查询中:
Why are you using
EXECUTE IMMEDIATE
? Why not this:or combine everything into one query:
在当前的动态语句中,您有:
您正在连接日期变量的字符串表示形式。如果您的 NLS_DATE_FORMAT 设置为 DD-MON-RR(并且您的日期语言为英语),那么对于今天的日期,其计算结果为:
解析
该语句时,您将依赖于隐式再次转换,所以:
然后
也使用您的 NLS_DATE_FORMAT。
这就是“小时和分钟丢失”的原因 - 您正在使用忽略它们的格式模型进行隐式转换,因此您最终会在午夜得到最终评估的日期值。
如果您确实想要并且需要使用动态 SQL,那么您应该使用绑定变量:
但正如 @pmdba 已经说过的,您根本不需要为此使用动态 SQL - 查询以及所有表和列都是已知的在编译时,只有变量值在运行时发生变化;所以只需使用静态 SQL:
In your current dynamic statement, where you have:
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:which is:
When that statement is parsed you're then relying on implicit conversion again, so:
is
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:
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: