SQL-使用XML路径正确格式化CSS类

SQL-使用XML路径正确格式化CSS类,sql,sql-server,Sql,Sql Server,我试图通过使用XML路径语法从SQL查询中添加一些CSS类来格式化HTML表 我试图应用上的答案:但我不知道我做错了什么: DECLARE @tableHTML NVARCHAR(MAX) ; SET @tableHTML = N'<table border="0" cellpadding="4" style="font-size:12px; font-family:Arial">' + N'<tr>' + N'<th style="border-b

我试图通过使用XML路径语法从SQL查询中添加一些CSS类来格式化HTML表

我试图应用上的答案:但我不知道我做错了什么:

DECLARE @tableHTML  NVARCHAR(MAX) ; 
SET @tableHTML =   
N'<table border="0" cellpadding="4" style="font-size:12px; font-family:Arial">' +  
N'<tr>' + 
N'<th style="border-bottom:1px solid #7AC0DA">Activity Date</th>' + 
N'<th style="border-bottom:1px solid #7AC0DA">Lead</th>' + 
N'<th style="border-bottom:1px solid #7AC0DA">Lead By</th>' +  
N'<th style="border-bottom:1px solid #7AC0DA">Activity Title</th>' +  
N'<th style="border-bottom:1px solid #7AC0DA">Featured On</th>' + 
CAST ( (SELECT 'trclass' as [@class],
          (SELECT StartDate as [*], 'tdclass' as [@class] FOR XML PATH('td'), type),
          (SELECT LeadGroup as [*], 'tdclass' as [@class] FOR XML PATH('td'), type),
          (SELECT LeadBy as [*], 'tdclass' as [@class] FOR XML PATH('td'), type),
          (SELECT Title as [*], 'tdclass' as [@class] FOR XML PATH('td'), type),
          (SELECT Featured as [*], 'tdclass' as [@class] FOR XML PATH('td'), type)
        FROM @Activites ORDER BY [StartDate],[LeadGroup],[LeadBy]
        FOR XML PATH('tr') , TYPE   
) AS NVARCHAR(MAX) ) +  
N'</table>';
DECLARE@tableHTML-NVARCHAR(最大值);
SET@tableHTML=
N''+
N''+
N‘活动日期’+
N'Lead'+
以“+
N‘活动名称’+
N'在'+
强制转换((选择“trclass”作为[@class],
(选择StartDate作为[*],选择'tdclass'作为[@class]作为XML路径('td'),类型),
(选择LeadGroup作为[*],选择'tdclass'作为[@class]作为XML路径('td'),类型),
(选择LeadBy作为[*],选择'tdclass'作为[@class]作为XML路径('td'),类型),
(对于XML路径('td'),选择标题为[*],'tdclass'为[@class],键入),
(对于XML路径('td'),选择特性为[*],选择'tdclass'作为[@class],键入)
来自@Activites ORDER BY[StartDate]、[LeadGroup]、[LeadBy]
对于XML路径('tr'),键入
)作为NVARCHAR(最大值))+
N';
我希望有这样的输出:

<table>
    <tr>
        <th style="border-bottom:1px solid #7AC0DA">Activity Date</th>
        <th style="border-bottom:1px solid #7AC0DA">Lead</th>
        <th style="border-bottom:1px solid #7AC0DA">Lead By</th>
        <th style="border-bottom:1px solid #7AC0DA">Activity Title</th>
        <th style="border-bottom:1px solid #7AC0DA">Featured</th>
    <tr>
    <tr clas="trclass">
        <td clas="tdclass">2016-01-01</td>
        <td clas="tdclass">Marketing</td>
        <td clas="tdclass">John Smith</td>
        <td clas="tdclass">Project XYZ</td>
        <td clas="tdclass">Web - Magazine</td>
    </tr>
</table>

活动日期
领导
领导
活动名称
作为特色的
2016-01-01
营销
约翰·史密斯
XYZ项目
网络杂志
但是,我在执行查询时遇到以下错误:

以属性为中心的列“@style”不能位于 FOR XML PATH中XML层次结构中的非以属性为中心的同级


我做错了什么?我必须说,我对XML路径的工作原理有点困惑。谢谢

您的错误表示您在某个地方选择了希望成为属性的内容(错误表示“样式”)之后,您选择的内容可能不是属性(可能是您的一个表标题)<代码>对于XML路径不允许这样做,您必须首先选择属性。但是,我看不到您在代码中选择
@style
的位置

我已经修改了您的查询,以选择下面所需格式的XML

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')
如果您希望在nvarchar(max)变量中显示结果,只需将其选中即可

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

哦这使它更容易阅读,现在对我来说也很有意义。谢谢!