插入 … SELECT :缺少 SELECT 关键字

发布于 2025-01-14 01:40:23 字数 1582 浏览 5 评论 0原文

我正在尝试使用递归 CTE 将字符串拆分为多个值。然后数据被插入到另一个表中。

以下内容在 PostgreSQL 中有效:

CREATE TABLE test(data varchar(255));

WITH RECURSIVE
    cte(genres) AS (SELECT 'apple,banana,cherry,date'),
    split(genre,rest,genres) AS (
        SELECT '', genres||',',genres FROM cte
        UNION ALL
        SELECT
            substring(rest,0,position(',' IN rest)),
            substring(rest,position(',' IN rest)+1),
            genres
        FROM split WHERE rest<>''
    )
INSERT INTO test(data)
SELECT genre
FROM split;

但是,Oracle 版本:

CREATE TABLE test(data varchar(255));
WITH
    cte(genres) AS (SELECT 'apple,banana,cherry,date' FROM dual),
    split(genre,rest,genres) AS (
        SELECT '', genres||',',genres FROM cte
        UNION ALL
        SELECT
            substr(rest,1,instr(rest,',')-1), 
            substr(rest,instr(rest,',')+1),
            genres
        FROM split WHERE rest IS NOT NULL
    )
INSERT INTO test(data)
SELECT genre
FROM split WHERE genre IS NOT NULL;

给了我错误消息:

ORA-00928: 缺少 SELECT 关键字。

现在我很确定我已经得到了一个,位于 INSERT 和下面的 FROM 之间。

如果您注释掉 INSERT,其余部分将给出结果。在其他地方,我知道简单的 INSERT … SELECT 确实有效。

这与递归 CTE 有关吗?如何使用标准递归 CTE 使其正常工作?

在 Docker 映像中使用 Oracle 18c。这里有一个小提琴: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=d24f3105634933af1b7072bded1dacdf

I am experimenting with a recursive CTE to split a string into multiple values. The data is then inserted into another table.

The following works in PostgreSQL:

CREATE TABLE test(data varchar(255));

WITH RECURSIVE
    cte(genres) AS (SELECT 'apple,banana,cherry,date'),
    split(genre,rest,genres) AS (
        SELECT '', genres||',',genres FROM cte
        UNION ALL
        SELECT
            substring(rest,0,position(',' IN rest)),
            substring(rest,position(',' IN rest)+1),
            genres
        FROM split WHERE rest<>''
    )
INSERT INTO test(data)
SELECT genre
FROM split;

However, the Oracle version:

CREATE TABLE test(data varchar(255));
WITH
    cte(genres) AS (SELECT 'apple,banana,cherry,date' FROM dual),
    split(genre,rest,genres) AS (
        SELECT '', genres||',',genres FROM cte
        UNION ALL
        SELECT
            substr(rest,1,instr(rest,',')-1), 
            substr(rest,instr(rest,',')+1),
            genres
        FROM split WHERE rest IS NOT NULL
    )
INSERT INTO test(data)
SELECT genre
FROM split WHERE genre IS NOT NULL;

gives me the error message:

ORA-00928: missing SELECT keyword.

Now I’m pretty sure that I’ve got one, there between the INSERT and the following FROM.

If you comment out the INSERT, the rest of it will give the results. Elsewhere, I know that a simple INSERT … SELECT does work.

Is it something to do with the recursive CTE? How can I get this to work properly using a standard recursive CTE?

Using Oracle 18c in a Docker image. There is a fiddle here: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=d24f3105634933af1b7072bded1dacdf .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2025-01-21 01:40:23

INSERT-SELECT 表示首先是“INSERT”命令,然后是 SELECT 部分,WITH 是 SELECT 的一部分,而不是插入。

SQL> create table t ( x int );

Table created.

SQL> with blah as ( select 1 c from dual)
  2  insert into t
  3  select * from blah;
insert into t
*
ERROR at line 2:
ORA-00928: missing SELECT keyword


SQL> insert into t
  2  with blah as ( select 1 c from dual)
  3  select * from blah;

1 row created.

An INSERT-SELECT means "INSERT" command first, then the SELECT part, and the WITH is part of the SELECT not the insert.

SQL> create table t ( x int );

Table created.

SQL> with blah as ( select 1 c from dual)
  2  insert into t
  3  select * from blah;
insert into t
*
ERROR at line 2:
ORA-00928: missing SELECT keyword


SQL> insert into t
  2  with blah as ( select 1 c from dual)
  3  select * from blah;

1 row created.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文