sql - 在 SQL Server 中使用已删除记录中的值

标签 sql sql-server sql-server-2012

我需要删除记录并在另一个查询中按其 ID 进行搜索,如下所示

select * from Flat
WHERE  Flat.nu in (delete top (1) from temp output deleted.nu)

最佳答案

我认为你不能同时运行这些。

我建议您使用(临时表):tempIds(id int)。 然后使用 output 子句,如下所示:

delete top(1) from temp
output deleted.id into tempIds;

注意:最好使用CTE删除第一行。

然后查询 tempIds 表,然后清除 tempIds

关于sql - 在 SQL Server 中使用已删除记录中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41414392/

相关文章:

sql - 逗号连接列的 tSQL UNPIVOT 成多行

sql - 为什么不能在 Slick 中使用已编译的 Insert 语句

java - JdbcTemplate如何自动生成主键

php - [MySQL] : Get product row - if doesn't exist by brand & product_code - get only by brand where product_code is empty

c# - 下载并执行 SQL 脚本——有哪些安全问题+解决方案?

sql-server - 重置 SQL Server 索引使用情况

c# - 我怎样才能只使用 SQL 来更新这个 [复杂] 表?

sql - 向已存在的子查询引入简单计数子查询

sql-server - 估计数据库大小

sql - 使用 "Execute sp_Executesql"时的权限