首页 > 解决方案 > 使用 CREATE TABLE 在 SQLite 中创建表时出错作为选择

问题描述

我正在尝试创建一个为特定日期范围生成 EOM 日期的日期表,但 SQLite 在创建表时抛出错误。我目前的代码如下:

WITH RECURSIVE cnt (
        x
) AS (
    SELECT
            0
    UNION ALL
    SELECT
            x + 1
    FROM
            cnt
    LIMIT (
            SELECT
                    ROUND(((julianday ('2020-05-01') - julianday ('2016-04-01')) / 30) + 1))
)

CREATE TABLE report_dates AS
SELECT
  date(date(julianday ('2016-04-01'), '+' || x || ' month'), '-1 day') AS month
FROM
  cnt;

但是,我收到错误消息Query 1 ERROR: near "CREATE": syntax error。删除CREATE TABLE report_dates AS线确实使结果没有问题。是什么驱动了这一切?

标签: sqlsqliteselectcreate-tablerecursive-query

解决方案


cte 跟在create table:

CREATE TABLE report_dates AS
WITH RECURSIVE cnt (x) AS (
    SELECT 0
    UNION ALL
    SELECT x + 1
    FROM cnt
    LIMIT (
        SELECT ROUND(((julianday ('2020-05-01') - julianday ('2016-04-01')) / 30) + 1)
    )
)
SELECT date(date(julianday ('2016-04-01'), '+' || x || ' month'), '-1 day') AS month
FROM cnt;

推荐阅读