我的代码中有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标记为@MyTAbl
,例如Invalid object name @MyTable
我该怎么做?我正在使用SQL Server 2008 R2。
答案 0 :(得分: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。