在SQL Server中的单个查询中更新并选择列 [英] Update and select the column in a single query in sql server

查看:99
本文介绍了在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中的单个查询中更新并选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆