string sSQL = @"UPDATE [MyTable1]
SET AT09 = GETDATE()
,AT80 = 'IP'
,AT83 = 'CO'
,AT34 = GETDATE() //ONLY IF IT IS NOT EMPTY
,AT84 = '" + sU + "' WHERE AS = 0";
我在C#中有上述查询,我想要做的只是更新AT34
字段,如果它没有值。
因此,当查询首先运行时,它会添加当前日期但是任何后续时间,它都不会(给定列填充日期)
我怎样才能做到这一点。
答案 0 :(得分:7)
尝试
string sSQL = @"UPDATE [MyTable1]
SET AT09 = GETDATE()
,AT80 = 'IP'
,AT83 = 'CO'
,AT34 = (CASE WHEN AT34 IS NULL THEN GETDATE() ELSE AT34 END)
,AT84 = '" + sU + "' WHERE AS = 0";
答案 1 :(得分:3)
string sSQL = @"UPDATE [MyTable1]
SET AT09 = GETDATE()
,AT80 = 'IP'
,AT83 = 'CO'
,AT34 = CASE WHEN AT34 IS NULL THEN GETDATE() ELSE AT34 END
,AT84 = '" + sU + "' WHERE AS = 0";
答案 2 :(得分:3)
您可以使用此表达式指定at34&#39的值
AT34 = ISNULL(AT34, GETDATE())
答案 3 :(得分:2)
在您现在使用AT34=
的地方使用说明Case When AT34 is null then GetDate() else AT34 End
AT34=GetDate()
的案例词组。
答案 4 :(得分:1)
使用此
string sSQL = @"UPDATE [MyTable1]
SET AT09 = GETDATE()
,AT80 = 'IP'
,AT83 = 'CO'
,AT34 = CASE WHEN AT34 IS NOT NULL
THEN GETDATE()
ELSE AT34
END //ONLY IF IT IS NOT EMPTY
,AT84 = '" + sU + "'
WHERE AS = 0";
答案 5 :(得分:1)
使用isnull ...
string sSQL = @"UPDATE [MyTable1]
SET AT09 = GETDATE()
,AT80 = 'IP'
,AT83 = 'CO'
,AT34 = ISNULL(AT34, GETDATE()) //ONLY IF IT IS NOT EMPTY
,AT84 = '" + sU + "' WHERE AS = 0";
...或者在两个条件下调用两个更新命令。一个条件应检查日期是否为空,另一个条件检查是否不是......
string sSQL = @"UPDATE [MyTable1]
SET AT09 = GETDATE()
,AT80 = 'IP'
,AT83 = 'CO'
,AT34 = GETDATE() //ONLY IF IT IS NOT EMPTY
,AT84 = '" + sU + "' WHERE AS = 0 AND AT34 IS NULL
UPDATE [MyTable1]
SET AT09 = GETDATE()
,AT80 = 'IP'
,AT83 = 'CO'
,AT84 = '" + sU + "' WHERE AS = 0 AND AT34 IS NOT NULL";
答案 6 :(得分:1)
好的,您也可以通过使用C#条件来完成此操作。
AT34 = GETDATE()
希望有所帮助
答案 7 :(得分:1)
变式1: 设置AT34 = isnull(GETDATE(),AT34)
变式2:
设置AT34 CASE 当AT34为空时 那么getdate() ELSE AT34 END