在 SQL 服务器中使用 CTE 插入查询
Insert query using CTE in SQL Server
我想使用 CTE 从另一个 table 插入一个 table。我试过在 with
前加分号,但没用。
这是我的查询:
INSERT INTO [autoFIE2].[dbo].[tbl_article_type_parent_child] ([art_typ_parent_index], [art_typ_child_index])
WITH article_type_list AS
(
SELECT
art_typ_child_index, art_typ_parent_index
FROM
[autoFIE2].[dbo].[tbl_article_type_parent_child]
WHERE
art_typ_parent_index IS NULL
UNION ALL
SELECT
a.art_typ_child_index, a.art_typ_parent_index
FROM
[autoFIE2].[dbo].[tbl_article_type_parent_child] A
INNER JOIN
article_type_list as AL ON a.art_typ_parent_index = al.art_typ_child_index
WHERE
a.art_typ_parent_index IS NOT NULL)
SELECT *
FROM article_type_list;
执行此语句时出错:-
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'with'.
Msg 319, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking
context clause, the previous statement must be terminated with a
semicolon.
我应该怎么做才能将此分层数据插入另一个 table。有什么建议吗?
首先声明 cte
,然后从 select 列表中插入 cte:
;WITH article_type_list AS
(
SELECT
art_typ_child_index, art_typ_parent_index
FROM
[autoFIE2].[dbo].[tbl_article_type_parent_child]
WHERE
art_typ_parent_index IS NULL
UNION ALL
SELECT
a.art_typ_child_index, a.art_typ_parent_index
FROM
[autoFIE2].[dbo].[tbl_article_type_parent_child] A
INNER JOIN
article_type_list as AL ON a.art_typ_parent_index = al.art_typ_child_index
WHERE
a.art_typ_parent_index IS NOT NULL
)
INSERT INTO [autoFIE2].[dbo].[tbl_article_type_parent_child]
([art_typ_parent_index], [art_typ_child_index])
SELECT * FROM article_type_list;
我想使用 CTE 从另一个 table 插入一个 table。我试过在 with
前加分号,但没用。
这是我的查询:
INSERT INTO [autoFIE2].[dbo].[tbl_article_type_parent_child] ([art_typ_parent_index], [art_typ_child_index])
WITH article_type_list AS
(
SELECT
art_typ_child_index, art_typ_parent_index
FROM
[autoFIE2].[dbo].[tbl_article_type_parent_child]
WHERE
art_typ_parent_index IS NULL
UNION ALL
SELECT
a.art_typ_child_index, a.art_typ_parent_index
FROM
[autoFIE2].[dbo].[tbl_article_type_parent_child] A
INNER JOIN
article_type_list as AL ON a.art_typ_parent_index = al.art_typ_child_index
WHERE
a.art_typ_parent_index IS NOT NULL)
SELECT *
FROM article_type_list;
执行此语句时出错:-
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'with'.Msg 319, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
我应该怎么做才能将此分层数据插入另一个 table。有什么建议吗?
首先声明 cte
,然后从 select 列表中插入 cte:
;WITH article_type_list AS
(
SELECT
art_typ_child_index, art_typ_parent_index
FROM
[autoFIE2].[dbo].[tbl_article_type_parent_child]
WHERE
art_typ_parent_index IS NULL
UNION ALL
SELECT
a.art_typ_child_index, a.art_typ_parent_index
FROM
[autoFIE2].[dbo].[tbl_article_type_parent_child] A
INNER JOIN
article_type_list as AL ON a.art_typ_parent_index = al.art_typ_child_index
WHERE
a.art_typ_parent_index IS NOT NULL
)
INSERT INTO [autoFIE2].[dbo].[tbl_article_type_parent_child]
([art_typ_parent_index], [art_typ_child_index])
SELECT * FROM article_type_list;