【发布时间】:2021-10-20 13:45:21
【问题描述】:
美好的一天!
我需要将数据从 SQL Server 2019 导出/导入到运行 PostgreSQL 13.3 的 AWS RDS
只有几张表中的几百行。
这是我第一次遇到 Postgres,所以我决定将数据简单地编写为“INSERT ... SELECT”,就像我使用 SQL Server 一样...我需要的东西太多了。
我为此使用 DBeaver v21,因为我可以轻松访问源数据库和目标数据库。
我测试成功了:
CREATE TABLE public.invoices (
invoiceno int8 NOT NULL GENERATED BY DEFAULT AS IDENTITY,
terminalid int4 NOT NULL,
invoicedate timestamp NOT NULL,
description varchar(100) NOT null
);
INSERT INTO public.invoices(InvoiceNo,TerminalID,InvoiceDate,Description)
SELECT 7 as invoiceno , 5 as terminalid , '2018-10-24 21:29:00' as invoicedate , N'Coffe and cookie' as description
-- Updated Rows 1
-- No problem here
我用 UNION ALL 编写了其余数据的脚本,就像这样(缩短的示例):
INSERT INTO public.invoices(InvoiceNo,TerminalID,InvoiceDate,Description)
SELECT 7 as invoiceno , 5 as terminalid , '2018-10-24 21:29:00' as invoicedate , N'Coffe and cookie' as description
UNION ALL
SELECT 1000, 5 , '2018-10-24 21:29:00' , N'Tea and crumpets'
现在我明白了:
SQL Error [42804]: ERROR: column "invoicedate" is of type timestamp without time zone but expression is of type text
Hint: You will need to rewrite or cast the expression.
Position: 118
我确实在消息中看到它可以通过 CAST 来“修复”(或重写!)....
但是为什么 Postgres 可以隐式转换 1 行,而 2 行却是不可能的呢?
为什么插入超过 1 行时会失败? - 它清楚地知道如何转换文本 -> 日期 ...
我尝试使用 VALUES、CTE、派生表,但没有成功。
因为我必须花更多时间在 postgres 上——我真的很想了解这里发生了什么。我的语法是否错误(SQL Server 可以正常工作),DBeaver 是否将我的数据弄乱了,等等...?
任何建议将不胜感激。 谢谢
【问题讨论】:
标签: postgresql import dbeaver