DECLARE @tableHTML NVARCHAR(MAX)
DECLARE @VSubject varchar(50)
Declare @sql1 Varchar(4000)
Declare @FileName Varchar(500)
declare @tab varchar(10)=char(9)
Declare @date varchar(30)=getdate()-2
declare @nextline varchar(10) , @body1 varchar(2000),@SubTitle varchar(200)
Truncate table Tsurvey
set @VSubject = 'Survey Updates On ' + convert(varchar(11),@date ,106)
SET @FileName='\\10.200.2.40\MartReports\Survey_'+cast(convert(date,@date) as varchar)+'.xls'
set @nextline= ''+char(13)
set @SubTitle= 'Survey Details'
SET @tableHTML = N'<H2>'+@SubTitle+'</H2>'
+N'<table border="3">'
+N'<th><p style="color:blue;margin-left:20px;">ID</p></th>'
+N'<th><p style="color:Red;margin-left:20px;">CreatedDate</p></th>'
+N'<th><p style="color:Green;margin-left:20px;">PhoneNumber</p></th>'
+N'<th><p style="color:Blue;margin-left:20px;">EmpCode</p></th>'
+N'<th><p style="color:Orange;margin-left:20px;">PatientName</p></th>'
+N'<th><p style="color:blue;margin-left:20px;">Question</p></th>'
+N'<th><p style="color:Green;margin-left:20px;">Answer</p></th>'
+
CAST
( ( select
td = a.ID,'',
td = a.CreatedDate,'',
td = a.PhoneNumber,'',
td = a.EmpCode,'',
td = a.PatientName,'',
td = a.Question,'',
td = a.Answer,''
from
(
select ID,convert(varchar(11),CreatedDate,106) as CreatedDate,PhoneNumber,EmpCode,PatientName,Question,Answer
from [tbl_UserSurveyQuestionAnswer] where cast([CreatedDate] as date) = cast(@date as date)
) as A
FOR XML PATH('tr'), TYPE )
AS NVARCHAR(MAX) ) + N'</table>' ;
print @tableHTML
Set @body1 = @tableHTML +@nextline+ 'Please find the attachment Excel.'
----Excel----
insert Tsurvey (ID,CreatedDate,PhoneNumber,EmpCode,PatientName,Question,Answer)
select ID,convert(varchar(11),CreatedDate,106) as CreatedDate,PhoneNumber,EmpCode,PatientName,Question,Answer
from [tbl_UserSurveyQuestionAnswer] where cast([CreatedDate] as date) = cast(@date as date)
select @sql1 = 'bcp "select ''ID'',''CreatedDate'',''PhoneNumber'',''EmpCode'',''PatientName'',''Question'',''Answer'' UNION ALL SELECT cast(ID as varchar(50)),cast([CreatedDate] as varchar(50)),cast([PhoneNumber] as varchar(50)),[EmpCode],[PatientName],[Question],[Answer] FROM survey.dbo.tsurvey " QUERYOUT '+@FileName+' -c -T -S '+ @@servername
--print @sql1
if LEN(@tableHTML)>0
begin
EXEC MASTER..xp_cmdshell @sql1
EXEC msdb.dbo.sp_send_dbmail
--@profile_name = 'hrms',
@recipients = 'mailid' ,
--@copy_recipients = @CC ,
@subject = @VSubject ,
@body = @body1 ,
@file_attachments=@FileName ,
@body_format = 'HTML' ;
end