SQL 如何将今天的日期返回到 Oracle 中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4925144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:14:36  来源:igfitidea点击:

How to return today's date to a variable in Oracle

sqloraclesql-server-2008

提问by William

I want to do this:

我想做这个:

DECLARE @today as smalldatetime
SELECT @today = GetDate()

But i need an oracle translation

但我需要一个 oracle 翻译

回答by OMG Ponies

Oracle uses SYSDATE, and there's the ANSI standard CURRENT_TIMESTAMP(supported by both SQL Server and Oracle, besides others) to get the current date & time.

Oracle 使用SYSDATE, 并且有 ANSI 标准CURRENT_TIMESTAMP(SQL Server 和 Oracle 都支持,除此之外)来获取当前日期和时间。

v_today DATE;

SELECT SYSDATE
  INTO v_today
  FROM DUAL;

...would be the equivalent to the TSQL you posted. Oracle uses the INTOclause for populating variables, where the variable data type must match the column position in the SELECT clause.

...将等同于您发布的 TSQL。Oracle 使用INTO子句填充变量,其中变量数据类型必须与 SELECT 子句中的列位置匹配。

回答by Shannon Severance

While not a strict translation, I prefer the following construction in Oracle:

虽然不是严格的翻译,但我更喜欢 Oracle 中的以下结构:

v_today date; -- needs to go where variables are declared

v_today := sysdate; -- used where code is run.

Or even:

甚至:

v_today date := sysdate;