sql server - How to loop the variables in t-sql -
i have t-sql have loop variables.
declare @empno nvarchar(max),@tablehtml nvarchar(max) ; set @empno = (select employee_no emptemp active = 'yes'); set @tablehtml = n'<h2>additions</h2>' + n'<table border="1">' + n'<th>ename</th>' + n'<th>sal</th>' + '<tr>' + cast ( ( select td=ename,'',td=sal,'' emp empno = @empno) xml path('tr'), type ) nvarchar(max) ) + n'</table>' ; exec msdb.dbo.sp_send_dbmail @recipients=someone@gmail.com, @subject = emp , @body = @tablehtml , @body_format = 'html', @profile_name = 'sql profile'
the emptemp table looks like
id empno active 1 245 yes 2 124 yes 3 255 yes 4 224 no
i have send individual mail based on data in emp table
that how cursor like:
declare @empno nvarchar(max) declare @tablehtml nvarchar(max) ; declare @id int; declare curs cursor fast_forward select distinct id demptemp active = 'yes' order id open curs fetch next curs @id while @@fetch_status = 0 begin set @empno = (select empno emptemp id = @id); set @tablehtml = n'<h2>additions</h2>' + n'<table border="1">' + n'<th>ename</th>' + n'<th>sal</th>' + '<tr>' + cast ( ( select td=ename,'',td=sal,'' emp empno = @empno) xml path('tr'), type ) nvarchar(max) ) + n'</table>' ; exec msdb.dbo.sp_send_dbmail @recipients=someone@gmail.com, @subject = emp , @body = @tablehtml , @body_format = 'html', @profile_name = 'sql profile' fetch next curs @id end close curs deallocate curs;
Comments
Post a Comment