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

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

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

  1. CREATE TABLE IF NOT EXISTS shipments_swp
  2. (
  3. shipment_id bigint DISTKEY ENCODE RAW,
  4. user_id bigint ENCODE ZSTD,
  5. last_tracking_event_time timestamp encode zstd,
  6. bd_updated_at timestamp default sysdate ENCODE ZSTD
  7. );
  8. Insert into shipments_swp
  9. (
  10. select *
  11. from common.shipments_dim
  12. );

错误消息如图所示

  1. 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之前,可能需要将其转换为时间戳。我尝试了一个简单的测试,看看插入正确工作,我没有问题插入到它:

  1. CREATE TABLE IF NOT EXISTS test_table_a (
  2. ts_a TIMESTAMP DEFAULT SYSDATE ENCODE ZSTD
  3. );
  4. -- Insert a few different date formats
  5. insert into test_table_a values('2020/05/22');
  6. insert into test_table_a values('2020-05-22');
  7. insert into test_table_a values('2020-05-22 16:12:27.830728');
  8. insert into test_table_a values('2020-05-22 16:06:00');
  9. --- Create a second table and write to that
  10. --- to mimic the insert behavior from the question
  11. CREATE TABLE IF NOT EXISTS test_table_b (
  12. ts_b TIMESTAMP DEFAULT SYSDATE ENCODE ZSTD
  13. );
  14. INSERT INTO test_table_b (
  15. SELECT *
  16. FROM test_table_a
  17. );
  18. --- This insert succeeds
  19. SELECT * FROM test_table_b;
  20. 2020-05-22 16:06:00
  21. 2020-05-22 00:00:00
  22. 2020-05-22 16:06:56.823844
  23. 2020-05-22 00:00:00
展开查看全部

相关问题