Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

SELECT .... FOR XML and data alignment in the table

 
   Database Help (Home) -> XML RSS
Next:  Create XML, first line  
Author Message
John_Doe

External


Since: Jun 12, 2008
Posts: 1



(Msg. 1) Posted: Thu Jun 12, 2008 1:08 pm
Post subject: SELECT .... FOR XML and data alignment in the table
Archived from groups: microsoft>public>sqlserver>xml (more info?)

This is taken from SQL 2005 help from sp_send_dbmail article:

DECLARE @tableHTML NVARCHAR(MAX) ;

SET @tableHTML =
N'<H1>Work Order Report</H1>' +
N'<table border="1">' +
N'<tr><th>Work Order ID</th><th>Product ID</th>' +
N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +
N'<th>Expected Revenue</th></tr>' +
CAST ( ( SELECT td = wo.WorkOrderID, '',
td = p.ProductID, '',
td = p.Name, '',
td = wo.OrderQty, '',
td = wo.DueDate, '',
td = (p.ListPrice - p.StandardCost) * wo.OrderQty
FROM AdventureWorks.Production.WorkOrder as wo
JOIN AdventureWorks.Production.Product AS p
ON wo.ProductID = p.ProductID
WHERE DueDate > '2004-04-30'
AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
ORDER BY DueDate ASC,
(p.ListPrice - p.StandardCost) * wo.OrderQty
DESC
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;

EXEC msdb.dbo.sp_send_dbmail @recipients='danw@Adventure-Works.com',
@subject = 'Work Order List',
@body = @tableHTML,
@body_format = 'HTML' ;

If you replace email address with yours and run this query on
AdventureWorks database you will receive table in the email (assuming
you configured Database mail). All data in the table cells will be
left-aligned.

Does anyone know how to modify the above so that the data is right-
aligned?

 >> Stay informed about: SELECT .... FOR XML and data alignment in the table 
Back to top
Login to vote
Michael Coles

External


Since: May 29, 2008
Posts: 55



(Msg. 2) Posted: Sat Jun 14, 2008 5:25 pm
Post subject: Re: SELECT .... FOR XML and data alignment in the table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You should be able to use the HTML <td> element "align" attribute to get the
desired result:

DECLARE @tableHTML NVARCHAR(MAX) ;

SET @tableHTML =
N'<H1>Work Order Report</H1>' +
N'<table border="1">' +
N'<tr><th>Work Order ID</th><th>Product ID</th>' +
N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +
N'<th>Expected Revenue</th></tr>' +
CAST ( ( SELECT
'right' AS "td/@align",
td = wo.WorkOrderID, '',
'right' AS "td/@align",
td = p.ProductID, '',
'right' AS "td/@align",
td = p.Name, '',
'right' AS "td/@align",
td = wo.OrderQty, '',
'right' AS "td/@align",
td = wo.DueDate, '',
'right' AS "td/@align",
td = (p.ListPrice - p.StandardCost) * wo.OrderQty
FROM AdventureWorks.Production.WorkOrder as wo
JOIN AdventureWorks.Production.Product AS p
ON wo.ProductID = p.ProductID
WHERE DueDate > '2004-04-30'
AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
ORDER BY DueDate ASC,
(p.ListPrice - p.StandardCost) * wo.OrderQty DESC
FOR XML PATH('tr')
) AS NVARCHAR(MAX) ) +
N'</table>' ;

--

========
Michael Coles
"Pro SQL Server 2008 XML"
http://www.amazon.com/Pro-SQL-Server-2008-XML/dp/1590599837/


<John_Doe.TakeThisOut@inbox.ru> wrote in message
news:dd9fa015-3b1a-41a4-a9f3-b507351b35ef@s50g2000hsb.googlegroups.com...
> This is taken from SQL 2005 help from sp_send_dbmail article:
>
> DECLARE @tableHTML NVARCHAR(MAX) ;
>
> SET @tableHTML =
> N'<H1>Work Order Report</H1>' +
> N'<table border="1">' +
> N'<tr><th>Work Order ID</th><th>Product ID</th>' +
> N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +
> N'<th>Expected Revenue</th></tr>' +
> CAST ( ( SELECT td = wo.WorkOrderID, '',
> td = p.ProductID, '',
> td = p.Name, '',
> td = wo.OrderQty, '',
> td = wo.DueDate, '',
> td = (p.ListPrice - p.StandardCost) * wo.OrderQty
> FROM AdventureWorks.Production.WorkOrder as wo
> JOIN AdventureWorks.Production.Product AS p
> ON wo.ProductID = p.ProductID
> WHERE DueDate > '2004-04-30'
> AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
> ORDER BY DueDate ASC,
> (p.ListPrice - p.StandardCost) * wo.OrderQty
> DESC
> FOR XML PATH('tr'), TYPE
> ) AS NVARCHAR(MAX) ) +
> N'</table>' ;
>
> EXEC msdb.dbo.sp_send_dbmail @recipients='danw@Adventure-Works.com',
> @subject = 'Work Order List',
> @body = @tableHTML,
> @body_format = 'HTML' ;
>
> If you replace email address with yours and run this query on
> AdventureWorks database you will receive table in the email (assuming
> you configured Database mail). All data in the table cells will be
> left-aligned.
>
> Does anyone know how to modify the above so that the data is right-
> aligned?

 >> Stay informed about: SELECT .... FOR XML and data alignment in the table 
Back to top
Login to vote
Nikhil B S

External


Since: Jul 25, 2008
Posts: 1



(Msg. 3) Posted: Fri Jul 25, 2008 5:55 pm
Post subject: Re: SELECT .... FOR XML and data alignment in the table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Is there any way to format this so that i can make one of the columns a
hyperlink such that the column data being retrived is a part of the hyperlink?

the idea is that i retive an id and append that id to a url to create the
hyperlink and also display the id in the table.

"Michael Coles" wrote:

> You should be able to use the HTML <td> element "align" attribute to get the
> desired result:
>
> DECLARE @tableHTML NVARCHAR(MAX) ;
>
> SET @tableHTML =
> N'<H1>Work Order Report</H1>' +
> N'<table border="1">' +
> N'<tr><th>Work Order ID</th><th>Product ID</th>' +
> N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +
> N'<th>Expected Revenue</th></tr>' +
> CAST ( ( SELECT
> 'right' AS "td/@align",
> td = wo.WorkOrderID, '',
> 'right' AS "td/@align",
> td = p.ProductID, '',
> 'right' AS "td/@align",
> td = p.Name, '',
> 'right' AS "td/@align",
> td = wo.OrderQty, '',
> 'right' AS "td/@align",
> td = wo.DueDate, '',
> 'right' AS "td/@align",
> td = (p.ListPrice - p.StandardCost) * wo.OrderQty
> FROM AdventureWorks.Production.WorkOrder as wo
> JOIN AdventureWorks.Production.Product AS p
> ON wo.ProductID = p.ProductID
> WHERE DueDate > '2004-04-30'
> AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
> ORDER BY DueDate ASC,
> (p.ListPrice - p.StandardCost) * wo.OrderQty DESC
> FOR XML PATH('tr')
> ) AS NVARCHAR(MAX) ) +
> N'</table>' ;
>
> --
>
> ========
> Michael Coles
> "Pro SQL Server 2008 XML"
> http://www.amazon.com/Pro-SQL-Server-2008-XML/dp/1590599837/
>
>
> <John_Doe.RemoveThis@inbox.ru> wrote in message
> news:dd9fa015-3b1a-41a4-a9f3-b507351b35ef@s50g2000hsb.googlegroups.com...
> > This is taken from SQL 2005 help from sp_send_dbmail article:
> >
> > DECLARE @tableHTML NVARCHAR(MAX) ;
> >
> > SET @tableHTML =
> > N'<H1>Work Order Report</H1>' +
> > N'<table border="1">' +
> > N'<tr><th>Work Order ID</th><th>Product ID</th>' +
> > N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +
> > N'<th>Expected Revenue</th></tr>' +
> > CAST ( ( SELECT td = wo.WorkOrderID, '',
> > td = p.ProductID, '',
> > td = p.Name, '',
> > td = wo.OrderQty, '',
> > td = wo.DueDate, '',
> > td = (p.ListPrice - p.StandardCost) * wo.OrderQty
> > FROM AdventureWorks.Production.WorkOrder as wo
> > JOIN AdventureWorks.Production.Product AS p
> > ON wo.ProductID = p.ProductID
> > WHERE DueDate > '2004-04-30'
> > AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
> > ORDER BY DueDate ASC,
> > (p.ListPrice - p.StandardCost) * wo.OrderQty
> > DESC
> > FOR XML PATH('tr'), TYPE
> > ) AS NVARCHAR(MAX) ) +
> > N'</table>' ;
> >
> > EXEC msdb.dbo.sp_send_dbmail @recipients='danw@Adventure-Works.com',
> > @subject = 'Work Order List',
> > @body = @tableHTML,
> > @body_format = 'HTML' ;
> >
> > If you replace email address with yours and run this query on
> > AdventureWorks database you will receive table in the email (assuming
> > you configured Database mail). All data in the table cells will be
> > left-aligned.
> >
> > Does anyone know how to modify the above so that the data is right-
> > aligned?
>
>
>
 >> Stay informed about: SELECT .... FOR XML and data alignment in the table 
Back to top
Login to vote
Michael Coles

External


Since: May 29, 2008
Posts: 55



(Msg. 4) Posted: Sun Aug 10, 2008 12:40 am
Post subject: Re: SELECT .... FOR XML and data alignment in the table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

There's no "hyperlink" data type in SQL Server. Hyperlinks are a
client-side construct, more specifically they are an HTML concept. You can
use simple string concatenation to create an HTML tag like the following on
SQL Server (change the URL to match your needs):

'<A HREF="http://www.myurl.com?id=' + CAST(id AS varchar(20)) +
'">' + CAST(id AS varchar(20)) + '</A>';

Or you could do this on the client side (preferred method, if possible).
The end result is an HTML tag that looks like this:

<A HREF="http://www.myurl.com?id=1234">1234</A>

You can position this tag in your client web page to create a clickable
hyperlink.

--

========
Michael Coles
"Pro T-SQL 2008 Programmer's Guide"
http://www.amazon.com/T-SQL-2008-Programmer-rsquo-Guide/dp/143021001X


"Nikhil B S" <Nikhil B S.TakeThisOut@discussions.microsoft.com> wrote in message
news:84F9B575-198D-4EFA-8B92-9BC9D4F6FB80@microsoft.com...
> Is there any way to format this so that i can make one of the columns a
> hyperlink such that the column data being retrived is a part of the
> hyperlink?
>
> the idea is that i retive an id and append that id to a url to create the
> hyperlink and also display the id in the table.
>
> "Michael Coles" wrote:
>
>> You should be able to use the HTML <td> element "align" attribute to get
>> the
>> desired result:
>>
>> DECLARE @tableHTML NVARCHAR(MAX) ;
>>
>> SET @tableHTML =
>> N'<H1>Work Order Report</H1>' +
>> N'<table border="1">' +
>> N'<tr><th>Work Order ID</th><th>Product ID</th>' +
>> N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +
>> N'<th>Expected Revenue</th></tr>' +
>> CAST ( ( SELECT
>> 'right' AS "td/@align",
>> td = wo.WorkOrderID, '',
>> 'right' AS "td/@align",
>> td = p.ProductID, '',
>> 'right' AS "td/@align",
>> td = p.Name, '',
>> 'right' AS "td/@align",
>> td = wo.OrderQty, '',
>> 'right' AS "td/@align",
>> td = wo.DueDate, '',
>> 'right' AS "td/@align",
>> td = (p.ListPrice - p.StandardCost) * wo.OrderQty
>> FROM AdventureWorks.Production.WorkOrder as wo
>> JOIN AdventureWorks.Production.Product AS p
>> ON wo.ProductID = p.ProductID
>> WHERE DueDate > '2004-04-30'
>> AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
>> ORDER BY DueDate ASC,
>> (p.ListPrice - p.StandardCost) * wo.OrderQty DESC
>> FOR XML PATH('tr')
>> ) AS NVARCHAR(MAX) ) +
>> N'</table>' ;
>>
>> --
>>
>> ========
>> Michael Coles
>> "Pro SQL Server 2008 XML"
>> http://www.amazon.com/Pro-SQL-Server-2008-XML/dp/1590599837/
>>
>>
>> <John_Doe.TakeThisOut@inbox.ru> wrote in message
>> news:dd9fa015-3b1a-41a4-a9f3-b507351b35ef@s50g2000hsb.googlegroups.com...
>> > This is taken from SQL 2005 help from sp_send_dbmail article:
>> >
>> > DECLARE @tableHTML NVARCHAR(MAX) ;
>> >
>> > SET @tableHTML =
>> > N'<H1>Work Order Report</H1>' +
>> > N'<table border="1">' +
>> > N'<tr><th>Work Order ID</th><th>Product ID</th>' +
>> > N'<th>Name</th><th>Order Qty</th><th>Due Date</th>' +
>> > N'<th>Expected Revenue</th></tr>' +
>> > CAST ( ( SELECT td = wo.WorkOrderID, '',
>> > td = p.ProductID, '',
>> > td = p.Name, '',
>> > td = wo.OrderQty, '',
>> > td = wo.DueDate, '',
>> > td = (p.ListPrice - p.StandardCost) * wo.OrderQty
>> > FROM AdventureWorks.Production.WorkOrder as wo
>> > JOIN AdventureWorks.Production.Product AS p
>> > ON wo.ProductID = p.ProductID
>> > WHERE DueDate > '2004-04-30'
>> > AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
>> > ORDER BY DueDate ASC,
>> > (p.ListPrice - p.StandardCost) * wo.OrderQty
>> > DESC
>> > FOR XML PATH('tr'), TYPE
>> > ) AS NVARCHAR(MAX) ) +
>> > N'</table>' ;
>> >
>> > EXEC msdb.dbo.sp_send_dbmail @recipients='danw@Adventure-Works.com',
>> > @subject = 'Work Order List',
>> > @body = @tableHTML,
>> > @body_format = 'HTML' ;
>> >
>> > If you replace email address with yours and run this query on
>> > AdventureWorks database you will receive table in the email (assuming
>> > you configured Database mail). All data in the table cells will be
>> > left-aligned.
>> >
>> > Does anyone know how to modify the above so that the data is right-
>> > aligned?
>>
>>
>>
 >> Stay informed about: SELECT .... FOR XML and data alignment in the table 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Get data out of a table using a schema through select stat.. - Hello everyone, I need to get the data out of multiple sql table joins in the form of xml using the schema. Is there a way to do that in the select statement. -- Shabnam Gupta

XML data into relational table - Hi all! I have an XML like this: <?xml version="1.0" ?> <actions> <action code="ABC1234" number="1"> <group value="2"> <subgroup value="5"> <d value=...

Extract Data From A Table & Save As An XML File - Hello Everyone, I am trying to extract data from from my Hit list and save as a file in XML format. I create a stored proc called it as stProblemClients I wrote: Select * from clients where status = 'Not Dead' I set a schedule stProblemClients to run....

Help me with this SELECT FROM OPENXML - The following script doesnt return any records. Why ? --------------------------------------------------------- declare @t varchar(1000) DECLARE @XMLDocPointer1 INT EXEC sp_xml_preparedocument @XMLDocPointer1 OUTPUT, @t set @t = '<EMP><EE EID...

How to select an attribute value from an XML-column using .. - Hi, I have stored some XML data in a XML-column in one of my tables. It looks something like this: <Parameters> <Parameter Name="View" Value="A" /> <Parameter Name="@HistDate" Value="2007-02-12&qu...
   Database Help (Home) -> XML All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]