2更新语句到输出子句

时间:2016-07-29 21:01:37

标签: sql-server sql-server-2008

UPDATE b
SET  b.update_flag=0 
from table1 a
inner join table2 b on a.user_code=b.user_code 
where a.pin_number IS NULL

UPDATE a
SET a.pin_number = Adinfo.dbo.udf_ad_Encrypt(b.pin_number)
from table1 a
inner join table2 b on a.user_code=b.user_code 
where a.pin_number IS NULL 

有人可以帮助我如何在sql server

中使用output子句编写它

1 个答案:

答案 0 :(得分:-1)

我看到问题所在:I should use output keyword and get the values from the second update statement and get those values inserted in first update statement. It can be done by using output clause. – Durga

您只需更改更新语句的位置即可 首先,您更新表格“b”中的pin_number,然后在表格“a”中将所有update_flag设置为“1”。

然后根本不需要输出。只有一个限制:你必须把它包装在事务中。

BEGIN TRANSACTION 

UPDATE a
SET a.pin_number = Adinfo.dbo.udf_ad_Encrypt(b.pin_number)
from table1 a
inner join table2 b on a.user_code=b.user_code 
where a.pin_number IS NULL

UPDATE b
SET  b.update_flag=0 
from table1 a
inner join table2 b on a.user_code=b.user_code 
where a.pin_number IS NULL

COMMIT TRANSACTION