再會!
我需要將資料從 SQL Server 2019 匯出/匯入到運行 PostgreSQL 13.3 的 AWS RDS
它只是來自少數幾張桌子的幾百行。
這是我第一次遇到 Postgres,所以我決定像使用 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 是否弄亂了我的資料等...?
任何建議,將不勝感激。謝謝
uj5u.com熱心網友回復:
'2018-10-24 21:29:00' 是一個字串值,Postgres 對正確的資料型別比 SQL Server 更挑剔。
您需要將該值指定為適當的時間戳常量,
timestamp '2018-10-24 21:29:00'
請注意,您可以使用values子句以更緊湊的形式撰寫它:
INSERT INTO public.invoices(InvoiceNo,TerminalID,InvoiceDate,Description)
values
(7, 5, timestamp '2018-10-24 21:29:00', 'Coffe and cookie'),
(1000, 5 , timestamp '2018-10-24 21:29:00' , 'Tea and crumpets');
uj5u.com熱心網友回復:
這種行為的原因是按編譯順序排列的。在首先使用 VIEW 的情況下,視圖中的編譯查詢和視圖中的列型別(名稱)取自“視圖”的第一部分(第一個 SELECT 命令)。因此,您獲得的是文本而不是時間戳,并且它與插入的表型別不匹配。
MSSQL 編譯器更聰明一點:-)。
在第一個示例中,您有簡單的 INSERT INTO ... SELECT .... 和編譯器立即期望時間戳型別 - 因此,它不會引發任何編譯錯誤(但是當資料不通過自動轉換規則時,執行時間可能會發生錯誤)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/328562.html
標籤:PostgreSQL 进口 海狸
上一篇:是否可以在Postgres的左連接上執行“LIMIT1”?
下一篇:對PostgreSQL性能的理解
