Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select and update in one query?

I need to return a row:

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

but at the same time I need to update it:

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

and need all data of this row

It is possible in the same query?

like image 727
e-info128 Avatar asked Jul 19 '13 15:07

e-info128


1 Answers

Resolve this!

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

like image 128
e-info128 Avatar answered Sep 30 '22 20:09

e-info128