3

此 SQL SERVER 2005 语句首先删除 18 条记录FIELD1 ASC,然后将相同的 18 条记录插入到克隆表中:

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

请帮我将前面的语句转换为 first INSERT,并且THEN-IF INSERT已确定 - 删除相同的 18 条记录;我刚刚尝试过,但以下代码给出了错误,

Msg 102, Level 15, State 1, Line 7 Incorrect syntax near 'INSERTED'.

WITH  q AS
        (
        SELECT TOP 18 *
        FROM    pippo
        ORDER BY ID_OL ASC /* You may want to add ORDER BY here */
        )
insert into tablenew select * from q OUTPUT INSERTED.* INTO TABLENEW

预先感谢您的帮助。

4

2 回答 2

2
declare @some_temp_table table (ID_OL int)

WITH  q AS
        (
        SELECT TOP 18 *
        FROM    pippo
        ORDER BY ID_OL ASC /* You may want to add ORDER BY here */
        )
insert into minnie
OUTPUT INSERTED.ID_OL INTO @some_temp_table
select * from q

delete from pippo where ID_OL in (select ID_OL from @some_temp_table)

另一个版本

set xact_abort on

declare @filter table (ID_OL int primary key)

insert into @filter (ID_OL)
SELECT TOP 18 ID_OL
FROM pippo
ORDER BY ID_OL ASC

begin transaction

insert into minnie 
select * from pippo where ID_OL in (select ID_OL from @filter)

delete from pippo where ID_OL in (select ID_OL from @filter)

commit transaction
于 2012-10-17T13:13:58.757 回答
1
insert into temptable
SELECT TOP 18 * FROM  pippo ORDER BY FIELD1 ASC

if(@@ROWCOUNT = 18)
BEGIN
WITH  q AS
        (
        SELECT TOP 18 *
        FROM    pippo
        ORDER BY ID_OL ASC /* You may want to add ORDER BY here */
        )

delete from q 
END
于 2012-10-17T13:21:35.020 回答