简体   繁体   English

如何在 SQL 服务器中创建 IF

[英]How to create an IF in SQL Server

I have a table that contains a lot of requests ( BursaryRequestId ).我有一个包含很多请求的表( BursaryRequestId )。 Some requests have the same identifier because they have an original and modified copy.一些请求具有相同的标识符,因为它们具有原始副本和修改副本。

I need to write a T-SQL script that will check all the results if the Identifier StatusID of the modified version is equal to the original version.我需要编写一个 T-SQL 脚本,如果修改版本的 Identifier StatusID等于原始版本,它将检查所有结果。 If it's not equal then change the modified version with the original's version StatusID .如果不相等,则使用原始版本StatusID更改修改后的版本。

桌子

You can see on the image that the modified version ( IsOriginal = '0' ) have a StatusId of '1' and that need to be changed to '3' like the original version.您可以在图像上看到修改后的版本 ( IsOriginal = '0' ) 的StatusId为 '1' 并且需要像原始版本一样更改为 '3'。

I think you want:我想你想要:

with toupdate as (
      select t.*,
             max(case when isoriginal = 1 then statusid end) over (partition by identifier) as original_statusid
      from t
     )
update toupdate
    set statusid = original_statusid
    where isoriginal = 0 and statusid <> original_statusid;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM