score:3

Accepted answer

Your error indicates that somewhere you are selecting something you want to be an attribute (the error indicates 'style') after you are selecting something that is not an attribute (probably one of your table headers). FOR XML PATH doesn't allow that, you have to select the attributes first. I cannot, however, see where you are selecting @style in your code.

I've modified your query to select the XML in the format you desire below.

declare @style nvarchar(50) = 'border-bottom:1px solid #7AC0DA'
declare @Activities table
(
    StartDate datetime,
    LeadGroup nvarchar(10),
    LeadBy nvarchar(20),
    Title nvarchar(50),
    Featured nvarchar(50)
)
insert @Activities values
('1/1/2016', 'Marketing', 'John Smith', 'Project XYZ', 'Web - Magazine') 

select
    '0' as '@border',
    '4' as '@cellpadding',
    'font-size:12px; font-family:Arial' as '@style',
    (
        select  (select @style as '@style', 'Activity Date' as '*' for xml path('th'), type),
                (select @style as '@style', 'Lead' as '*' for xml path('th'), type),
                (select @style as '@style', 'Lead By' as '*' for xml path('th'), type),
                (select @style as '@style', 'Activity Title' as '*' for xml path('th'), type),
                (select @style as '@style', 'Featured' as '*' for xml path('th'), type)
        for xml path('tr'), type
    ),
    (
        select 'trclass' as '@class',
               (select StartDate as '*' for xml path('td'), type),
               (select LeadGroup as '*' for xml path('td'), type),
               (select LeadBy as '*' for xml path('td'), type),
               (select Title as '*' for xml path('td'), type),
               (select Featured as '*' for xml path('td'), type)
        from @Activities
        for xml path('tr'), type
    )
for xml path('table')

If you would like the results of that in an nvarchar(max) variable, just select it into it.

declare @tableHtml nvarchar(max)
select @tableHtml = (
    //query from above
)

More questions

More questions with similar tag