' + N' < th>Erro...">

SQL query against HTML table

I have a query in T-SQL

DECLARE @body NVARCHAR(MAX) SET @body = N'< table style="width:80%">'
+ N'<tr>
    < th>ErrorID< /th>
    < th>Status< /th>
    < th>Count< /th>        
    < /tr>'
+ CAST((
    SELECT
       [ErrorID] AS td
      ,[Status] AS td
      ,[Count]  AS td
FROM [Datbase].[dbo].[Table]
    FOR XML RAW('tr'), ELEMENTS
) AS NVARCHAR(MAX))
+ N'< /table>'

      

Output example:

<tr><th>ErrorID</th><th>Status</th>< th>Count</th></tr>
<tr><td>1</td><td>Pending</td><td>2</td></tr>
<tr><td>1</td><td>Pending</td><td>3</td></tr>
<tr><td>1</td><td>Pending</td><td>11</td></tr

      

Is it possible that there will be a conditional statement that if [Count]> 10 <td>

, and the whole line becomes <td style="color:red">

? TIA.

+3


source to share


2 answers


You can make it easier with css. See example below.



.wrapper div:nth-child(n+11) {
   color: red
}
      

<div class="wrapper">
    <div class="container">1</div>
    <div class="container">2</div>
    <div class="container">3</div>
    <div class="container">4</div>
    <div class="container">5</div>
    <div class="container">6</div>
    <div class="container">7</div>
    <div class="container">8</div>
    <div class="container">9</div>
    <div class="container">10</div>
    <div class="container">11</div>
    <div class="container">12</div>
</div>
      

Run codeHide result


0


source


using TSQL ROW_NUMBER () function, you can include in your select statement a new column filled with ROW_NUMBER function. now you can use the new column to determine what to do when the value is greater than 10 as shown below.

SELECT
   [ErrorID] 
  ,[Status] 
  ,[Count] ,ROW_NUMBER() OVER( ORDER BY fieldOfChoice) AS ROWNUMBER 

      



FROM [Datbase]. [dbo]. [Table]

However, using css as shown below is a more elegant solution

0


source







All Articles