I'm trying to send an email by a SQL Server job with the result of a query.
The query works perfectly and I face an issue when I pass a TABLE in the @query
parameter of sp_send_dbmail
Here is my code :
DECLARE @res TABLE
(
SiteCode [nvarchar](50),
DateLastODV [datetime]
);
INSERT INTO @res
SELECT
SiteCode
,MAX(DateODV) AS DateLastODV
FROM Configuration.ODVCompteur
where year(DateODV) = 2015
group by SiteCode
order by DateLastODV desc
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Foo',
@recipients = 'foo@foooo.com',
@subject = 'Foooooooo',
@query = @res,
@Attach_Query_result_as_file = 0
I got this error (in french but can easily be translate if needed) :
Line 0: Procedure: sp_send_dbmail, Msg 206, Level 16, State 2: Conflit de types d'opérandes : table est incompatible avec nvarchar(max)