For some reasons I am generating HTML code for customers of specific time period and in that code I need Dollar fields's ( 'payed' and 'charges' columns ) text right aligned, how is this possible using XML raw('tr').
create table #Customer
(
id varchar(500),
CustomerName varchar(500),
customertype varchar(500),
LastOrderNo varchar(500),
charges varchar(500),
Payed varchar(500)
)
insert into #Customer
select '201','Cus1','XX','on-09799','60.09$','30.00$'
union all
select '202','Cus2','XX','on-09777','90.09$','50.00$'
union all
select '203','Cus3','YY','on-09766','100.09$','100.00$'
union all
select '204','Cus4','ZZ','on-09788','2000.09$','2000.00$'
Declare @Body varchar(max)
set @Body=''
select @Body =@Body+'<table cellpadding=0 cellspacing=0 border=0 >' +
'<tr><td width="100px" > ID</td>' +
'<td width="140px" >CustomerName</td>' +
'<td width="100px" >Customertype</td>' +
'<td width="100px" > LastOrderNo</td>' +
'<td width="100px" align="right" bgcolor=#E6E6FA><b> charges</b></td>' +
'<td width="110px" align="right" bgcolor=#E6E6FA><b> Payed</b></td></tr>'
select @Body = @Body+(SELECT
td= id,'',
td= CustomerName,'',
td= customertype,'',
td= LastOrderNo,'',
td= charges,'',
td= Payed,''
from #Customer
For XML raw('tr'), Elements
)+'</table>'
select @Body
select * from #Customer
drop table #Customer
I would suggest you try to change the two lines to:
'<td width="100px" style="text-align:right" bgcolor=#E6E6FA><b> charges</b></td>' +
'<td width="110px" style="text-align:right" bgcolor=#E6E6FA><b> Payed</b></td></tr>'
You can further move the background color inside of the style with:
style="text-align:right;background-color:#E6E6FA"
Then you can add 'text-align:right' as 'td/@style'
, right before td= charges,'',
. If this is added right after the SELECT it will align all the cols. Try that.
select @Body = @Body+(SELECT
td= id,'',
td= CustomerName,'',
td= customertype,'',
td= LastOrderNo,'',
[td/@align]='right',
td= charges,'',
[td/@align]='right',
td= Payed,''
from #Customer
For XML path('tr') ---- instead of for xml raw(tr), element
)+'</table>'