Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Apply Style in Select Query Expand / Collapse
Author
Message
Posted Thursday, October 4, 2012 10:48 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, March 6, 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

Post #1368814
Posted Friday, October 5, 2012 7:43 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:54 PM
Points: 13,471, Visits: 12,329
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
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1369044
Posted Friday, October 5, 2012 9:45 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:57 PM
Points: 12,917, Visits: 32,083
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
Post #1369154
Posted Saturday, October 6, 2012 12:21 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, March 6, 2013 11:48 PM
Points: 66, Visits: 160
Worked for me thanks!
Post #1369388
Posted Saturday, October 6, 2012 8:48 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 1:10 AM
Points: 1,787, Visits: 5,724
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


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1369431
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse