2014-07-07 3 views
1

사용자 생성시 전자 메일을 보내는 간단한 절차가 있습니다. 사용자 이름과 전체 이름 필드를 채우려면 config 테이블에서 전자 메일 본문을로드 한 다음 REPLACE를 사용하여 자리 표시자를 바꿉니다.T-SQL 전자 메일 본문의 REPLACE 문으로 인해 Null 값이 발생합니다.

SELECT @BodyMessage = ConfigValue , @Email = email 
FROM dbo.ConfigValues 
WHERE ConfigName = 'ADNotification' 

IF @Email = '' SET @Email = @DefaultEmail 

SET @BodyMessage = REPLACE(@BodyMessage, 'FullName', @FullName) 
SET @BodyMessage = REPLACE(@BodyMessage, 'UserName', @UserName) 
Select @BodyMessage 

SET @SubjectLine = 'User Account Created' 

EXEC msdb.dbo.sp_send_dbmail @profile_name='EUI', 
@[email protected], @[email protected], 
@body_format = 'TEXT', @body= @BodyMessage 

이렇게 실행하면 @BodyMessage는 비어 있습니다. 내가 주석 두 사람이 문을 교체하는 경우, 이메일 내가 최근에 다른 피드백을 기반으로 SELECT @Bodymessage 문을 추가

SELECT @BodyMessage = ConfigValue , @Email = email 
FROM dbo.ConfigValues 
WHERE ConfigName = 'ADNotification' 

IF @Email = '' SET @Email = @DefaultEmail 

--SET @BodyMessage = REPLACE(@BodyMessage, 'FullName', @FullName) 
--SET @BodyMessage = REPLACE(@BodyMessage, 'UserName', @UserName) 
Select @BodyMessage 

SET @SubjectLine = 'User Account Created' 

EXEC msdb.dbo.sp_send_dbmail @profile_name='EUI', 
@[email protected], @[email protected], 
@body_format = 'TEXT', @body= @BodyMessage 

(이 같은) 잘 보낸다; 코드는 명령문의 유무와 상관없이 동일하게 실행됩니다. msdb 데이터베이스에서 보낸 편지함 테이블을 확인하면 본문이 null입니다.

내가 할 일은 대체 문을 올바르게 필드를 바꾸는 것입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+4

어디에서'@ FullName' /'@ UserName'을 정의합니까? 그것들이 NULL의 경우, 결과의'@ BodyMessage'는 치환 후에 NULL가됩니다. – JiggsJedi

+2

'@FullName'매개 변수와 '@Email'매개 변수는 어디에 설정되어 있습니까? – MDiesel

+1

그게 전부 였어. 우리는 MiddleName이있는 @FullName을 구성하고 있었으며 설정할 필요가 없습니다. 나는 REPLACE의 행동에 대해 몰랐다. 감사! –

답변

3

거의 확실하게 @FullName 또는 @UserName은 NULL입니다. 이렇게하면 REPLACE 함수가 NULL을 리턴합니다. 이 두 가지 값을 확인하십시오. 또는 대체 함수에 ISNULL을 추가 할 수 있습니다.

이와 비슷한 것.

SET @BodyMessage = REPLACE(@BodyMessage, 'FullName', ISNULL(@FullName, '')) 
SET @BodyMessage = REPLACE(@BodyMessage, 'UserName', ISNULL(@UserName, '')) 
Select @BodyMessage