Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用空日期更新所有表_Sql_Sql Server 2008 - Fatal编程技术网

Sql 使用空日期更新所有表

Sql 使用空日期更新所有表,sql,sql-server-2008,Sql,Sql Server 2008,我得到了这个select查询的帮助,我正在使用它来获取两列中具有空值的所有表 select count(*) from ( select LastModifiedDate from Table1 union all select LastModifiedDate from Table2 union all select LastModifiedDate from Table3 ) a where LastModifiedDate is null 要更

我得到了这个select查询的帮助,我正在使用它来获取两列中具有空值的所有表

select count(*)
from (
   select LastModifiedDate from Table1
      union all
   select LastModifiedDate from Table2
      union all
   select LastModifiedDate from Table3
) a 
where LastModifiedDate is null
要更新所有三个表,有更好的方法吗

Update table1
SET LastModifiedDate = GETDATE()
WHERE LastModifiedDate is null

Update table2
SET LastModifiedDate = GETDATE()
WHERE LastModifiedDate is null
等等?
很抱歉,我拆分了查询,一个查询查找CreationDate,另一个查询查找LastModifiedDate

要更新三个表,如果其中任何一列为空,则将两列设置为当前日期时间


三条tableupdate语句就可以了。没有比这更简单的了。

是的,有更好的方法。正如Gordon Linoff在评论中指出的,您可能会用建议的查询覆盖有效的创建日期

请尝试以下方法:

Update table1
SET CreationDate = ISNULL(CreationDate,GETDATE())
  , LastModifiedDate = GETDATE()
WHERE CreationDate is null or LastModifiedDate is null

Update table2
SET CreationDate = ISNULL(CreationDate,GETDATE())
  , LastModifiedDate = GETDATE()
WHERE CreationDate is null or LastModifiedDate is null

Update table3
SET CreationDate = ISNULL(CreationDate,GETDATE())
  , LastModifiedDate = GETDATE()
WHERE CreationDate is null or LastModifiedDate is null
使用
ISNULL
将保留现有的
CreationDate
数据。您不希望将其用于
LastModifiedDate
,因为您将要修改行,因此该值应该更改


但是,没有理由尝试重新构造您的语句,因为它们没有问题。

即使只有一个日期为空,您也要同时更改这两个日期吗?你可能写了很多有效的创建日期。