0

我的代码中有WITH AS子句

DECLARE @MyTable TABLE(Field_1 INT, Field_2 INT)
INSERT @MyTable VALUES
(1,2),
(2,2)

WITH x AS ( SELECT Field_1, Field_2  FROM @MyTable)
SELECT * FROM x 

如何将上述代码的结果插入到同一张表中?我想插入如下:

DECLARE @MyTable TABLE(Field_1 INT, Field_2 INT)
INSERT @MyTable VALUES
(1,2),
(2,2)
WITH x AS ( SELECT Field_1, Field_2  FROM @MyTable)

INSERT INTO @MyTAble (SELECT * FROM x)
SELECT * FROM @MyTable

但它不起作用。Management Studio mark @MyTAble likeInvalid object name @MyTable 我该怎么做?我正在使用 SQL Server 2008 R2。

4

1 回答 1

3

也许你的意思是这个(分号和其他小语法很重要!),

DECLARE @MyTable TABLE(Field_1 INT, Field_2 INT);
------------------------------------------------^

INSERT @MyTable VALUES
(1,2),
(2,2);
-----^    

WITH x AS ( SELECT Field_1, Field_2  FROM @MyTable)
------------------------------------------^
INSERT INTO @MyTAble  SELECT * FROM x ;
---------------------^---------------^^

SELECT * FROM @MyTable;
--------------^-------^

...但我认为这在 MySQL 中不起作用。

于 2012-08-10T12:30:00.757 回答