Showing posts with label clause. Show all posts
Showing posts with label clause. Show all posts

Monday, March 12, 2012

for xml raw problem

Hi
I'm running a simple query with for xml raw on a table
one of columns in select clause here is of type text and stores xml
file itself.
The problem i'm facing is when I get this text back from this column I
get back unformatted xml
e.g
instead of getting <GPF> I get <GPF> etc
How do I get this column as formatted xml text ?
Thanks for help
Vishy>
> I'm running a simple query with for xml raw on a table
> one of columns in select clause here is of type text and stores xml
> file itself.
> The problem i'm facing is when I get this text back from this column I
> get back unformatted xml
> e.g
> instead of getting <GPF> I get <GPF> etc
> How do I get this column as formatted xml text ?
>
Hello
signs '<' and '>' are very important in XML because they show begin and end
of xml element, so when an element or attribute value has such signs it must
be changed into < and >. But when you open XML which contains such
substitution - using DOM or OPENXML in SQL you will get values with signs
'<', '>'. So I think that it sould not be a problem - siply try to process
XML which was generated and you will see that it has valid values of
elements/attributes.
I hope it helps,
Alwik|||Thanks Alwik for reply
I tried to create object on XmlDocument using LoadXml() method
it loads up the xml successfully but if I want to run any Xpath; it
fails
as <GPF> is not treated as <GPF> so I can't find GPF element
is there any work around?
Vishy|||Are you using SQL Server 2000 or 2005?
In the case of SQL Server 2000, you will have to use the explicit mode and
its !xml directive, if you want to have string data be inlined into the XML
structure.
In the case of SQL Server 2005, you can stick with the FOR XML RAW query,
but you need to cast the string data to XML in the select clause.
Best regards
Michael
"Vishy" <vishal.halbe@.gmail.com> wrote in message
news:1128445808.595093.141180@.g49g2000cwa.googlegroups.com...
> Thanks Alwik for reply
> I tried to create object on XmlDocument using LoadXml() method
> it loads up the xml successfully but if I want to run any Xpath; it
> fails
> as <GPF> is not treated as <GPF> so I can't find GPF element
> is there any work around?
> Vishy
>|||Thanks Michael for reply
I'm using SQL 2000
Vishy

for xml raw problem

Hi
I'm running a simple query with for xml raw on a table
one of columns in select clause here is of type text and stores xml
file itself.
The problem i'm facing is when I get this text back from this column I
get back unformatted xml
e.g
instead of getting <GPF> I get <GPF> etc
How do I get this column as formatted xml text ?
Thanks for help
Vishy
>
> I'm running a simple query with for xml raw on a table
> one of columns in select clause here is of type text and stores xml
> file itself.
> The problem i'm facing is when I get this text back from this column I
> get back unformatted xml
> e.g
> instead of getting <GPF> I get <GPF> etc
> How do I get this column as formatted xml text ?
>
Hello
signs '<' and '>' are very important in XML because they show begin and end
of xml element, so when an element or attribute value has such signs it must
be changed into < and >. But when you open XML which contains such
substitution - using DOM or OPENXML in SQL you will get values with signs
'<', '>'. So I think that it sould not be a problem - siply try to process
XML which was generated and you will see that it has valid values of
elements/attributes.
I hope it helps,
Alwik
|||Thanks Alwik for reply
I tried to create object on XmlDocument using LoadXml() method
it loads up the xml successfully but if I want to run any Xpath; it
fails
as <GPF> is not treated as <GPF> so I can't find GPF element
is there any work around?
Vishy
|||Are you using SQL Server 2000 or 2005?
In the case of SQL Server 2000, you will have to use the explicit mode and
its !xml directive, if you want to have string data be inlined into the XML
structure.
In the case of SQL Server 2005, you can stick with the FOR XML RAW query,
but you need to cast the string data to XML in the select clause.
Best regards
Michael
"Vishy" <vishal.halbe@.gmail.com> wrote in message
news:1128445808.595093.141180@.g49g2000cwa.googlegr oups.com...
> Thanks Alwik for reply
> I tried to create object on XmlDocument using LoadXml() method
> it loads up the xml successfully but if I want to run any Xpath; it
> fails
> as <GPF> is not treated as <GPF> so I can't find GPF element
> is there any work around?
> Vishy
>
|||Thanks Michael for reply
I'm using SQL 2000
Vishy

Friday, March 9, 2012

For XML Fields Missing

SQL Server 2000 Enterprise Edition SP3a

Windows 2003 Enterprise Edition

I am trying to export a subset of a table to XML (using the where clause) and I am not getting all fields exported. I have tried using Auto, Raw, and Explicit and they all omit these fields. Here is my schema:

CREATE TABLE [dbo].[TableName] (
[TableID] [uniqueidentifier] NOT NULL ,
[Field01] [char] (6) NOT NULL ,
[Field02] [char] (4) NOT NULL ,
[Field03] [int] NOT NULL ,
[Field04] [varchar] (50) NOT NULL ,
[Field05] [char] (4) NULL ,
[Field06] [smalldatetime] NOT NULL ,
[Field07] [smallint] NULL ,
[Field08] [tinyint] NULL ,
[Field09] [tinyint] NULL ,
[Field10] [tinyint] NULL ,
[Field11] [smalldatetime] NULL ,
[Field12] [smalldatetime] NULL ,
[Field13] [money] NULL ,
[Field14] [money] NULL ,
[Field15] [smalldatetime] NULL ,
[Field16] [varchar] (50) ,
[Field17] [money] NULL ,
[Field18] [money] NULL ,
[Field19] [bit] NULL
) ON [PRIMARY]

When the XML is generated, I am not getting Field07, Field08, Field09, Field10, Field12, Field13, Field14, Field15, and Field16.

Here is my query:

CREATE PROCEDURE dbasp_get_TableData

(@.TableID uniqueidentifier) AS

SELECT
TableID, Field01, Field02, Field03, Field04, Field05, Field06, Field07,
Field08, Field09, Field10, Field11, Field12,
Field13, Field14, Field15, Field16,
Field17, Field18, Field19

FROM database.dbo.TableName

WHERE tableid = @.TableID

FOR XML AUTO, ELEMENTS

I still get the same problem if I do a Select * statement. All fields have data, so NULL cannot be an issue.

Please help!!! It does not make sense.

Thank you in advance.

I couldn't repro your problem. Following is my repro and result. Please provide your repro script and the version of your SQL 2000.

use tempdb
go

CREATE TABLE [dbo].[TableName] (
[TableID] [uniqueidentifier] NOT NULL ,
[Field01] [char] (6) NOT NULL ,
[Field02] [char] (4) NOT NULL ,
[Field03] [int] NOT NULL ,
[Field04] [varchar] (50) NOT NULL ,
[Field05] [char] (4) NULL ,
[Field06] [smalldatetime] NOT NULL ,
[Field07] [smallint] NULL ,
[Field08] [tinyint] NULL ,
[Field09] [tinyint] NULL ,
[Field10] [tinyint] NULL ,
[Field11] [smalldatetime] NULL ,
[Field12] [smalldatetime] NULL ,
[Field13] [money] NULL ,
[Field14] [money] NULL ,
[Field15] [smalldatetime] NULL ,
[Field16] [varchar] (50) ,
[Field17] [money] NULL ,
[Field18] [money] NULL ,
[Field19] [bit] NULL
) ON [PRIMARY]
go

insert TableName values(newid(), 'abc', 'def', 3, '04', '05', getdate(), 7, 8, 9, 10, getdate(), getdate(), 13.1, 14.5, getdate(), '16varchar', 17.3, 18.5, 1)
go

CREATE PROCEDURE dbasp_get_TableData

(@.TableID uniqueidentifier) AS

SELECT
TableID, Field01, Field02, Field03, Field04, Field05, Field06, Field07,
Field08, Field09, Field10, Field11, Field12,
Field13, Field14, Field15, Field16,
Field17, Field18, Field19

FROM tempdb.dbo.TableName

WHERE tableid = @.TableID

FOR XML AUTO, ELEMENTS
go

exec dbasp_get_TableData '9883A32F-3EF4-408F-BFEC-6540598FEBC1'

Result I get:

==================

<tempdb.dbo.TableName><TableID>9883A32F-3EF4-408F-BFEC-6540598FEBC1</TableID><Field01>abc </Field01><Field02>def </Field02><Field03>3</Field03><Field04>04</Field04><Field05>05 </Field05><Field06>2007-01-16T10:21:00</Field06><Field07>7</Field07><Field08>8</Field08><Field09>9</Field09><Field10>10</Field10><Field11>2007-01-16T10:21:00</Field11><Field12>2007-01-16T10:21:00</Field12><Field13>13.1000</Field13><Field14>14.5000</Field14><Field15>2007-01-16T10:21:00</Field15><Field16>16varchar</Field16><Field17>17.3000</Field17><Field18>18.5000</Field18><Field19>1</Field19></tempdb.dbo.TableName>

|||I did find where the problem was coming from. I reproduced the same result as your example. Somehow I had changed the GUID that was used when I knew that all fields were accounted for (not null) to a GUID that had several NULL values. Those fields don't get exported (not even the field names) when it is null. I have been able to get my code to work by utilizing ISNULL. Thank you for your time in helping me.

For XML Fields Missing

SQL Server 2000 Enterprise Edition SP3a

Windows 2003 Enterprise Edition

I am trying to export a subset of a table to XML (using the where clause) and I am not getting all fields exported. I have tried using Auto, Raw, and Explicit and they all omit these fields. Here is my schema:

CREATE TABLE [dbo].[TableName] (
[TableID] [uniqueidentifier] NOT NULL ,
[Field01] [char] (6) NOT NULL ,
[Field02] [char] (4) NOT NULL ,
[Field03] [int] NOT NULL ,
[Field04] [varchar] (50) NOT NULL ,
[Field05] [char] (4) NULL ,
[Field06] [smalldatetime] NOT NULL ,
[Field07] [smallint] NULL ,
[Field08] [tinyint] NULL ,
[Field09] [tinyint] NULL ,
[Field10] [tinyint] NULL ,
[Field11] [smalldatetime] NULL ,
[Field12] [smalldatetime] NULL ,
[Field13] [money] NULL ,
[Field14] [money] NULL ,
[Field15] [smalldatetime] NULL ,
[Field16] [varchar] (50) ,
[Field17] [money] NULL ,
[Field18] [money] NULL ,
[Field19] [bit] NULL
) ON [PRIMARY]

When the XML is generated, I am not getting Field07, Field08, Field09, Field10, Field12, Field13, Field14, Field15, and Field16.

Here is my query:

CREATE PROCEDURE dbasp_get_TableData

(@.TableID uniqueidentifier) AS

SELECT
TableID, Field01, Field02, Field03, Field04, Field05, Field06, Field07,
Field08, Field09, Field10, Field11, Field12,
Field13, Field14, Field15, Field16,
Field17, Field18, Field19

FROM database.dbo.TableName

WHERE tableid = @.TableID

FOR XML AUTO, ELEMENTS

I still get the same problem if I do a Select * statement. All fields have data, so NULL cannot be an issue.

Please help!!! It does not make sense.

Thank you in advance.

I couldn't repro your problem. Following is my repro and result. Please provide your repro script and the version of your SQL 2000.

use tempdb
go

CREATE TABLE [dbo].[TableName] (
[TableID] [uniqueidentifier] NOT NULL ,
[Field01] [char] (6) NOT NULL ,
[Field02] [char] (4) NOT NULL ,
[Field03] [int] NOT NULL ,
[Field04] [varchar] (50) NOT NULL ,
[Field05] [char] (4) NULL ,
[Field06] [smalldatetime] NOT NULL ,
[Field07] [smallint] NULL ,
[Field08] [tinyint] NULL ,
[Field09] [tinyint] NULL ,
[Field10] [tinyint] NULL ,
[Field11] [smalldatetime] NULL ,
[Field12] [smalldatetime] NULL ,
[Field13] [money] NULL ,
[Field14] [money] NULL ,
[Field15] [smalldatetime] NULL ,
[Field16] [varchar] (50) ,
[Field17] [money] NULL ,
[Field18] [money] NULL ,
[Field19] [bit] NULL
) ON [PRIMARY]
go

insert TableName values(newid(), 'abc', 'def', 3, '04', '05', getdate(), 7, 8, 9, 10, getdate(), getdate(), 13.1, 14.5, getdate(), '16varchar', 17.3, 18.5, 1)
go

CREATE PROCEDURE dbasp_get_TableData

(@.TableID uniqueidentifier) AS

SELECT
TableID, Field01, Field02, Field03, Field04, Field05, Field06, Field07,
Field08, Field09, Field10, Field11, Field12,
Field13, Field14, Field15, Field16,
Field17, Field18, Field19

FROM tempdb.dbo.TableName

WHERE tableid = @.TableID

FOR XML AUTO, ELEMENTS
go

exec dbasp_get_TableData '9883A32F-3EF4-408F-BFEC-6540598FEBC1'

Result I get:

==================

<tempdb.dbo.TableName><TableID>9883A32F-3EF4-408F-BFEC-6540598FEBC1</TableID><Field01>abc </Field01><Field02>def </Field02><Field03>3</Field03><Field04>04</Field04><Field05>05 </Field05><Field06>2007-01-16T10:21:00</Field06><Field07>7</Field07><Field08>8</Field08><Field09>9</Field09><Field10>10</Field10><Field11>2007-01-16T10:21:00</Field11><Field12>2007-01-16T10:21:00</Field12><Field13>13.1000</Field13><Field14>14.5000</Field14><Field15>2007-01-16T10:21:00</Field15><Field16>16varchar</Field16><Field17>17.3000</Field17><Field18>18.5000</Field18><Field19>1</Field19></tempdb.dbo.TableName>

|||I did find where the problem was coming from. I reproduced the same result as your example. Somehow I had changed the GUID that was used when I knew that all fields were accounted for (not null) to a GUID that had several NULL values. Those fields don't get exported (not even the field names) when it is null. I have been able to get my code to work by utilizing ISNULL. Thank you for your time in helping me.

Wednesday, March 7, 2012

FOR XML EXPLICIT - Empty Tags?

Hey all.
I need to output a query in a given XML format and I figure I'd have a stab
using the FOR XML clause of SQL 2000 first rather than in code.
I've worked out the basics using the FOR XML EXPLICIT clause and am able to
output a structure such as this:
<state id="SA">
<property id="5">
<name>Prop1</name>
<area>35</area>
</property>
<property id="10">
<name>Prop2</name>
<area>55</area>
</property>
</state>
<state id="NSW">
<property id="24">
...
</state>
However I haven't worked out how to add 'empty' surrounding tags (I think
they may be called 'associations' in XML speak), so the schema would become:
(note the addition of the <states> and <properties> tags)
<states>
<state id="SA">
<properties>
<property id="5">
<name>Prop1</name>
<area>35</area>
</property>
<property id="10">
<name>Prop2</name>
<area>55</area>
</property>
</properties>
</state>
<state id="NSW">
<properties>
<property id="24">
...
</properties>
</state>
</states>
(apologies if the formatting doesn't stick)
Any ideas? Anyone familiar with using XML in SS?
For reference, my actual current query is below - I figured the above
example was easier to use.
SELECT
1 as Tag,
NULL as Parent,
c.textstate as [state!1!idstate],
null as [property!2!name!element],
null as [property!2!areaHA!element],
null as [property!2!dateGranted!element],
null as
[property!2!titleHoldingBody!element],
null as [property!2!idproperty]
FROM
dbo.tblStates c
Where
c.IDState<>0
union all
SELECT
2,
1,
b.textstate,
a.ShortLandName,
a.area,
a.GrantDate,
a.THBName,
a.IDProperty
FROM
dbo.tblStates b, dbo.vw_LandPurchases_WebsiteExport_Prop a where
a.IDState = b.IDState
Order By
[state!1!idstate],[property!2!idproperty
]
for xml explicit
Cheers,
AndrewYou can do it by adding another tag (and therefore another UNION) to your
query in which the empty "container" element is craeted by selecting NULL as
shown in the following example from Northwind.
An alternative approach would be to use an annotated schema with a
sql:is-constant annotation.
cheers,
Graeme
sample code
--
Use Northwind
SELECT 1 AS Tag,
NULL AS Parent,
NULL AS [Invoices!1],
NULL AS [Invoice!2!InvoiceNo],
NULL AS [Invoice!2!Date],
NULL AS [Item!3!Product],
NULL AS [Item!3!Price!element]
UNION ALL
SELECT 2 AS Tag,
1 AS Parent,
NULL,
OrderID,
OrderDate,
NULL,
NULL
FROM Orders
UNION ALL
SELECT 3,
2,
NULL,
O.OrderID,
NULL,
P.ProductName,
OD.UnitPrice
FROM Orders O JOIN [Order Details] OD
ON O.OrderID = OD.OrderID
JOIN Products P
ON OD.ProductID = P.ProductID
ORDER BY [Invoice!2!InvoiceNo], [Item!3!Product]
FOR XML EXPLICIT
Graeme Malcolm
Principal Technologist
Content Master
- a member of CM Group Ltd.
www.contentmaster.com
"Bunce" <Bunce@.discussions.microsoft.com> wrote in message
news:8A76BF8A-8A30-472A-89B3-AA6068AF5701@.microsoft.com...
Hey all.
I need to output a query in a given XML format and I figure I'd have a stab
using the FOR XML clause of SQL 2000 first rather than in code.
I've worked out the basics using the FOR XML EXPLICIT clause and am able to
output a structure such as this:
<state id="SA">
<property id="5">
<name>Prop1</name>
<area>35</area>
</property>
<property id="10">
<name>Prop2</name>
<area>55</area>
</property>
</state>
<state id="NSW">
<property id="24">
...
</state>
However I haven't worked out how to add 'empty' surrounding tags (I think
they may be called 'associations' in XML speak), so the schema would become:
(note the addition of the <states> and <properties> tags)
<states>
<state id="SA">
<properties>
<property id="5">
<name>Prop1</name>
<area>35</area>
</property>
<property id="10">
<name>Prop2</name>
<area>55</area>
</property>
</properties>
</state>
<state id="NSW">
<properties>
<property id="24">
...
</properties>
</state>
</states>
(apologies if the formatting doesn't stick)
Any ideas? Anyone familiar with using XML in SS?
For reference, my actual current query is below - I figured the above
example was easier to use.
SELECT
1 as Tag,
NULL as Parent,
c.textstate as [state!1!idstate],
null as [property!2!name!element],
null as [property!2!areaHA!element],
null as [property!2!dateGranted!element],
null as
[property!2!titleHoldingBody!element],
null as [property!2!idproperty]
FROM
dbo.tblStates c
Where
c.IDState<>0
union all
SELECT
2,
1,
b.textstate,
a.ShortLandName,
a.area,
a.GrantDate,
a.THBName,
a.IDProperty
FROM
dbo.tblStates b, dbo.vw_LandPurchases_WebsiteExport_Prop a where
a.IDState = b.IDState
Order By
[state!1!idstate],[property!2!idproperty
]
for xml explicit
Cheers,
Andrew|||Note that this becomes much easier in SQL Server 2005 (see
http://msdn.microsoft.com/library/e...l/forxml2k5.asp). Note
that I find such wrapper elements to be of questionable value when they only
provide an additional level of indirection in your tree. Since XPath has
list semantics, /Invoice already gives you all invoices. If you add
additional properties to Invoices such as summary information, it makes
sense.
Best regards
Michael
"Graeme Malcolm" <graemem_cm@.hotmail.com> wrote in message
news:OQmoBJtlFHA.1204@.TK2MSFTNGP12.phx.gbl...
> You can do it by adding another tag (and therefore another UNION) to your
> query in which the empty "container" element is craeted by selecting NULL
> as
> shown in the following example from Northwind.
> An alternative approach would be to use an annotated schema with a
> sql:is-constant annotation.
> cheers,
> Graeme
> sample code
> --
> Use Northwind
> SELECT 1 AS Tag,
> NULL AS Parent,
> NULL AS [Invoices!1],
> NULL AS [Invoice!2!InvoiceNo],
> NULL AS [Invoice!2!Date],
> NULL AS [Item!3!Product],
> NULL AS [Item!3!Price!element]
> UNION ALL
> SELECT 2 AS Tag,
> 1 AS Parent,
> NULL,
> OrderID,
> OrderDate,
> NULL,
> NULL
> FROM Orders
> UNION ALL
> SELECT 3,
> 2,
> NULL,
> O.OrderID,
> NULL,
> P.ProductName,
> OD.UnitPrice
> FROM Orders O JOIN [Order Details] OD
> ON O.OrderID = OD.OrderID
> JOIN Products P
> ON OD.ProductID = P.ProductID
> ORDER BY [Invoice!2!InvoiceNo], [Item!3!Product]
> FOR XML EXPLICIT
> --
> Graeme Malcolm
> Principal Technologist
> Content Master
> - a member of CM Group Ltd.
> www.contentmaster.com
>
> "Bunce" <Bunce@.discussions.microsoft.com> wrote in message
> news:8A76BF8A-8A30-472A-89B3-AA6068AF5701@.microsoft.com...
> Hey all.
> I need to output a query in a given XML format and I figure I'd have a
> stab
> using the FOR XML clause of SQL 2000 first rather than in code.
> I've worked out the basics using the FOR XML EXPLICIT clause and am able
> to
> output a structure such as this:
> <state id="SA">
> <property id="5">
> <name>Prop1</name>
> <area>35</area>
> </property>
> <property id="10">
> <name>Prop2</name>
> <area>55</area>
> </property>
> </state>
> <state id="NSW">
> <property id="24">
> ...
> </state>
>
> However I haven't worked out how to add 'empty' surrounding tags (I think
> they may be called 'associations' in XML speak), so the schema would
> become:
> (note the addition of the <states> and <properties> tags)
> <states>
> <state id="SA">
> <properties>
> <property id="5">
> <name>Prop1</name>
> <area>35</area>
> </property>
> <property id="10">
> <name>Prop2</name>
> <area>55</area>
> </property>
> </properties>
> </state>
> <state id="NSW">
> <properties>
> <property id="24">
> ...
> </properties>
> </state>
> </states>
> (apologies if the formatting doesn't stick)
> Any ideas? Anyone familiar with using XML in SS?
> For reference, my actual current query is below - I figured the above
> example was easier to use.
> SELECT
> 1 as Tag,
> NULL as Parent,
> c.textstate as [state!1!idstate],
> null as [property!2!name!element],
> null as [property!2!areaHA!element],
> null as [property!2!dateGranted!element],
> null as
> [property!2!titleHoldingBody!element],
> null as [property!2!idproperty]
> FROM
> dbo.tblStates c
> Where
> c.IDState<>0
> union all
> SELECT
> 2,
> 1,
> b.textstate,
> a.ShortLandName,
> a.area,
> a.GrantDate,
> a.THBName,
> a.IDProperty
> FROM
> dbo.tblStates b, dbo.vw_LandPurchases_WebsiteExport_Prop a where
> a.IDState = b.IDState
> Order By
> [state!1!idstate],[property!2!idproperty
]
> for xml explicit
> Cheers,
> Andrew
>

FOR XML EXPLICIT - Empty Tags?

Hey all.
I need to output a query in a given XML format and I figure I'd have a stab
using the FOR XML clause of SQL 2000 first rather than in code.
I've worked out the basics using the FOR XML EXPLICIT clause and am able to
output a structure such as this:
<state id="SA">
<property id="5">
<name>Prop1</name>
<area>35</area>
</property>
<property id="10">
<name>Prop2</name>
<area>55</area>
</property>
</state>
<state id="NSW">
<property id="24">
...
</state>
However I haven't worked out how to add 'empty' surrounding tags (I think
they may be called 'associations' in XML speak), so the schema would become:
(note the addition of the <states> and <properties> tags)
<states>
<state id="SA">
<properties>
<property id="5">
<name>Prop1</name>
<area>35</area>
</property>
<property id="10">
<name>Prop2</name>
<area>55</area>
</property>
</properties>
</state>
<state id="NSW">
<properties>
<property id="24">
...
</properties>
</state>
</states>
(apologies if the formatting doesn't stick)
Any ideas? Anyone familiar with using XML in SS?
For reference, my actual current query is below - I figured the above
example was easier to use.
SELECT
1 as Tag,
NULL as Parent,
c.textstateas [state!1!idstate],
nullas [property!2!name!element],
nullas [property!2!areaHA!element],
nullas [property!2!dateGranted!element],
null as
[property!2!titleHoldingBody!element],
null as [property!2!idproperty]
FROM
dbo.tblStates c
Where
c.IDState<>0
union all
SELECT
2,
1,
b.textstate,
a.ShortLandName,
a.area,
a.GrantDate,
a.THBName,
a.IDProperty
FROM
dbo.tblStates b, dbo.vw_LandPurchases_WebsiteExport_Prop a where
a.IDState = b.IDState
Order By
[state!1!idstate],[property!2!idproperty]
for xml explicit
Cheers,
Andrew
You can do it by adding another tag (and therefore another UNION) to your
query in which the empty "container" element is craeted by selecting NULL as
shown in the following example from Northwind.
An alternative approach would be to use an annotated schema with a
sql:is-constant annotation.
cheers,
Graeme
sample code
Use Northwind
SELECT 1 AS Tag,
NULL AS Parent,
NULL AS [Invoices!1],
NULL AS [Invoice!2!InvoiceNo],
NULL AS [Invoice!2!Date],
NULL AS [Item!3!Product],
NULL AS [Item!3!Price!element]
UNION ALL
SELECT 2 AS Tag,
1 AS Parent,
NULL,
OrderID,
OrderDate,
NULL,
NULL
FROM Orders
UNION ALL
SELECT 3,
2,
NULL,
O.OrderID,
NULL,
P.ProductName,
OD.UnitPrice
FROM Orders O JOIN [Order Details] OD
ON O.OrderID = OD.OrderID
JOIN Products P
ON OD.ProductID = P.ProductID
ORDER BY [Invoice!2!InvoiceNo], [Item!3!Product]
FOR XML EXPLICIT
Graeme Malcolm
Principal Technologist
Content Master
- a member of CM Group Ltd.
www.contentmaster.com
"Bunce" <Bunce@.discussions.microsoft.com> wrote in message
news:8A76BF8A-8A30-472A-89B3-AA6068AF5701@.microsoft.com...
Hey all.
I need to output a query in a given XML format and I figure I'd have a stab
using the FOR XML clause of SQL 2000 first rather than in code.
I've worked out the basics using the FOR XML EXPLICIT clause and am able to
output a structure such as this:
<state id="SA">
<property id="5">
<name>Prop1</name>
<area>35</area>
</property>
<property id="10">
<name>Prop2</name>
<area>55</area>
</property>
</state>
<state id="NSW">
<property id="24">
....
</state>
However I haven't worked out how to add 'empty' surrounding tags (I think
they may be called 'associations' in XML speak), so the schema would become:
(note the addition of the <states> and <properties> tags)
<states>
<state id="SA">
<properties>
<property id="5">
<name>Prop1</name>
<area>35</area>
</property>
<property id="10">
<name>Prop2</name>
<area>55</area>
</property>
</properties>
</state>
<state id="NSW">
<properties>
<property id="24">
....
</properties>
</state>
</states>
(apologies if the formatting doesn't stick)
Any ideas? Anyone familiar with using XML in SS?
For reference, my actual current query is below - I figured the above
example was easier to use.
SELECT
1 as Tag,
NULL as Parent,
c.textstate as [state!1!idstate],
null as [property!2!name!element],
null as [property!2!areaHA!element],
null as [property!2!dateGranted!element],
null as
[property!2!titleHoldingBody!element],
null as [property!2!idproperty]
FROM
dbo.tblStates c
Where
c.IDState<>0
union all
SELECT
2,
1,
b.textstate,
a.ShortLandName,
a.area,
a.GrantDate,
a.THBName,
a.IDProperty
FROM
dbo.tblStates b, dbo.vw_LandPurchases_WebsiteExport_Prop a where
a.IDState = b.IDState
Order By
[state!1!idstate],[property!2!idproperty]
for xml explicit
Cheers,
Andrew
|||Note that this becomes much easier in SQL Server 2005 (see
http://msdn.microsoft.com/library/en.../forxml2k5.asp). Note
that I find such wrapper elements to be of questionable value when they only
provide an additional level of indirection in your tree. Since XPath has
list semantics, /Invoice already gives you all invoices. If you add
additional properties to Invoices such as summary information, it makes
sense.
Best regards
Michael
"Graeme Malcolm" <graemem_cm@.hotmail.com> wrote in message
news:OQmoBJtlFHA.1204@.TK2MSFTNGP12.phx.gbl...
> You can do it by adding another tag (and therefore another UNION) to your
> query in which the empty "container" element is craeted by selecting NULL
> as
> shown in the following example from Northwind.
> An alternative approach would be to use an annotated schema with a
> sql:is-constant annotation.
> cheers,
> Graeme
> sample code
> --
> Use Northwind
> SELECT 1 AS Tag,
> NULL AS Parent,
> NULL AS [Invoices!1],
> NULL AS [Invoice!2!InvoiceNo],
> NULL AS [Invoice!2!Date],
> NULL AS [Item!3!Product],
> NULL AS [Item!3!Price!element]
> UNION ALL
> SELECT 2 AS Tag,
> 1 AS Parent,
> NULL,
> OrderID,
> OrderDate,
> NULL,
> NULL
> FROM Orders
> UNION ALL
> SELECT 3,
> 2,
> NULL,
> O.OrderID,
> NULL,
> P.ProductName,
> OD.UnitPrice
> FROM Orders O JOIN [Order Details] OD
> ON O.OrderID = OD.OrderID
> JOIN Products P
> ON OD.ProductID = P.ProductID
> ORDER BY [Invoice!2!InvoiceNo], [Item!3!Product]
> FOR XML EXPLICIT
> --
> Graeme Malcolm
> Principal Technologist
> Content Master
> - a member of CM Group Ltd.
> www.contentmaster.com
>
> "Bunce" <Bunce@.discussions.microsoft.com> wrote in message
> news:8A76BF8A-8A30-472A-89B3-AA6068AF5701@.microsoft.com...
> Hey all.
> I need to output a query in a given XML format and I figure I'd have a
> stab
> using the FOR XML clause of SQL 2000 first rather than in code.
> I've worked out the basics using the FOR XML EXPLICIT clause and am able
> to
> output a structure such as this:
> <state id="SA">
> <property id="5">
> <name>Prop1</name>
> <area>35</area>
> </property>
> <property id="10">
> <name>Prop2</name>
> <area>55</area>
> </property>
> </state>
> <state id="NSW">
> <property id="24">
> ...
> </state>
>
> However I haven't worked out how to add 'empty' surrounding tags (I think
> they may be called 'associations' in XML speak), so the schema would
> become:
> (note the addition of the <states> and <properties> tags)
> <states>
> <state id="SA">
> <properties>
> <property id="5">
> <name>Prop1</name>
> <area>35</area>
> </property>
> <property id="10">
> <name>Prop2</name>
> <area>55</area>
> </property>
> </properties>
> </state>
> <state id="NSW">
> <properties>
> <property id="24">
> ...
> </properties>
> </state>
> </states>
> (apologies if the formatting doesn't stick)
> Any ideas? Anyone familiar with using XML in SS?
> For reference, my actual current query is below - I figured the above
> example was easier to use.
> SELECT
> 1 as Tag,
> NULL as Parent,
> c.textstate as [state!1!idstate],
> null as [property!2!name!element],
> null as [property!2!areaHA!element],
> null as [property!2!dateGranted!element],
> null as
> [property!2!titleHoldingBody!element],
> null as [property!2!idproperty]
> FROM
> dbo.tblStates c
> Where
> c.IDState<>0
> union all
> SELECT
> 2,
> 1,
> b.textstate,
> a.ShortLandName,
> a.area,
> a.GrantDate,
> a.THBName,
> a.IDProperty
> FROM
> dbo.tblStates b, dbo.vw_LandPurchases_WebsiteExport_Prop a where
> a.IDState = b.IDState
> Order By
> [state!1!idstate],[property!2!idproperty]
> for xml explicit
> Cheers,
> Andrew
>

FOR XML Clause TYPE option

Hello,

Could anybody tell me if it's possible to named the result of this query?

SELECT User_Id, Name, Surname, Age, Nationality
FROM [Basic Data] FOR XML PATH, TYPE, elements, root('AllPrimaryData')

The result of this query is an XML Document in a column without a name and I'm trying to name it but I can't.

Could anybody help me with that?

Thanks a lot.

I'm not sure if this will work, but try:

SELECT

User_Id as "@.UserId",

Name as "@.Name",

Surname as "@.SurName",

Age as "@.Age",

Nationality as "@.Nationality"
FROM [Basic Data]

FOR XML PATH, TYPE, elements, root('AllPrimaryData')|||

Try something like this:

select nameOfColumn from (
SELECT User_Id, Name, Surname, Age, Nationality
FROM [Basic Data] FOR XML PATH, TYPE, elements, root('AllPrimaryData')
as myXMLTable(nameOfColumn)

or do it with a CTE:

with myXMLTable(nameOfColumn) as (
SELECT User_Id, Name, Surname, Age, Nationality
FROM [Basic Data] FOR XML PATH, TYPE, elements, root('AllPrimaryData')
)
select nameOfColumn
from myXMLTable

Steve Kass
Drew University
www.stevekass.com

|||

Hi,

Thanks for your response, but it doesn't work.

|||Thanks a lot Steve, your first proposal works fine!

FOR XML clause and <root> element


Hi,
When I run the following query in Query Analyzer, it works fine, but I
cannot add <ROOT> ... </ROOT> element to result:
SELECT * FROM
(
select top 10
CompanyName,
SUM((CONVERT(money,("Order
Details".UnitPrice*Quantity*(1-Discount)/100))*100)) AS Amount
, count(*) OrderItemCount
from
customers
inner join orders on orders.CustomerID = customers.CustomerID
inner join [Order Details] on Orders.OrderID = [Order Details].OrderID
GROUP BY
CompanyName
) AS GoodCustomers
FOR XML AUTO ,ELEMENTS
Ho can add <root> element to the result?
Thank you,
AlanHi
You can do that externally such as http://sqlxml.org/faqs.aspx?faq=10
This may also help http://www.perfectxml.com/Articles/XML/ExportSQLXML.asp
John
"A.M" wrote:

>
> Hi,
>
> When I run the following query in Query Analyzer, it works fine, but I
> cannot add <ROOT> ... </ROOT> element to result:
>
>
> SELECT * FROM
> (
> select top 10
> CompanyName,
> SUM((CONVERT(money,("Order
> Details".UnitPrice*Quantity*(1-Discount)/100))*100)) AS Amount
> , count(*) OrderItemCount
> from
> customers
> inner join orders on orders.CustomerID = customers.CustomerID
> inner join [Order Details] on Orders.OrderID = [Order Details].OrderID
> GROUP BY
> CompanyName
> ) AS GoodCustomers
> FOR XML AUTO ,ELEMENTS
>
>
>
>
>
> Ho can add <root> element to the result?
>
> Thank you,
> Alan
>
>|||Hi,
Additional to what John recommanded, there is a new property that you can
use to tell the OLEDB provider to add
the root tag for you.
command.Properties("Output Stream") = response
command.Properties("XML Root") = "root"
command.Execute , , 1024
Then the stream you are getting back will have "XML Root" as the document
root tag.
Sincerely yours,
Michael Cheng
Microsoft Online Partner Support
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
========================================
=============
This posting is provided "AS IS" with no warranties, and confers no rights.

For XML clause

I'm trying to turn a product table into an XML file to create an
heirarchical menu, and I was wondering if there was any easy way to do
this. The table is (simplified) below:

Create table product(
category varchar,
subcategory varchar,
name varchar)

and I want the XML to represent the category structure as below:

<Categories>
<sucategories>
<products>
....
</products>
</subcategories>
</Categories
Thanks,

John(jhoge123@.yahoo.com) writes:
> I'm trying to turn a product table into an XML file to create an
> heirarchical menu, and I was wondering if there was any easy way to do
> this. The table is (simplified) below:
> Create table product(
> category varchar,
> subcategory varchar,
> name varchar)
> and I want the XML to represent the category structure as below:
><Categories>
> <sucategories>
> <products>
> ....
> </products>
> </subcategories>
></Categories
I think that you should be able to do this with FOR XML EXPLICIT.
But I'm not very good at XML queries, so I don't want give an outline of
something that is likely to be incorrect.

If you post a CREATE TABLE statement for the table, INSERT statements
for some sample data, and the desired XML from the data, I might give
it a try.

Or you could look at FOR XML yourself in Books Online. (That's anyway
where I have to look to write this type of queries. :-)

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Create table product(
category varchar(10),
subcategory varchar(10),
name varchar(10))

insert into product(category,subcategory,name)
values ('Vehicle','Car','Sports')
insert into product(category,subcategory,name)
values ('Vehicle','Car','Saloon')
insert into product(category,subcategory,name)
values ('Vehicle','Motorbike','Sports')
insert into product(category,subcategory,name)
values ('Vehicle','Motorbike','Tourer')

dbcc traceon(257) -- pretty print for Query Analyzer text results

select distinct 1 as Tag,
null as Parent,
category as [Category!1!Value],
null as [subcategories!2!Value],
null as [products!3!Value]
from product
union all
select distinct 2 as Tag,
1 as Parent,
category as [Category!1!Value],
subcategory as [subcategories!2!Value],
null as [products!3!Value]
from product
union all
select 3 as Tag,
2 as Parent,
category as [Category!1!Value],
subcategory as [subcategories!2!Value],
name as [products!3!Value]
from product
order by [Category!1!Value],[subcategories!2!Value],[products!3!Value]
for xml explicit|||(markc600@.hotmail.com) writes:
> dbcc traceon(257) -- pretty print for Query Analyzer text results

Ah, that's one cute. Didn't know.

And thanks for posting the example!

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Sunday, February 26, 2012

FOR XML AUTO in SQL SERVER 2000

Hi

I am trying to use FOR XML AUTO clause in Sql Server 2000.

My requirement is to use this clause and assign the result set into a parameter.Like

Declare @.ss XML

set @.ss=(SELECT * FROM TB_TMGroup WHERE TM_GroupID = 29 FOR XML AUTO,ELEMENTS, BINARY BASE64)
print @.ss

So that i can use this paramer "@.ss" inside a insert statement.

Can any one help on this ?

Thanks.

You can’t do this in SQL Server 2000.

In SQL Server 2000 there is no XML data type, so you can’t able to store the FOR XML output into your local variable.

The first XML data type is introduced in SQL Server 2005. If you use SQL Server 2005 then your query will work fine. J

FOR XML AUTO - Performance under SQL 2000 SP3

I'm currently having a performance problem with a straight SQL select statement with the 'for xml auto' clause returning a result set involving approximately 5,000 records. This is taking about 16 seconds on a SQL 2000 installation SP3 (8.00.818), while it only takes a few milliseconds when removing the 'for xml auto' (when executed via Query Analyzer). Time under the 'for xml auto' is directly proportional to the number of records processed.

Testing the same query/table on another server with SP4 installed shows little difference in times.

I'll be trying to set up another test installation with SP3 on a different server to see if this behavior is something specific to SP3, but has anyone else encountered this problem?

Not anything specific to SP3...just tested it on a 8.00.818 box that I created and the query with/without the 'for xml auto' clause processed the results instantaneously. Will check to see what might be different about server configuration settings and collations.|||Seems to be associated with amount of data being output in the XML stream. The original output consists of 261 KB data and takes 16 seconds duration to execute. Renaming attributes in table to single characters to reduce tag name overhead reduces data size by 69% and duration to 70%. Execution was via Query Analyzer local to the database server. Execution on same table/data on another server was 115 ms. Query plans generated are the same. Output of data as a normal SQL result set (without 'for xml clause') on the problem installation takes time down to milliseconds also.|||

It is hard to give you any response to this without an exact repro (data and query at least). Did you compare the query plans?

The XML will be bigger than TDS rowset format but should not have such a big overhead. Are you sure you are not including the time it takes the QA to show the data?

Also, if the overhead does not show in SP4, could you just upgrade to SP4?

Best regards

Michael

FOR UPDATE clause in cursor

I'm refactoring a cursor written by another employee. It includes a cursor
that uses the FOR UPDATE clause to update records as the cursor iterates
through the records. Please assume that the cursor cannot/should not be
replaced.
When executing the snippet of code below, I get the SQL Server error below.
What syntax would allow me to make the cursor read/write rather than read
only? I tried making the cursor DYNAMIC, but this did not appear to help.
Thanks in adavnce.
-Mark
Server: Msg 16957, Level 16, State 4, Procedure p_om_daily_reload, Line 402
FOR UPDATE cannot be specified on a READ ONLY cursor.
********************** SNIPPET OF CODE ****************************
declare @.audit_note varchar(200)
declare reg_curs_mentorhip_cancelled CURSOR DYNAMIC
FOR
-- Look for existing payment records using the reg_num that have been
marked as canceled in informix.
SELECT DISTINCT mp.mentorship_id, im.hours
FROM t_om_informix_mentorship im
INNER JOIN t_om_app_mentorship_payment mp
ON im.reg_num = mp.reg_num
WHERE im.reg_status = 'C'
FOR UPDATE -- MARK: Added to allow cursor data to be updated. Default is
read-only
OPEN reg_curs_mentorhip_cancelled
FETCH NEXT FROM reg_curs_mentorhip_cancelled INTO @.mentorship_id,
@.add_purchase
WHILE (@.@.FETCH_STATUS <> -1)
BEGIN
IF (@.@.FETCH_STATUS <> -2)
BEGIN
PRINT'* 4a , subtract the amount of time refunded'
UPDATE t_om_app_mentorship
SET time_purchased = time_purchased - @.add_purchase
WHERE mentorship_id = @.mentorship_id;
PRINT'* 4a , remove payment record'
DELETE t_om_app_mentorship_payment
WHERE CURRENT OF reg_curs_mentorhip_cancelled
PRINT'* 4a ,add audit trail'
SET @.audit_note = 'mentorship_id = ' + convert(varchar(15),
@.mentorship_id) + ', Hours affected: ' + CAST(@.add_purchase as varchar)
EXEC p_om_data_audit_trail_i @.user_key = 'unknown', @.priority = 1,
@.create_user = 'Reload_Script', @.category = 'Payments',
@.issue = 'Mentorship Payment either disappeared or was cancelled',
@.note = @.audit_note
END
FETCH NEXT FROM reg_curs_mentorhip_cancelled INTO @.mentorship_id,
@.add_purchase
END
CLOSE reg_curs_mentorhip_cancelled
DEALLOCATE reg_curs_mentorhip_cancelledThe cursor cannot be updateable because of the DISTINCT modifier in the
query.

> Please assume that the cursor cannot/should not be
> replaced.
No chance of me assuming that! Cursors bad. Updateable cursors worse.
;-)
David Portas
SQL Server MVP
--|||>> Please assume that the cursor cannot/should not be replaced. <<
Is this a teaching exercise of some kind? "Look Igor, screwdriver
better than rocks for cabinet making!!" "Igor not like screwdriver!
Igor have rock!"
Based on ~20 years with SQL, ten years on the ANSI Standards committee
of SQL, and six books on the language, I find Kosher Pork easier to
assume -- Hey, we got genetic engineering and can fix those feet!
I have written five cursors in my career and I know that at least three
of them could have been done with a CASE expression. They would have
run **orders of magnitude** faster and been easierr to maintain.
Also, when you post code could you consistently uppercase reserved
words? Learn about semi-colons in SQL? ISO_11179 rules? Not use PRINT
in application code (it is for debugging only)? What about the basic
rule that audit trails are external to the procedures, so you know that
they valid?
You have code like an OO or COBOL program. The basic problem; this is
procedural code written in SQL, a declarative language. Your entire
approach and vocabulary are non-relational (Records in SQL').
Dave was nicer than me (who isn't?). EVERYTHING needs to be re-done
whiel there is still time. This is so crappy it is not worh saving and
might have screwed up data integrity already.

Friday, February 24, 2012

For index

Is there any way I can findout all the columns and it's table which used in
stored procedure in the where clause and don't have indexes on it ? If yes
then how can I finout out.
Thanks
Look at the Index Tuning Wizard. That would be a great tool for you to
learn how to use.
"Rogers" <Rogers@.discussions.microsoft.com> wrote in message
news:188538E6-A0AA-4750-98DA-AE4DE36ECB81@.microsoft.com...
> Is there any way I can findout all the columns and it's table which used
in
> stored procedure in the where clause and don't have indexes on it ? If yes
> then how can I finout out.
> Thanks

Sunday, February 19, 2012

FOR clause in mssql2k5.

Here is the BOL example for the PIVOT function:
SELECT VendorID, [164] AS Emp1, [198] AS Emp2, [223] AS Emp3, [231] AS Emp4,
[233] AS Emp5
FROM
(SELECT PurchaseOrderID, EmployeeID, VendorID
FROM Purchasing.PurchaseOrderHeader) p
PIVOT
(
COUNT (PurchaseOrderID)
FOR EmployeeID IN
( [164], [198], [223], [231], [233] )
) AS pvt
ORDER BY VendorID;
What Im having a hard time understanding though is the FOR clause. Why is it
used instead of the WHERE clause?
TIA, ChrisRIt is not a WHERE clause, and I think they used a different keyword in order
to make that absolutely clear. It only supports FOR <columnname> IN
(<values> ). If you need an actual WHERE clause, you can put one in the
derived table, or before the ORDER BY clause.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"ChrisR" <ChrisR@.noEmail.com> wrote in message
news:usyswa3eGHA.4932@.TK2MSFTNGP03.phx.gbl...
> Here is the BOL example for the PIVOT function:
> SELECT VendorID, [164] AS Emp1, [198] AS Emp2, [223] AS Emp3, [231] AS
> Emp4,
> [233] AS Emp5
> FROM
> (SELECT PurchaseOrderID, EmployeeID, VendorID
> FROM Purchasing.PurchaseOrderHeader) p
> PIVOT
> (
> COUNT (PurchaseOrderID)
> FOR EmployeeID IN
> ( [164], [198], [223], [231], [233] )
> ) AS pvt
> ORDER BY VendorID;
> What Im having a hard time understanding though is the FOR clause. Why is
> it
> used instead of the WHERE clause?
> TIA, ChrisR
>|||FOR clause is a part of the PIVOT table
you can say its all part of the FROM clause.
the result of this is actually a table on which you can do a select and a
filter
(SELECT PurchaseOrderID, EmployeeID, VendorID
FROM Purchasing.PurchaseOrderHeader) p
PIVOT
(
COUNT (PurchaseOrderID)
FOR EmployeeID IN
( [164], [198], [223], [231], [233] )
) AS pvt
And the above piece of code means this
Do a count of purchase order id grouping by each of the employeeID given in
the inclause and replace the EmployeeID column with these columns
[164], [198], [223], [231], [233] each having the split up count for its
employeeID.
I know I am confusing :)
"ChrisR" wrote:

> Here is the BOL example for the PIVOT function:
> SELECT VendorID, [164] AS Emp1, [198] AS Emp2, [223] AS Emp3, [231] AS Emp
4,
> [233] AS Emp5
> FROM
> (SELECT PurchaseOrderID, EmployeeID, VendorID
> FROM Purchasing.PurchaseOrderHeader) p
> PIVOT
> (
> COUNT (PurchaseOrderID)
> FOR EmployeeID IN
> ( [164], [198], [223], [231], [233] )
> ) AS pvt
> ORDER BY VendorID;
> What Im having a hard time understanding though is the FOR clause. Why is
it
> used instead of the WHERE clause?
> TIA, ChrisR
>
>|||You can display row data to column using PIVOT and FOR clause .
"ChrisR"?? ??? ??:

> Here is the BOL example for the PIVOT function:
> SELECT VendorID, [164] AS Emp1, [198] AS Emp2, [223] AS Emp3, [231] AS Emp
4,
> [233] AS Emp5
> FROM
> (SELECT PurchaseOrderID, EmployeeID, VendorID
> FROM Purchasing.PurchaseOrderHeader) p
> PIVOT
> (
> COUNT (PurchaseOrderID)
> FOR EmployeeID IN
> ( [164], [198], [223], [231], [233] )
> ) AS pvt
> ORDER BY VendorID;
> What Im having a hard time understanding though is the FOR clause. Why is
it
> used instead of the WHERE clause?
> TIA, ChrisR
>
>