开发者社区> 问答> 正文

SQL Server:编辑查询结果在SQL Server发送的电子邮件中的显示方式

我已经建立了一个存储过程,如果满足条件,则将发送电子邮件,并且在发送电子邮件时,它看起来像这样。

Name            Assignment
                                         Start_Date      End_Date
--------------------------------------------------------------------------------------------------
---
---------------------------------------------------------------
Doe, John        Light Duty
                                            2019-01-05      2019-10-05
(1 row affected)
我希望标题显示在同
一```  
行中,减少虚线,并将结果显示在同一行中。如何修改查询结果显示?

展开
收起
祖安文状元 2020-01-05 14:41:22 602 0
1 条回答
写回答
取消 提交回答
  • 您可以尝试类似:

    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”更改为来自实际表,则您真正需要做的所有事情。

    2020-01-05 14:41:35
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
SQL Server在电子商务中的应用与实践 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载