[英]T-SQL concatenation issue with NULLS
我正在尝试进行简单的串联,但是在某处失败! 我只想在其他两个变量不为空时在电子邮件中显示注释。 即使一个变量包含数据,它也应显示注释。 下面是我的代码。
SET @Note = CASE
WHEN @tableHTML_AssignedTo IS NOT NULL OR @tableHTML_SubmittedBy IS NOT NULL
THEN 'Note: The tickets with the following statuses are not listed in this alert: Closed,Rejected'
ELSE NULL
END
SET @ComposeBody = ISNULL(@tableHTML_AssignedTo, '') +
ISNULL(@tableHTML_SubmittedBy, '') +
ISNULL(@Note, '')
我知道这是简单的代码,但对我做错的事情我并不明显。 但是用我的代码,如果两个表都为Notes都为空,则会发送一封空电子邮件。 如果删除Notes,则不会发送电子邮件,但我想在电子邮件中添加Notes
注意:我正在光标内执行此代码以递归发送电子邮件
我会这样-看起来更简单。
这里的技巧是了解null如何与concat一起使用。 具有null的任何concat为null。 因此,如果Assigned或Submitted为null,则COALESCE的第一个参数将为null。 下一步每个步骤都会检查其他两种情况。
SET @Note = 'Note: The tickets with the following statuses are not listed in this alert: Closed,Rejected';
SET @ComposeBody = COALESCE(
@tableHTML_AssignedTo+@tableHTML_SubmittedBy+@Note, -- Selected if both non null
@tableHTML_AssignedTo+@Note, -- Selected if SubmittedBy null
@tableHTML_SubmittedBy+@Note) -- Selected if AssignedTo null
-- Both are null, set body to null.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.