开发者

Select and update in single query

开发者 https://www.devze.com 2023-01-08 09:27 出处:网络
i have a contact us table containing name emailid phoneno message repliedmessage as fields, by default the replied message field is null after replying to a particular message i am updating that field

i have a contact us table containing name emailid phoneno message repliedmessage as fields, by default the replied message field is null after replying to a particular message i am updating that field开发者_如何转开发 but at the same time i also want to retrieve the other values also like name and emailid using select statements


Are you saying you are updating multiple rows or only one?

If you're updating multiple rows, you could select the affected row IDs into a temp table, perform the update, then return a join of the tmp table's IDs on the updated table.

If you're updating a single row, just perform a select on that row after the update.

It is bad for maintainability to try two operations at the same time, unless the logic of the situation makes it clear, later on, that it is required.

You could always have a simple LastUpdated DATETIME field on the table. Then you just need a variable which you set to getdate() and use during the update. After the update simply return every row which where LastUpdated matches the datetime variable.

Eg:

declare @opTime datetime
set @opTime = getdate()

update .... (...., LastUpdated) values (....., @opTime)

select * from ... where LastUpdated = @opTime
0

精彩评论

暂无评论...
验证码 换一张
取 消