此SQL SERVER 2005语句首先删除按FIELD1 ASC
排序的18条记录,然后在克隆表中插入相同的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
- 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
提前感谢您的帮助。
答案 0 :(得分: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
答案 1 :(得分: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