May 29, 2011 at 12:25 am
Hi,
I am creating a sql script that will generate a HTML report based on table contents. The below is the query that i designed for creating a HTML report based on the results of a Select Query. The query is working fine but i am facing a single problem in the query. In the result, i am getting the output as shown below.
declare @query nvarchar(max)
set @query =
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<style type=''text/css''>
<!--
table {
border-collapse: collapse;
border: 1px solid #3399FF;
font: 10pt verdana;
color: #343434;
}
table caption { font-weight: bold; background-color: white; }
table td, table th, table caption { border: 1px solid #3399FF; }
table th { background-color: #3399FF; font-weight: bold; }
-->
</style>
<title>Generated table</title>
</head>
<body>
<table>
<caption>Test Data</caption>
<tr><th>Employee Name</th><th>Days PastDue > 60 < 90</th><th>Days PastDue > 90 < 120</th><th>Days PastDue > 120</th></tr>' +
cast(
(select
td = EMPLOYEE_NAME, ' ',
td = Sum(case when [PASTDUE>60<90] is null then '0' else [PASTDUE>60<90] end), ' ',
td = Sum(case when [PASTDUE>90<120] is null then '0' else [PASTDUE>90<120] end), ' ',
td = Sum(case when [PASTDUE>120] is null then '0' else [PASTDUE>120] end)
from db_ITG.dbo.training_report where [PASTDUE>60<90] <> 0
or [PASTDUE>90<120] <> 0 or [PASTDUE>120] <> 0
group by Employee_Name
ORDER by Employee_Name
forxml path('tr'), type ) as nvarchar(max)) +
'</table>
</body>
</html>'
select @query
The above code is working perfectly except with one issue. I have attached the report generated by the above query for reference. Some of the values in the report are displayed bfore the table and not in the appropriate column highlighted in the attached screenshot.
.
The values of two columns are displayed out the table and the place where it has to be displayed by default is showing as empty.
I want all the values to be displayed only inside the table and not outside.
Can you please help me how to overcome this error and can have all the values inside the table itself.
Thanks in advance
Karthik V
May 29, 2011 at 2:24 am
run the query without the html formatting and check if the xml result is what you're looking for. If so, manually add the html format to the xml result.
The error might be caused by some special character inside the generated xml.
May 29, 2011 at 2:54 am
Hi Lutz,
previously i had the coding without HTML Tag and i had the same output. below is my HTML tag
declare @query nvarchar(max)
set @query =
'<style type = ''text/css''>' +
'table { border-collapse:collapse; border:1px solid #3399FF; font:10pt verdana; color:#343434; }' +
'table td, table th, table caption { border:1px solid #3399FF; }' +
'table caption { font-weight:bold; background-color:white }' +
'table th background-color:#3399FF; font-weight:bold; }' +
'</style>' +
'<table>' +
'<caption>ITG Outstanding Training Report Details</caption>' +
'<tr><th>Employee Name</th><th>DAYSPASTDUE > 60 < 90</th><th>DAYSPASTDUE > 90 < 120</th><th>DAYSPASTDUE > 120</th></tr>' +
cast(
(
(select
td = '',
td = EMPLOYEE_NAME,' ',' ',
td = Sum(case when [PASTDUE>60<90] is null then '0' else [PASTDUE>60<90] end),' ',
td = Sum(case when [PASTDUE>90<120] is null then '0' else [PASTDUE>90<120] end),' ',
td = Sum(case when [PASTDUE>120] is null then '0' else [PASTDUE>120] end)
from db_ITG.dbo.training_report where [PASTDUE>60<90] <> 0
or [PASTDUE>90<120] <> 0 or [PASTDUE>120] <> 0
group by Employee_Name
ORDER by Employee_Name
forxml path('tr'), type ) as nvarchar(max)) +
'</table>
select @query
May 29, 2011 at 3:12 am
I've seen the html tag before. No reason to repost it. 😉
I don't think you had "the same" output with and without the html tag...
My question was: what is the XML output without the html formatting? Have a look at it and see if there's the same issue.
If you'd like us to have a look at it, please provide ready to use sample data as described in the first link in my signature.
Without having the table and the data we can't narrow down the issue.
May 29, 2011 at 4:00 am
Hi LutzM
Here is the XML output of the query
select
EMPLOYEE_NAME,
Sum(case when [PASTDUE>60<90] is null then '0' else [PASTDUE>60<90] end),
Sum(case when [PASTDUE>90<120] is null then '0' else [PASTDUE>90<120] end),
Sum(case when [PASTDUE>120] is null then '0' else [PASTDUE>120] end)
from db_ITG.dbo.training_report where [PASTDUE>60<90] <> 0
or [PASTDUE>90<120] <> 0 or [PASTDUE>120] <> 0
group by Employee_Name
ORDER by Employee_Name
FOR XML PATH
Is this what you are speaking about XML Output. As i am a newbie i dont know much about sql server. kindly spare me if there are any mistakes. The table structure is as follows
EMPLOYEE_NAMEvarchar(50),
[PASTDUE>60<90] INT,
[PASTDUE>90<120] INT,
[PASTDUE>120] INT
The columns [PASTDUE>60<90] , [PASTDUE>90<120] and [PASTDUE>120] consists of numbers only.
Let me know how can i provide u sample data.
Thanks,
Karthik
May 29, 2011 at 4:19 am
Please remove the xml sample since it obviously contains personal information (you crrectly obfuscated it in your original screenshot).
I don't think it's the output of the original statement except the html stuff since I can't see td or tr as tag names. I also asked to provide table def and sample data instead of just the xml result.
May 29, 2011 at 6:38 am
Hi LutzM,
Here is the XML Output for .
The HTML output of the query is
The table def is
NameOwnerTypeCreated_datetime
training_reportdbouser table53:55.5
Column_nameTypeComputedLengthPrecScaleNullableTrimTrailingBlanksFixedLenNullInSourceCollation
EMPLOYEE_NAMEvarcharno50 yesnoyesLatin1_General_CI_AI
PASTDUE>60<90intno4100yes(n/a)(n/a)NULL
PASTDUE>90<120intno4100yes(n/a)(n/a)NULL
PASTDUE>120intno4100yes(n/a)(n/a)NULL
IdentitySeedIncrementNot For Replication
No identity column defined.NULLNULLNULL
RowGuidCol
No rowguidcol column defined.
Data_located_on_filegroup
PRIMARY
How can i send you the sample data?
May 29, 2011 at 10:48 am
I copied your result into a html file and it looked just fine. Based on the data I cannot verify the result you see.
What do you use to view the result? Did you try to save it as an html file?
May 29, 2011 at 11:05 am
Hi,
i am sending this HTML output as a web report to email. The query that i use to send email is as follows
exec msdb.dbo.sp_send_dbmail
@profile_name = 'SmarterMail',
@recipients = abc@myemail.com',
@subject = 'ITG Outstanding Training Report Details',
@query = 'exec db_ITG.dbo.sp_sendHTMLemail',
@body_format = 'html',
@query_no_truncate = 1
When i copied it to a HTML file, im also getting the correct result. But when in email the result is showing as the one in screenshot.
May 29, 2011 at 12:04 pm
Did you try to email it as an attached html file?
It seems like it's an issue with the embedded html data of sp_send_dbmail.
At least we exluded the query being a possible cause.
May 29, 2011 at 9:35 pm
hi,
i tried sending the report to gmail and it worked fine. I did not find any data mislocated but i am not able to have the table colors and formattings. I think there are some formatting issues with the mail server that i use locally. due to that i am not able to get the desired output.
I will try to sort it out and will post it for you.
Anyways thanks for helping me out. Because of you I have learnt a new concept of XML output in sql server. 🙂
Thanks,
Karthik V
May 31, 2011 at 3:40 pm
This seems like a lot more work than it should be...is SSRS available in your environment or can it be made available? SSRS is the recommended solution for this type of problem (replaces sp_makewebtask too). SSRS can generate reports from a tabular resultset and those reports can easily be delivered as an HTML email. Don't get me wrong, XML skills are critical, but in this case SSRS would alleviate the need for all the manual Frankensteining issues you're working through 😀
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
May 31, 2011 at 5:22 pm
opc.three (5/31/2011)
(replaces sp_makewebtask too
BWAA-HAAA!!!! In my eyes, there will never be a sufficient replacement for that wonderful tool. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
June 1, 2011 at 9:39 pm
You would have something to say about that...does xp_cmdshell know about your love affair with sp_makewebtask? I bet she'd be jealous 😛
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
June 1, 2011 at 10:29 pm
Ahhhh... you've left out the third member of the team to make it all happen securely; the Master database. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply