|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Wednesday, March 06, 2013 11:48 PM
Points: 66,
Visits: 160
|
|
Hi, I am using FOR XML to generate the HTML and following is my query. i want to apply class in td, how can i achieve the same ??
CAST ( ( SELECT td=Row_Number() over( Order by TaskIndex),'', td = [ProjectServer_NPD_Reporting].dbo.Fn_Task_Wbs(P.Projectuid,convert(char,TaskWBS)), '' , td=[TaskName],'', td = CONVERT(VARCHAR,[TaskStartDate],6), '' , td = CONVERT(VARCHAR,[TaskFinishDate],6), '', td = CONVERT(VARCHAR,[TaskPercentCompleted])+'% Complete', '' from
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Today @ 3:12 PM
Points: 8,957,
Visits: 8,523
|
|
parth83.rawal (10/4/2012)
Hi, I am using FOR XML to generate the HTML and following is my query. i want to apply class in td, how can i achieve the same ?? CAST ( ( SELECT td=Row_Number() over( Order by TaskIndex),'', td = [ProjectServer_NPD_Reporting].dbo.Fn_Task_Wbs(P.Projectuid,convert(char,TaskWBS)), '' , td=[TaskName],'', td = CONVERT(VARCHAR,[TaskStartDate],6), '' , td = CONVERT(VARCHAR,[TaskFinishDate],6), '', td = CONVERT(VARCHAR,[TaskPercentCompleted])+'% Complete', '' from
Can you post the whole query?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Moden's splitter.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 4:22 PM
Points: 11,789,
Visits: 28,063
|
|
does this example, where I'm applying some styles to the results help? note i have to do a find/replace to the string to unescape some characters;
DECLARE @HTMLBody varchar(max) Select @HTMLBody = (Select Row_Number() Over(Order By is_linked, name) % 2 As [TRRow], name As [TD], product As [TD], provider As [TD], data_source As [TD style="width: 210px;min-width: 100px"], is_linked As [TD align="center"] From sys.servers Order By is_linked, name For XML raw('tr'), Elements) Set @HTMLBody = Replace(@HTMLBody, '_x0020_', space(1)) Set @HTMLBody= Replace(@HTMLBody,'_x0022_','"') Set @HTMLBody= Replace(@HTMLBody,'_x003B_',';') Set @HTMLBody = Replace(@HTMLBody, '_x003D_', '=') Set @HTMLBody = Replace(@HTMLBody, '<tr><TRRow>1</TRRow>', '<tr bgcolor="#C6CFFF">') Set @HTMLBody = Replace(@HTMLBody, '<TRRow>0</TRRow>', '') Select '<table>' + @HTMLBody + '</table>'
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Wednesday, March 06, 2013 11:48 PM
Points: 66,
Visits: 160
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 4:54 PM
Points: 1,333,
Visits: 4,015
|
|
parth83.rawal (10/6/2012) Worked for me thanks!
Please also consider this - it is a lot faster and neater IMHO...
SELECT '#C6CFFF' AS [@bgcolor], s.name As [TD], '' AS [*], -- Blank, just here to prevent element merging product As [TD], '' AS [*], provider As [TD], '' AS [*], 'width: 210px;min-width: 100px' AS [TD/@style], data_source As [TD], '' AS [*], 'center' AS [TD/@align], is_linked As [TD] FROM sys.servers s,syscolumns ORDER BY is_linked, s.name FOR XML PATH('TR'),ROOT('TABLE')
I know this looks odd, but that is just because we are using XML to craft HTML. The main difference for this query being that HTML commonly has repeated elements (The TD elements), where XML doesn't tend to.
What this means for this query is that we need to find a way round the in-built functionality in FOR XML PATH that merges consecutive elements that have the same TAG. The way to stop this is either to alternate between upper and lower case TDs, OR as in this example, to use a blank to break the rule.
Generally, here we have a ROOT('TABLE') for the whole query, a row tag - FOR XML PATH('TR') and we have attributes, which come at the start of the SELECT for row attributes, but come before each individual TAG.
I think, in general you will find this method to be faster, neater and easier to produce the correct output.
For comparison, here is the output from Lowell's method
<table> <tr bgcolor="#C6CFFF"> <TD>COMP1</TD> <TD>SQL Server</TD> <TD>SQLNCLI</TD> <TD style="width: 210px;min-width: 100px">COMP1</TD style="width: 210px;min-width: 100px"> <TD align="center">0</TD align="center"> </tr> </table>
And the output from this method
<TABLE> <TR bgcolor="#C6CFFF"> <TD>COMP1</TD> <TD>SQL Server</TD> <TD>SQLNCLI</TD> <TD style="width: 210px;min-width: 100px">COMP1</TD> <TD align="center">0</TD> </TR> </TABLE>
MM
|
|
|
|