来自 CTE 的 Redshift INSERT INTO TABLE
Redshift INSERT INTO TABLE from CTE
根据 Redshift WITH 子句 documentation,您可以将 WITH 子句与 INSERT INTO...SELECT
语句一起使用。但是,在对此进行测试时,出现以下错误。这是不可能的,还是我的语法有误?
CREATE TABLE TestCTEInsert (SomeTimestamp TIMESTAMP);
WITH CTE AS
(SELECT GETDATE() as SomeTimestamp)
INSERT INTO TestCTEInsert
(SomeTimestamp) SELECT SomeTimestamp from CTE;
ERROR: 42601: syntax error at or near "insert"
有趣的是,它确实支持插入到新的 table 即
WITH CTE AS
(SELECT GETDATE() as SomeTimestamp)
INSERT SomeTimestamp INTO NewTable
SELECT SomeTimestamp from CTE;
The command completed successfully (1 rows affected)
编辑: 只是为了确认,我在使用 INTEGER
列而不是 TIMESTAMP
时遇到同样的错误:
CREATE TABLE TestCTE (SomeInt INTEGER);
WITH CTE AS
(SELECT 1 as SomeInt)
INSERT INTO TestCTEInsert
SELECT SomeInt from CTE;
ERROR: 42601: syntax error at or near "insert"
尝试将 CTE 放入插入物中(不确定是否达到目的)
INSERT INTO TestCTEInsert
WITH CTE AS
(SELECT CURRENT_TIMESTAMP as SomeTimestamp)
SELECT SomeTimestamp from CTE;
将您的脚本更改为此
CREATE TABLE TestCTE (SomeInt INTEGER)
WITH CTE AS (SELECT 1 as SomeInt)
INSERT INTO TestCTE SELECT SomeInt from CTE;
试试这个
CREATE TABLE TestCTE (SomeInt INTEGER)
;WITH CTE AS
(SELECT 1 as SomeInt)
INSERT (SomeInt) INTO TestCTE
SELECT SomeInt FROM CTE;
;
终止了一个语句,所以它需要放在语句的末尾,而不是中间的某个地方:
您可以通过两种方式做到这一点,要么使用 create table as select
create table TestCTEInsert
as
WITH CTE AS
(
SELECT current_timestamp as SomeTimestamp
)
SELECT SomeTimestamp
from CTE; -- ; only at the end
或分两步:
CREATE TABLE TestCTEInsert (SomeTimestamp TIMESTAMP); -- end this with a ;
insert into TestCTEInsert
WITH CTE AS
(
SELECT current_timestamp as SomeTimestamp
)
SELECT SomeTimestamp
from CTE; -- ; only at the end
以上运行在 vanilla Postgres 安装上,我无权访问 RDS
根据 Redshift WITH 子句 documentation,您可以将 WITH 子句与 INSERT INTO...SELECT
语句一起使用。但是,在对此进行测试时,出现以下错误。这是不可能的,还是我的语法有误?
CREATE TABLE TestCTEInsert (SomeTimestamp TIMESTAMP);
WITH CTE AS
(SELECT GETDATE() as SomeTimestamp)
INSERT INTO TestCTEInsert
(SomeTimestamp) SELECT SomeTimestamp from CTE;
ERROR: 42601: syntax error at or near "insert"
有趣的是,它确实支持插入到新的 table 即
WITH CTE AS
(SELECT GETDATE() as SomeTimestamp)
INSERT SomeTimestamp INTO NewTable
SELECT SomeTimestamp from CTE;
The command completed successfully (1 rows affected)
编辑: 只是为了确认,我在使用 INTEGER
列而不是 TIMESTAMP
时遇到同样的错误:
CREATE TABLE TestCTE (SomeInt INTEGER);
WITH CTE AS
(SELECT 1 as SomeInt)
INSERT INTO TestCTEInsert
SELECT SomeInt from CTE;
ERROR: 42601: syntax error at or near "insert"
尝试将 CTE 放入插入物中(不确定是否达到目的)
INSERT INTO TestCTEInsert
WITH CTE AS
(SELECT CURRENT_TIMESTAMP as SomeTimestamp)
SELECT SomeTimestamp from CTE;
将您的脚本更改为此
CREATE TABLE TestCTE (SomeInt INTEGER)
WITH CTE AS (SELECT 1 as SomeInt)
INSERT INTO TestCTE SELECT SomeInt from CTE;
试试这个
CREATE TABLE TestCTE (SomeInt INTEGER)
;WITH CTE AS
(SELECT 1 as SomeInt)
INSERT (SomeInt) INTO TestCTE
SELECT SomeInt FROM CTE;
;
终止了一个语句,所以它需要放在语句的末尾,而不是中间的某个地方:
您可以通过两种方式做到这一点,要么使用 create table as select
create table TestCTEInsert
as
WITH CTE AS
(
SELECT current_timestamp as SomeTimestamp
)
SELECT SomeTimestamp
from CTE; -- ; only at the end
或分两步:
CREATE TABLE TestCTEInsert (SomeTimestamp TIMESTAMP); -- end this with a ;
insert into TestCTEInsert
WITH CTE AS
(
SELECT current_timestamp as SomeTimestamp
)
SELECT SomeTimestamp
from CTE; -- ; only at the end
以上运行在 vanilla Postgres 安装上,我无权访问 RDS