我已经建立了一个存储过程,如果满足条件,则将发送电子邮件,并且在发送电子邮件时,它看起来像这样。
Name Assignment
Start_Date End_Date
--------------------------------------------------------------------------------------------------
---
---------------------------------------------------------------
Doe, John Light Duty
2019-01-05 2019-10-05
(1 row affected)
我希望标题显示在同
一```
行中,减少虚线,并将结果显示在同一行中。如何修改查询结果显示?
您可以尝试类似:
IF OBJECT_ID('tempdb..#Data') IS NOT NULL DROP TABLE #Data
-- Create your dataset, assuming Name, Assignment, StartDate and EndDate as columns
SELECT
*
INTO #Data
FROM YourTable
-- Variables to iterate into
DECLARE @Name VARCHAR(MAX)
DECLARE @Assignment VARCHAR(MAX)
DECLARE @StartDate DATE
DECLARE @EndDate DATE
-- If dataset is empty, do nothing
IF (SELECT COUNT(*) FROM #Data) > 0 BEGIN
-- Create headers and css
SET @HtmlContent = '
<style>
html { font-family:Arial }
th, td { padding:5px; }
table { border-collapse:collapse }
th { background-color: blue; border:1px solid white }
td { border: 1px solid #ccc }
</style>
<table>
<tr>
<th>Name</th>
<th>Assignment</th>
<th>Start Date</th>
<th>End Date</th>
</tr>'
-- Create and open cursor
DECLARE DataCursor CURSOR FOR SELECT * FROM #Data
OPEN DataCursor
-- fetch first results
FETCH NEXT FROM DataCursor INTO @Name, @Assignment, @StartDate, @EndDate
-- Loop while results are still being fetched
WHILE @@FETCH_STATUS = 0 BEGIN
-- Create the next html row
SET @HtmlContent = @HtmlContent + '
<tr>
<td>' + @Name + '</td>
<td>' + @Assignment + '</td>
<td style=''text-align:center''>' + FORMAT(@StartDate, 'dd/MM/yyyy') + '</td>
<td style=''text-align:center''>' + FORMAT(@EndDate, 'dd/MM/yyyy') + '</td>
</tr>'
-- Fetch the next results and restart loop
FETCH NEXT FROM DataCursor INTO @Name, @Assignment, @StartDate, @EndDate
END
-- Close and cleanup the cursor
CLOSE DataCursor
DEALLOCATE DataCursor
-- Close the table, we opened it in the headers
SET @HtmlContent = @HtmlContent + '</table>'
-- Send the stuff
EXEC msdb..sp_send_dbmail @profile_name='Your mail profile',
@recipients='Something@somewhere.com',
@copy_recipients='Something@somewhere.com',
@subject='My subject',
@body=@HtmlContent,
@body_format = 'HTML'
END
大猩猩,您可能应该使用nvarchars而不是varchars(可能没有关于它的信息)。但这可能会让您入门。如果将“ SELECT * INTO #Data FROM YourTable”更改为来自实际表,则您真正需要做的所有事情。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。