如何将默认时间戳变量声明为当前时间戳不起作用

问题描述 投票:0回答:2

在我的存储过程中,我想将当前时间设置为默认时间戳。 但是我在编译错误中运行,但是加上

set
,它会起作用。

DECLARE VSTART TIMESTAMP DEFAULT CURRENT TIMESTAMP;

我有这个例外:

  [Code: -104, SQL State: 42601]  An unexpected token "CURRENT TIMESTAMP" was found following "ME TIMESTAMP DEFAULT".  Expected tokens may include:  "<literal>".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.28.11
db2-luw
2个回答
0
投票

您可以在存储过程中使用以下替代方法:

-- Declare a variable
DECLARE VSTART TIMESTAMP;

-- Set the variable's value to the current timestamp
SET VSTART = CURRENT TIMESTAMP;

-- Sample compound statement
BEGIN
  -- Your code here
END;

0
投票

请使用正确的 Db2 平台标签(db2-zos、db2-400、db2-luw)标记您的问题——有时答案取决于此。

如果您的 Db2 服务器是 Db2-LUW (Linux/Unix/Windows),那么您需要两个单独的语句,因为 文档 指定变量声明语法允许常量值的默认值,或 null。因此,一个语句声明变量,另一个语句为该变量分配一个不同的值。

对于两种形式的复合 sql(编译和内联)都是如此。

您显示的语法

...TIMESTAMP with DEFAULT CURRENT TIMESTAMP
在 DDL 中有效,但在复合 SQL 中无效。

© www.soinside.com 2019 - 2024. All rights reserved.