在SQL Server中的单个查询中更新并选择列

编程入门 行业动态 更新时间:2024-10-26 18:29:05
本文介绍了在SQL Server中的单个查询中更新并选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 更新表后,我需要查询以从表中选择列.我的意思是,在单个查询中,我需要更新以及选择列. 例如:

Hi everyone, I need the query to select the columns from the table as soon as I update the table. I mean, In a single query, I need to update as well as select the column. Ex:

Update table1 set status=''Accepted'' where EId=1

在这里,我正在更新表,但是我需要在同一查询中选择列. 请任何人在这方面帮助我... 问候, Raj

Here, I am updating the table, but I need to select the columns in a same query. Plz anyone assist me on this... Regards, Raj

推荐答案

如果您的意思是需要有关受UPDATE 语句影响的更新行的信息,则可以使用OUTPUT子句 [ ^ ] If you mean you need information about the updated rows affected by the UPDATE statement, you can use OUTPUT clause[^]

您可以读取变量并通过将变量设置为列的值来进行更新.但是,这只会在更新之前读取当前值. 声明@status varchar(55) 更新tbl 设置@status =状态,状态=``已接受'' 来自table1 tbl 其中EId = 1 选择@status-返回以前的值 要返回新值,只需将变量设置为新值并对所有列重复. 更新tbl 设置@status =``已接受'',状态=``已接受'' 来自table1 tbl 其中EId = 1 select @status-返回新值 但是,如果您使用的是ORM,实体映射,则此方法不起作用.如果是这样,则需要在第二个查询中从表中读取 更新表格... 从表中选择. You can read columns and update at the same time by setting variables to the values of the columns. This however only reads the current values before the update. declare @status varchar(55) update tbl set @status = status, status = ''Accepted'' from table1 tbl where EId =1 select @status --returns the previous values To return the new values just set the variable to the new value and repeat for all the columns. update tbl set @status = ''Accepted'', status = ''Accepted'' from table1 tbl where EId =1 select @status --returns the new value This however does not work if you are using ORM, Entity mapping. If you are, you will need to read from the table in a second query update table... select from table..

存储过程的听说过? 此处: 创建过程 [ ^ ] SQL存储过程 [ ^ ] 存储过程:概述 [ ^ ] Heard of Stored Procedure? Here: CREATE Procedure[^] SQL Stored Procedures[^] Stored Procedures: An Overview[^]

更多推荐

在SQL Server中的单个查询中更新并选择列

本文发布于:2023-10-14 11:47:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1490966.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SQL   Server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!