将日期列添加到 SQL 中的临时 table

Add a date column to a temporary table in SQL

我正在 Netezza 中使用临时 table,其中包含列 idgenderstart_dateend_date。我正在尝试向此 table 添加一个新列,其中包含所有行的默认日期 2019-01-01。我要添加此列的 table 是本地临时文件 table,因此 ALTER TABLE 不起作用 ("Error: Operation not allowed on a temp table")。为了解决这个问题,我创建了一个新的临时文件 table,其中包含一个适当命名的空列,但现在我发现我无法使用 UPDATE/SET 来用我的日期 ("Error: Value too large for column") 填充它。更改我的温度 table 以包含一个包含我的日期的新列的最佳方法是什么?如果可能的话,我想直接修改旧的 temp table 而不是创建一个新的。注意:我在我正在使用的数据库中没有必要的管理员权限,只能创建一个永久的 table 来代替 ALTER。

成功创建具有空列的新临时 table 的代码是:

DROP TABLE new_temp_table IF EXISTS;
GO
SELECT id, gender, start_date, end_date, NULL default_date
INTO TEMP TABLE new_temp_table
FROM old_temp_table;
GO

无法替换新列中的 NULL 值的代码是:

UPDATE new_temp_table
SET default_date = '2019-01-01';
GO

NULL 转换为您想要的类型:

SELECT id, gender, start_date, end_date,
       CAST(NULL as DATE) as default_date
INTO TEMP TABLE new_temp_table
FROM old_temp_table;

Netezza不知道是什么类型,所以猜测一下。而且这个猜测不太可能是约会(我认为是int)。