sql—使用sysdate创建默认列,但在插入数据时出现字符变化错误

qyswt5oh  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(407)

我使用默认的sysdate列在redshift中创建一个表,但是当我插入数据时,我得到一个奇怪的表达式,它的类型是字符变化错误。这是我第一次遇到这个错误。

CREATE TABLE IF NOT EXISTS shipments_swp
(
shipment_id                      bigint DISTKEY ENCODE RAW,
user_id                          bigint ENCODE ZSTD,
last_tracking_event_time         timestamp encode zstd,
bd_updated_at                    timestamp default sysdate ENCODE ZSTD
);

Insert into shipments_swp
(
select *
from common.shipments_dim
);

错误消息如图所示

SQL Error [500310] [42804]: [Amazon](500310) Invalid operation: column "bd_updated_at" is of type timestamp without time zone but expression is of type character varying;
ibps3vxo

ibps3vxo1#

你能给出一个你想插入到那个列中的时间戳的例子吗?在将该列写入swp之前,可能需要将其转换为时间戳。我尝试了一个简单的测试,看看插入正确工作,我没有问题插入到它:

CREATE TABLE IF NOT EXISTS test_table_a (
    ts_a    TIMESTAMP DEFAULT SYSDATE ENCODE ZSTD
);

-- Insert a few different date formats
insert into test_table_a values('2020/05/22');
insert into test_table_a values('2020-05-22');
insert into test_table_a values('2020-05-22 16:12:27.830728');
insert into test_table_a values('2020-05-22 16:06:00');

--- Create a second table and write to that 
--- to mimic the insert behavior from the question

CREATE TABLE IF NOT EXISTS test_table_b (
    ts_b    TIMESTAMP DEFAULT SYSDATE ENCODE ZSTD
);

INSERT INTO test_table_b (
    SELECT *
    FROM test_table_a
);

--- This insert succeeds
SELECT * FROM test_table_b;

2020-05-22 16:06:00
2020-05-22 00:00:00
2020-05-22 16:06:56.823844
2020-05-22 00:00:00

相关问题