用于发送dbmail的触发器,有一个变量“ DIA”在使用时将我的dbmail的@body部分转换为null,只要不使用此变量,触发器就会生成设计的电子邮件,并且在使用时会产生一封电子邮件,里面没有任何内容
我尝试从插入中声明变量,并根据插入的部分作为选择查询声明相同的结果。我以为我在某种程度上将变量转换为null到我的代码中,但是我看不到在哪里发生。我因此包括了完整的代码。当我将@DIAstr或@ DIAstr2添加到dbmail的@body部分中时,会发生问题
很抱歉,如果我缺少明显的内容,例如不是DBA或IT方面的东西,我倾向于得到这些项目,因为我们公司没有IT,我会尽我所能,但我会自学成才,我知道是我理解的空白。
基于最后的响应快速澄清,我意识到字符串中的空值将导致整个字符串为空,我很确定这是正在发生的事情,我只是不明白为什么,因为该值我一直在测试中通过不为null。
CREATE TRIGGER INCORRECT_PART_ENTRY_NOTIFICATION2
ON PART
AFTER INSERT
AS
DECLARE @PART as VARCHAR(30) = (SELECT ID FROM Inserted)
DECLARE @DESCRIPTION as VARCHAR(50) = (SELECT DESCRIPTION FROM Inserted)
DECLARE @COMMODITY as VARCHAR(30) = (SELECT COMMODITY_CODE FROM Inserted)
DECLARE @PRODUCT as VARCHAR(30) = (SELECT PRODUCT_CODE FROM Inserted)
DECLARE @THICKstr as VARCHAR(30) = (SELECT USER_1 FROM Inserted)
DECLARE @THICK as decimal(7,4) = (SELECT CASE WHEN ISNUMERIC(USER_1) = 1 THEN CAST(USER_1 AS decimal(7,4)) ELSE null END FROM Inserted)
DECLARE @DIAstr as VARCHAR(30) (SELECT USER_4 FROM Inserted)
DECLARE @DIAstr2 as VARCHAR(30) (SELECT USER_4 FROM [dbo].[PART] WHERE ID = @PART)
DECLARE @DIA as decimal(7,4) = (SELECT CASE WHEN ISNUMERIC(USER_4) = 1 THEN CAST(USER_4 AS decimal(7,4)) ELSE null END FROM Inserted)
DECLARE @THICK_CHECK as VARCHAR(30)
SET @THICK_CHECK = CASE WHEN @PRODUCT IN('STOCK HEADS','DOM "MISC"','EXP "MISC"','MISC WORK','SUPPLIES','TEST','UPDATE','TOOLING') THEN 'GOOD'
WHEN @PART LIKE '%' + REPLACE(@THICKstr, '.', '') + '%' THEN 'GOOD'
ELSE 'NOT GOOD' END
DECLARE @DIA_CHECK as VARCHAR(30)
SET @DIA_CHECK = CASE WHEN @PRODUCT IN('STOCK HEADS','RAW MATERIAL','STOCK BLANK','DOM "MISC"','EXP "MISC"','MISC WORK','SUPPLIES','TEST','UPDATE','TOOLING') THEN 'GOOD'
WHEN @PART LIKE '%' + REPLACE(@DIAstr, '.', '') + '%' THEN 'GOOD'
ELSE 'NOT GOOD' END
DECLARE @CODE_CALC as VARCHAR(20)
SET @CODE_CALC = CASE WHEN @PRODUCT = 'SUPPLIES' THEN 'SUPPLIES'
WHEN @PRODUCT = 'TEST' THEN 'TEST'
WHEN @PRODUCT = 'TOOLING' THEN 'TOOLING'
WHEN @PRODUCT = 'MISC WORK' THEN 'MISC WORK'
WHEN @PRODUCT = 'DOM "MISC"' THEN 'DOM "MISC"'
WHEN @PRODUCT = 'PLATE' THEN 'PLATE'
WHEN @PRODUCT = 'STOCK HEADS' THEN 'STOCK HEADS'
WHEN @PRODUCT = 'STOCK BLANK' THEN 'STOCK BLANK'
WHEN @PRODUCT = 'RAW MATERIAL' THEN 'RAW MATERIAL'
WHEN @PRODUCT = 'UPDATE' THEN 'UPDATE'
WHEN @PRODUCT IN('DOMESTIC','FINISHED GOODS','INTERCOMPANY') THEN 'UPDATE'
WHEN SUBSTRING(@PART,1,1) = 'V' or SUBSTRING(@PART,1,1) = 'U' THEN 'B&R DOM'
WHEN CAST(@DIA AS decimal(7,4)) < 30 AND CAST(@THICK AS decimal(7,4)) < .75 THEN 'DOM "A"'
WHEN CAST(@DIA AS decimal(7,4)) < 30 AND CAST(@THICK AS decimal(7,4)) BETWEEN .75 AND 1.25 THEN 'DOM "B"'
WHEN CAST(@DIA AS decimal(7,4)) < 30 AND CAST(@THICK AS decimal(7,4)) > 1.25 THEN 'DOM "C"'
WHEN CAST(@DIA AS decimal(7,4)) BETWEEN 30 AND 72 AND CAST(@THICK AS decimal(7,4)) < .75 THEN 'DOM "D"'
WHEN CAST(@DIA AS decimal(7,4)) BETWEEN 30 AND 72 AND CAST(@THICK AS decimal(7,4)) BETWEEN .75 AND 1.25 THEN 'DOM "E"'
WHEN CAST(@DIA AS decimal(7,4)) BETWEEN 30 AND 72 AND CAST(@THICK AS decimal(7,4)) > 1.25 THEN 'DOM "F"'
WHEN CAST(@DIA AS decimal(7,4)) BETWEEN 72.001 AND 120 AND CAST(@THICK AS decimal(7,4)) < .75 THEN 'DOM "G"'
WHEN CAST(@DIA AS decimal(7,4)) BETWEEN 72.001 AND 120 AND CAST(@THICK AS decimal(7,4)) BETWEEN .75 AND 1.25 THEN 'DOM "H"'
WHEN CAST(@DIA AS decimal(7,4)) BETWEEN 72.001 AND 120 AND CAST(@THICK AS decimal(7,4)) > 1.25 THEN 'DOM "I"'
WHEN CAST(@DIA AS decimal(7,4)) BETWEEN 120.001 AND 144 AND CAST(@THICK AS decimal(7,4)) < .75 THEN 'DOM "J"'
WHEN CAST(@DIA AS decimal(7,4)) BETWEEN 120.001 AND 144 AND CAST(@THICK AS decimal(7,4)) BETWEEN .75 AND 1.25 THEN 'DOM "K"'
WHEN CAST(@DIA AS decimal(7,4)) BETWEEN 120.001 AND 144 AND CAST(@THICK AS decimal(7,4)) > 1.25 THEN 'DOM "L"'
WHEN CAST(@DIA AS decimal(7,4)) > 144 AND CAST(@THICK AS decimal(7,4)) < .75 THEN 'DOM "M"'
WHEN CAST(@DIA AS decimal(7,4)) > 144 AND CAST(@THICK AS decimal(7,4)) BETWEEN .75 AND 1.25 THEN 'DOM "N"'
WHEN CAST(@DIA AS decimal(7,4)) > 144 AND CAST(@THICK AS decimal(7,4)) > 1.25 THEN 'DOM "O"'
ELSE 'UPDATE' END
DECLARE @CODE_CHECK AS CHAR(1)
SET @CODE_CHECK = CASE WHEN @CODE_CALC = 'UPDATE' THEN 'N'
WHEN @PRODUCT = @CODE_CALC THEN 'Y'
ELSE 'N' END
BEGIN
SET NOCOUNT ON;
DECLARE @SEND as Char(1)
SET @SEND = CASE WHEN @THICK_CHECK = 'NOT GOOD' THEN 'Y'
WHEN @DIA_CHECK = 'NOT GOOD' THEN 'Y'
WHEN @CODE_CHECK = 'N' THEN 'Y'
ELSE 'N' END
DECLARE @MailSubject AS varchar(50)
Set @MailSubject = 'Incorrect Part Entry - '+@PART
DECLARE @MESSAGE as Varchar(max)
SET @MESSAGE = (SELECT 'It appears that an error may have been made entering Part ID '+ @PART+'.' +
CHAR(10) +
'This e-mail was triggered either by descripencies in the calculated product code and the enntered product code, or a match between the values entered for thickness and DIA could not be made in the Part ID.' +
CHAR(10) +
@CODE_CALC + ' ' + @PRODUCT + ' ' + @THICKstr + ' ' + @DIAstr2)
IF @SEND = 'Y'
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'xxxxx@xxx',
@profile_name = 'xxxxxx',
@subject = @MailSubject,
@body = @MESSAGE;
END
END
GO