3

このSQLSERVER2005ステートメントは、最初に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

前のステートメントを最初に変換するのを手伝ってくださいINSERT。そして、THEN-HASBEENOK-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 に答える