1

I have to use T-SQL (SQL Server) in a stored procedure to generate an html table from a SELECT. I'm not sure if/how I might be best able to add additional attributes to the basic html table attributes;

select 
    (select 'ID' as 'th' for xml path(''),  type),
    (select 'StartDate'   as 'th' for xml path(''), type),
    (select 'LPlace'  as 'th' for xml path(''), type)
union all 
select 
    (select p.[Id]     as 'td' for xml path(''), type),
    (select p.[Created]           as 'td' for xml path(''), type),
    (select p.[LPlace] as 'td' for xml path(''), type)
from [SourceTbl] p
for xml path('tr'), ROOT('table')

Results:

<table>
  <tr>
    <th>ID</th>
    <th>StartDate</th>
    <th>LPlace</th>
  </tr>
  <tr>
    <td>1</td>
    <td>2019-10-13T11:36:22.0050221</td>
    <td>K7</td>
  </tr>
  <tr>
    <td>2</td>
    <td>2019-03-10T13:12:07.1008696</td>
    <td>J6</td>
  </tr>

What I would like to add is things such as:

<table style=''font-family: Arial;font-size: 8pt;border: solid 1px #888;border-collapse: collapse;

<th style=''border: 1px solid #090F20;background-color: #0D417E;padding: 2px 0;color: #F8F8FF;font-size: 9pt;''>ID</th>

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
stan4th
  • 710
  • 1
  • 6
  • 19
  • 2
    Personally, if you're really "bothered" about the presentation, don't use SQL Server to create the HTML, and use a proper presentation layer. – Thom A Mar 16 '20 at 17:10
  • 3
    How about simply adding a css stylesheet to the output? (If you don't want to go the presentation layer way.) – Filburt Mar 16 '20 at 17:14
  • 1
    Take a peek at https://dbfiddle.uk/?rdbms=sqlserver_2016&fiddle=ee8006ee199235c0894fa92645645529 Rather than STYLE ... CLASS may be cleaner – John Cappelletti Mar 16 '20 at 17:41
  • @Larnu I know (I feel quite embarrassed - but I don't get to choose...) – stan4th Mar 17 '20 at 09:08
  • 1
    Thanks @JohnCappelletti that's what I was looking for :) – stan4th Mar 17 '20 at 10:52

0 Answers0