如何从Sql server数据库表中以适当的格式显示邮件中的数据?

时间:2014-03-27 15:30:20

标签: sql sql-server string sql-server-2008

当用户通过数据库注册邮件ID时,我发送邮件。我希望邮件正文中的文本字段之间有空格。

SQL查询:

SELECT @Message =@MessageHeader + char(13) + char(10)+CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
 +'Company Name: ' +  IsNull([CompanyName],'') + char(13) + char(10)         
 + 'Contact Name:' + IsNull([ContactName],'') +char(13) +char(10) 
 + 'Contact Email:'+ CHAR(9) + CHAR(9)  + IsNull([ContactEmail],'') +char(13) +char(10) 
 + ' Password:'+ CHAR(9) + CHAR(9)  + IsNull([Password],'') +char(13) +char(10) 
 + 'Phone Number:'+ CHAR(9) + CHAR(9)  + IsNull([PhoneNumber],'') +char(13) +char(10) 
 + 'Address:'+ CHAR(9) + CHAR(9)  + IsNull([Address],'') +char(13) +char(10) 
 + 'Country:'+ CHAR(9) + CHAR(9)  + IsNull([Country],'') +char(13) +char(10) 
 + 'State:'+ CHAR(9) + CHAR(9)  + IsNull([State],'') +char(13) +char(10) 
 + 'City:'+ CHAR(9) + CHAR(9)  + IsNull([City],'') +char(13) +char(10) 
 + 'Zip:'+ CHAR(9) + CHAR(9)  + IsNull([Zipcode],'') +char(13) +char(10) 
 + 'Fax:'+ CHAR(9) + CHAR(9)  + IsNull([Fax],'') +char(13) +char(10) 

FROM MemTable WHERE MemberID= @MemberID 

现在邮件输出是:

Member ID:   22736
Company Name: tesCFS
Contact Name: tesCFS
Contact Email: tesCFS@tesCFS.com
Password: tesCFS
Phone Number: 213
Address:     tesCFS
Country:    
State:  
City:   
Zip:    
Fax:    

我希望输出为:

Member ID:          22736
Company Name:       tesCFS
Contact Name:       tesCFS
Contact Email:      tesCFS@tesCFS.com
Password:           tesCFS
Phone Number:       213
Address:            tesCFS
Country:    
State:  
City:   
Zip:    
Fax:    

1 个答案:

答案 0 :(得分:1)

我假设您希望两列排成一行。您可以考虑使用HTML电子邮件格式,在手动构造的表行元素中包装。它并不优雅,但却很有效。

如果要保留文本,则需要使用可变数量的标签,具体取决于您的姓名的长度,数量取决于显示程序(可能是电子邮件客户端)提供的空格数量制表符。您可以改为使用REPLICATE空间进行绝对控制。见下面的代码示例。

在任何一种情况下,您都必须(1)手动计算和编码您需要的许多选项卡或空格,或者(2)使表格返回名称,然后根据它们的长度对它们进行计算。

SELECT

-- Option 1, using tabs and manually figuring out how many are required
'Message Header' + char(13) + char(10)+CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
+ 'Company Name:' +  CHAR(9) + IsNull(CompanyName,'') + char(13) + char(10)         
+ 'Contact Name:' + CHAR(9) + IsNull(ContactName,'') +char(13) +char(10) 
+ 'Zip:' + CHAR(9) + CHAR(9) + CHAR(9) + IsNull(Zip,'') +char(13) +char(10) AS Option1,

-- Option 2, using spaces and manually figuring out how many are required
'Message Header' + char(13) + char(10)+CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
+ 'Company Name:' + REPLICATE(' ',3) + IsNull(CompanyName,'') + char(13) + char(10)         
+ 'Contact Name:' + REPLICATE(' ',3) + IsNull(ContactName,'') +char(13) +char(10) 
+ 'Zip:' + REPLICATE(' ',12) + IsNull(Zip,'') +char(13) +char(10) AS Option2,

-- Option 3, using dynamic space calculation
'Message Header' + char(13) + char(10)+CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
+ CompanyNamePrefix + ':' + REPLICATE(' ',(ColNum-LEN(CompanyNamePrefix))) + IsNull(CompanyName,'') + char(13) + char(10)         
+ ContactNamePrefix + ':' + REPLICATE(' ',(ColNum-LEN(ContactNamePrefix))) + IsNull(ContactName,'') +char(13) +char(10) 
+ ZipPrefix + ':' + REPLICATE(' ',(ColNum-LEN(ZipPrefix))) + IsNull(ZipPrefix,'') +char(13) +char(10) AS Option3

FROM
(
    SELECT
        15 AS ColNum,
        'Company Name' AS CompanyNamePrefix,
        'Contact Name' AS ContactNamePrefix,
        'Zip' AS ZipPrefix
) AS Names
CROSS JOIN
(
    SELECT
         'Ed''s Company' AS CompanyName,
         'Ed' AS ContactName,
         '55120' AS Zip
    UNION ALL
    SELECT
        'Bill''s Company',
        'Bill',
        '90210'
) AS MemTable;