在SQL SERVER中使用ISNULL更新查询

时间:2014-06-11 04:09:56

标签: sql sql-server tsql

DateTime如果传递数据则应更新,否则使用已保存的原始值更新它。此更新无效

DECLARE @FolderStatusDate DATETIME = NULL

SET @FolderStatusDate = '2012-07-04 14:09:04.043'

UPDATE CM.PfmFolder     
  SET   
      FolderStatusDate = ISNULL(@FolderStatusDate, FolderStatusDate)
      WHERE Id = @Id  

3 个答案:

答案 0 :(得分:1)

为什么不将NULL的支票移到WHERE条款?

DECLARE @FolderStatusDate DATETIME = NULL

SET @FolderStatusDate = '2012-07-04 14:09:04.043'

UPDATE CM.PfmFolder     
  SET   
      FolderStatusDate = @FolderStatusDate
      WHERE Id = @Id  
      AND @FolderStatusDate IS NOT NULL

答案 1 :(得分:0)

你也可以这样做

DECLARE @FolderStatusDate DATETIME = NULL

SET @FolderStatusDate = '2012-07-04 14:09:04.043'

UPDATE CM.PfmFolder     
SET   
  FolderStatusDate = case when ISNULL(@FolderStatusDate, '') = '' 
                           then FolderStatusDate else @FolderStatusDate end
  WHERE Id = @Id

答案 2 :(得分:0)

Hitesh Salian 回答

的轻微修改
 DECLARE @FolderStatusDate DATETIME = NULL

 SET @FolderStatusDate = '2012-07-04 14:09:04.043'

 UPDATE CM.PfmFolder     
 SET   
 FolderStatusDate = case @FolderStatusDate is Null
                       then FolderStatusDate else @FolderStatusDate end
 WHERE Id = @Id