I have to execute SQL statement every day and send result to dba by email.I configured database email.
Below is query:
SELECT
account.accountID,
account.name
FROM
account
LEFT OUTER JOIN accountfeaturesetting afs
ON afs.accountid = account.accountid
and afs.featureid = 'Schedules'
and afs.settingid = 'EditReasons'
WHERE
ISNULL(afs.Value, '0') = '1'
AND NOT EXISTS
(SELECT 1 FROM program
WHERE program.AccountID = account.AccountID
AND program.Active = 1 AND
(program.ScheduleEditReasonFlags <> 0 OR
program.ScheduleEditReasonFields <> 0))
AND account.IsMaster = 0
For this I created Sql job having 2 steps:
- execute select statement
send result:
Use MSDB EXEC msdb.dbo.sp_send_dbmail @profile_name = 'abc', @recipients = '[email protected]', @subject = 'queryresultset', @body='testmail', @query_attachment_filename='warning.csv'
It is throwing an error and the result set is not attached with query.
Error: Parameter @attach_query_result_as_file cannot be 1 (true) when no value is specified for parameter @query.
A query must be specified to attach the results of the query.
[SQLSTATE 42000] (Error 14625)
Edit:
Use MSDB
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'abc',
@recipients = '[email protected]',
@subject = 'queryresultset',
@body='testmail',
@query_attachment_filename='warning.csv'
DECLARE @msg VARCHAR(250);
declare @query varchar(2048);
SELECT @msg = 'Please refer to the attached spread sheet for the report.';
set @query='
SELECT
account.accountID,
account.name
FROM
account
LEFT OUTER JOIN accountfeaturesetting afs
ON afs.accountid = account.accountid
and afs.featureid = 'Schedules'
and afs.settingid = 'EditReasons'
WHERE
ISNULL(afs.Value, '0') = '1'
AND NOT EXISTS
(SELECT 1 FROM program
WHERE program.AccountID = account.AccountID
AND program.Active = 1 AND
(program.ScheduleEditReasonFlags <> 0 OR
program.ScheduleEditReasonFields <> 0))
AND account.IsMaster = 0
go';
exec (@query)
Above query is not taking schedules and editreasons value column. I am still getting a syntax error after declaring all variables like:
declare @featureid varchar(100);
declare @settingid varchar(100);
declare @value varchar(100);