[英]Update query with ISNULL in SQL SERVER
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
為什么不將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
你也可以這樣
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
對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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.