gpt4 book ai didi

sql - 首先插入,然后 - 如果可以 - 删除

转载 作者:行者123 更新时间:2023-12-04 23:43:39 24 4
gpt4 key购买 nike

此 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条记录;我刚试过,但下面的代码给出了错误,

消息 102,级别 15,状态 1,第 7 行“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

预先感谢您的帮助。

最佳答案

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

关于sql - 首先插入,然后 - 如果可以 - 删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12935103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com