6

我需要返回一行:

.NET[tableReturn] = select top(1) * from [table] where x = 0 order by desc

但同时我需要更新它:

update [table] set x = 1 where [id] = .NET[tableReturn].[id]

并且需要这一行的所有数据

可以在同一个查询中吗?

4

4 回答 4

9

解决这个!

DECLARE @id int;
SET @id = (select top(1) id from [table] where [x] = 0 order by id desc);

select * from [table] where id = @id;
update [table] set [x] = 20 where id = @id;

:D

于 2013-07-19T15:59:19.427 回答
1

尝试这个

with cte as (select top(1) * from [table] where x=0 order by 1 desc)
update [table] set x=1 from cte join [table] c on c.id =cte.id;
于 2013-07-20T12:57:49.743 回答
0

使用存储过程。在存储过程中,您可以根据需要执行任意数量的查询。
没有它,您将不得不独立调用多个查询。

希望这可以帮助。

于 2013-07-19T16:06:48.477 回答
0

尝试以下查询以使用单个查询更新列。

update [table] set x = 1 where x = (select cte.x from (select top(1) * from [table] where x = 0 order by x desc) cte)

注意:-但更新的列不应该是标识列

于 2013-07-19T16:09:35.247 回答