2011-02-04 5 views
1

이메일을 서버에서 몇 명의 수신자에게 보내려고합니다.Send Email From Database

나는 사용하여 내 데이터베이스를 쿼리 수행

SELECT Email from payrollchecksystem 

그리고보다

이메일을 보내 다음 사용

USE msdb 
GO 
EXEC sp_send_dbmail @profile_name='SQL Server Alerts System', 
@recipients = I DONT KNOW WHAT TO PUT HERE 
@subject='Test message', 
@body='This is the body of the test message. 
Congrates Database Mail Received By you Successfully.' 

내 문제는 내가하려고 할 때 @recipients을 위해 무엇을 입력해야되는지 잘 모릅니다이다 payrollchecksystem 테이블에서받은 모든 이메일을 보내시겠습니까?

답변

2

http://msdn.microsoft.com/en-us/library/ms190307.aspx

@recipients 은 세미콜론으로 구분으로 전자 메일을 분리하기 위해

+0

그러나 100 명의 수신자에게 이메일을 보내려한다면 어떻게 될까요? 수신자 목록이있는 .csv 파일을 만들었습니다. 이 경우 @recipients에 대해 무엇을 넣어야합니까? – hhhh

+1

문서와 같이 세미콜론으로 구분 된 100 개의 이메일 주소를 입력해야합니다. –

5

에 메시지를 보내 전자 메일 주소를 세미콜론으로 구분 한 목록입니다 문자열, 사용 COALESCE :

DECLARE @EmailList varchar(MAX) 

SELECT @EmailList = COALESCE(@EmailList + ';', '') + Email 
FROM payrollchecksystem 
WHERE Email IS NOT NULL 

SELECT @EmailList 

그러면 @EmailList를 다음과 같이 사용할 수 있습니다 :

USE msdb 
GO 
EXEC sp_send_dbmail @profile_name='SQL Server Alerts System', 
    @recipients = @EmailList 
    @subject='Test message', 
    @body='This is the body of the test message. Congrates Database Mail Received By you Successfully.'