根据 Redshift WITH Clause 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;
错误:42601:“插入”处或附近有语法错误
有趣的是,它确实支持插入到新表中,即
WITH CTE AS
(SELECT GETDATE() as SomeTimestamp)
INSERT SomeTimestamp INTO NewTable
SELECT SomeTimestamp from CTE;
命令成功完成(受影响 1 行)
编辑:只是为了确认,当使用
INTEGER
列而不是 TIMESTAMP
时,我遇到了同样的错误:
CREATE TABLE TestCTE (SomeInt INTEGER);
WITH CTE AS
(SELECT 1 as SomeInt)
INSERT INTO TestCTEInsert
SELECT SomeInt from CTE;
错误:42601:“插入”处或附近有语法错误
尝试将 CTE 放入插件中(不确定这是否符合要求)
INSERT INTO TestCTEInsert
WITH CTE AS
(SELECT CURRENT_TIMESTAMP as SomeTimestamp)
SELECT SomeTimestamp 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
上面运行在普通的 Postgres 安装上,我无权访问 RDS
将脚本更改为此
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;
当我在 redshift 数据库中执行 SQL 查询时,我在 sql 查询上遇到了相同的错误,我在 Insert into 中添加 with 子句,并运行一个过程,你们如何解决它,或者问题仍然存在。谁能帮帮我吗