【发布时间】:2019-10-14 15:00:19
【问题描述】:
我需要将以下代码转换为有效的 SQL 语句。
我尝试了多个更新语句,但后来意识到我可以在以后的更新语句中使用更新的值。这可能会导致多次更新,但原始代码只更新一次。 我也尝试过 CASE WHEN,但我找不到让 CASE 更新 Annuitant_1_DOB 或 Owner_DOB 的方法。
If B1_Joint_Annuitant_Switch = 1 then
If (ISNULL(Annuitant_1_DOB) or Annuitant_1_DOB < '1/1/1901') and ISNULL(Owner_DOB) = False then
Annuitant_1_DOB = Owner_DOB
Else
Owner_DOB = Annuitant_1_DOB
End If
Else
If (ISNULL(Owner_DOB) or Owner_DOB < '1/1/1901') and ISNULL(Annuitant_1_DOB) = False then
Owner_DOB = Annuitant_1_DOB
Else
Annuitant_1_DOB = Owner_DOB
End IF
End IF
结束 SQL 应该像 IF 语句一样运行,并且只对数据进行 1 次更新。 谢谢
尝试的代码:
--If joint_annuitant_switch_Exhibit = 1 then
-- If (ISNULL(DOBofAnnuitant1) or DOBofAnnuitant1 < '1/1/1901') and ISNULL(DOBofOwner) = False then
Update [INS_VAL_SANDBOX].[dbo].[VA_Download]
SET Annuitant_1_DOB = Owner_DOB
Where (Annuitant_1_DOB IS NULL or Annuitant_1_DOB < '1/1/1901') and Owner_DOB IS NOT NULL and B1_Joint_Annuitant_Switch = '1'
-- Else
Update [INS_VAL_SANDBOX].[dbo].[VA_Download]
SET Owner_DOB = Annuitant_1_DOB
Where (Annuitant_1_DOB IS NOT NULL or Annuitant_1_DOB >= '1/1/1901') and Owner_DOB IS NULL and B1_Joint_Annuitant_Switch = '1'
--Else
-- If (ISNULL(DOBofOwner) or DOBofOwner < '1/1/1901') and ISNULL(DOBofAnnuitant1) = False then
Update [INS_VAL_SANDBOX].[dbo].[VA_Download]
SET Owner_DOB = Annuitant_1_DOB
Where (Owner_DOB IS NULL or Owner_DOB < '1/1/1901') and Annuitant_1_DOB IS NOT NULL and B1_Joint_Annuitant_Switch <> '1'
-- Else
Update [INS_VAL_SANDBOX].[dbo].[VA_Download]
Set Annuitant_1_DOB = Owner_DOB
Where (Owner_DOB IS NOT NULL or Owner_DOB >= '1/1/1901') and Annuitant_1_DOB IS NULL and B1_Joint_Annuitant_Switch <> '1'
又一次尝试(显然不起作用):
Select
CASE B1_Joint_Annuitant_Switch
When 1 Then
CASE WHEN (Annuitant_1_DOB IS NULL or Annuitant_1_DOB < '1/1/1901') and Owner_DOB IS NOT NULL Then Annuitant_1_DOB = Owner_DOB
ELSE Owner_DOB = Annuitant_1_DOB END
ELSE
CASE WHEN (Owner_DOB IS NULL or Owner_DOB < '1/1/1901') and Annuitant_1_DOB IS NOT NULL Then Owner_DOB = Annuitant_1_DOB
ELSE Annuitant_1_DOB = Owner_DOB END
END
From [INS_VAL_SANDBOX].[dbo].[VA_Download]
【问题讨论】:
标签: sql sql-update