none
SQL SELECT. Carriage Return generation

    Question

  • I need to generate an email report for a customer using T-SQL. The report needs to be in the format

    Field1

    Field2

    Field3

    Field1

    Field2

    Field3

    Can I use SELECT from a table to do this?

    Thanks

    Friday, July 12, 2013 7:00 PM

Answers

  • If you are mailing the report has html, you can do something like below.  Obviously you would want to probably wrap the report in a proc or something, and execute it with sp_send_dbmail.

    declare @t table(field1 int, field2 int, field3 int)
    insert into @t values (1,2,3);
    insert into @t values (4,5,6);
    
    select 
    	'<tr><td>' + CAST(field1 as varchar(10)) + '</tr></td></br>',
    	'<tr><td>' + CAST(field2 as varchar(10)) + '</tr></td></br>',
    	'<tr><td>' + CAST(field3 as varchar(10)) + '</tr></td></br>'
    from @t


    http://jahaines.blogspot.com/

    Friday, July 12, 2013 8:54 PM
    Moderator

All replies