3

如何从“pippo”表中删除前 18 行(按 FIELD1 排序),然后在另一个名为 minnie 的克隆表中插入相同的 18 行?

以下代码提取按 FIELD1 排序的前 18 行,然后删除它们:

WITH  q AS
        (
        SELECT TOP 18 *
        FROM    pippo
        ORDER BY FIELD1 ASC /* You may want to add ORDER BY here */
        )
DELETE
FROM    q

在删除它们之前,如何将相同的 18 行插入到 minnie 表中?

预先感谢您的帮助。

4

2 回答 2

6

采用

WITH  q AS
        (
        SELECT TOP 18 *
        FROM    pippo
        ORDER BY FIELD1 ASC /* You may want to add ORDER BY here */
        )
DELETE
FROM    q
OUTPUT DELETED.* INTO TableNew
于 2012-10-17T10:24:43.683 回答
1

如果你愿意,你可以嵌套delete一个select查询insert

declare @t1 table (Field1 int not null)
declare @t2 table (Field1 int not null)

insert into @t1 (Field1) select ROW_NUMBER() OVER (ORDER BY object_id) from sys.objects

;with First18 as (
    select top 18 * from @t1 order by Field1
)
insert into @t2 (Field1)
select * from 
    (
    delete from First18 output deleted.Field1) t
于 2012-10-17T10:30:15.937 回答