DB2 CTAS 令牌错误
DB2 CTAS Token Error
我正在尝试在 DB2 11.1.0 版中使用 CTAS 语句创建一个新的 table 并将其插入。查询如下:
CREATE TABLE MY_SCRATCH.LC$U7OB81478732948714_zero_to_3 AS (
WITH two AS (SELECT id AS the_num FROM users WHERE id = 2)
, one_two AS (
SELECT id AS the_num FROM users WHERE id = 1
UNION ALL
SELECT * FROM two tmp
)
, zero_one_two AS (
SELECT id-7 AS the_num FROM users where id = 7
UNION ALL
SELECT * FROM one_two tmp
)
SELECT * FROM zero_one_two tmp
UNION ALL
SELECT id AS the_num FROM users WHERE id = 3
) WITH DATA
但是,我收到以下错误:
"my_error":"SQL Error: derived_table zero_to_3 creation failed: SQL Error in CREATE TABLE as SELECT: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=AS;RO_TO_3\" (\nWITH TWO;JOIN, DRIVER=4.16.53"
根据DB2 docs,错误原因如下:
A syntax error was detected where the symbol "token" occurs in the SQL statement. The list of symbols that might be legal shows some alternate symbols that could possibly be correct at that point, if the preceding part of the statement is entirely correct.
所以我在 RazorSQL 中 运行 上面的查询,它抛出了同样的错误。不太确定令牌问题出在哪里
您不能使用常见的 table 表达式。如果您查看 CREATE TABLE
的语法(下图针对您的特定问题进行了删减):
>>-CREATE TABLE--table-name------------------------------------->
>--+-----------------------+--AS--(--fullselect--)-------------->
| .-,-----------. |
| V | |
'-(----column-name-+--)-'
>--+-WITH NO DATA-+---------------------------------------------|
'-WITH DATA----'
一个fullselect is a portion of a full select query, but it doesn't include the common table expression. Common table expressions are part of the select-statement.
也许可以使用嵌套的 table 表达式而不是常见的 table 表达式来重写您的查询,但是您的示例查询很难说明这一点,因为您甚至真的不需要常见的 table 表达式。您的查询可以用更简单的方式编写为:
CREATE TABLE MY_SCRATCH.LC$U7OB81478732948714_zero_to_3 AS (
select id as the_num from users where id = 2
union all
select id as the_num from users where id = 1
union all
select id-7 as the_num from users where id = 7
union all
select id as the_num from users where id = 3
)
WITH DATA;
我正在尝试在 DB2 11.1.0 版中使用 CTAS 语句创建一个新的 table 并将其插入。查询如下:
CREATE TABLE MY_SCRATCH.LC$U7OB81478732948714_zero_to_3 AS (
WITH two AS (SELECT id AS the_num FROM users WHERE id = 2)
, one_two AS (
SELECT id AS the_num FROM users WHERE id = 1
UNION ALL
SELECT * FROM two tmp
)
, zero_one_two AS (
SELECT id-7 AS the_num FROM users where id = 7
UNION ALL
SELECT * FROM one_two tmp
)
SELECT * FROM zero_one_two tmp
UNION ALL
SELECT id AS the_num FROM users WHERE id = 3
) WITH DATA
但是,我收到以下错误:
"my_error":"SQL Error: derived_table zero_to_3 creation failed: SQL Error in CREATE TABLE as SELECT: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=AS;RO_TO_3\" (\nWITH TWO;JOIN, DRIVER=4.16.53"
根据DB2 docs,错误原因如下:
A syntax error was detected where the symbol "token" occurs in the SQL statement. The list of symbols that might be legal shows some alternate symbols that could possibly be correct at that point, if the preceding part of the statement is entirely correct.
所以我在 RazorSQL 中 运行 上面的查询,它抛出了同样的错误。不太确定令牌问题出在哪里
您不能使用常见的 table 表达式。如果您查看 CREATE TABLE
的语法(下图针对您的特定问题进行了删减):
>>-CREATE TABLE--table-name------------------------------------->
>--+-----------------------+--AS--(--fullselect--)-------------->
| .-,-----------. |
| V | |
'-(----column-name-+--)-'
>--+-WITH NO DATA-+---------------------------------------------|
'-WITH DATA----'
一个fullselect is a portion of a full select query, but it doesn't include the common table expression. Common table expressions are part of the select-statement.
也许可以使用嵌套的 table 表达式而不是常见的 table 表达式来重写您的查询,但是您的示例查询很难说明这一点,因为您甚至真的不需要常见的 table 表达式。您的查询可以用更简单的方式编写为:
CREATE TABLE MY_SCRATCH.LC$U7OB81478732948714_zero_to_3 AS (
select id as the_num from users where id = 2
union all
select id as the_num from users where id = 1
union all
select id-7 as the_num from users where id = 7
union all
select id as the_num from users where id = 3
)
WITH DATA;