I have a table that has thousands of rows that has the following data in the column named OldValue:
<attendance xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CalendarId>100</CalendarId>
<PeriodId>1019</PeriodId>
<PersonId>10457</PersonId>
<Date>2012-01-04 00:00:00</Date>
<ExcuseId>884</ExcuseId>
</attendance>
I am trying to set a selective xml index. Here is what I have done so far:
Creating the selective XML Index:
CREATE SELECTIVE XML INDEX sxi_Test ON [dbo].[mytable](OldValue)
WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as yming)
FOR
(
pathWH2 = '/yming:Attendance/PersonId' AS XQUERY 'xs:string' SINGLETON
);
GO
Querying the index always returns 0 rows:
WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as yming)
SELECT COUNT(*)
FROM mytable
WHERE OldValue.exist(N'(/yming:Attendance/yming:PersonId[.=10457])') = 1
GO
Any suggestions? Thank you.
I usually stumble into a solution with anything involving XML namespaces in SQL, but this seems to work for me:
DECLARE #x XML = '<attendance xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <CalendarId>100</CalendarId> <PeriodId>1019</PeriodId> <PersonId>10457</PersonId> <Date>2012-01-04 00:00:00</Date> <ExcuseId>884</ExcuseId> </attendance>';
WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as yming)
SELECT #x.exist(N'(/attendance/PersonId[.=10457])')
GO
Related
I have following XML stored in a XML column in a SQL Server database table.
<?xml version="1.0" encoding="UTF-8" ?>
<ns0:Root>
<ns0:Result>
<ns0:AStatus>Aaa</ns0:AStatus>
<ns0:BStatus>Bbb</ns0:BStatus>
</ns0:Result>
</ns0:Root>
I'd like to get value "Aaa", by firing SQL query.
Your XML data is incomplete - it uses a namespace prefix ns0 without defining it anywhere... I've added some arbitrary, totally made-up XML namespace here in my sample - you need to check what that XML namespace actually is in your case and adapt the sample accordingly!
Try this:
DECLARE #InputTable TABLE (ID INT NOT NULL, XmlData XML)
INSERT INTO #InputTable(ID, XmlData) VALUES(42, '<?xml version="1.0" encoding="UTF-8" ?>
<ns0:Root xmlns:ns0="urn:some-sample-xml-namespace">
<ns0:Result>
<ns0:AStatus>Aaa</ns0:AStatus>
<ns0:BStatus>Bbb</ns0:BStatus>
</ns0:Result>
</ns0:Root>')
-- define the XML namespace to use
;WITH XMLNAMESPACES('urn:some-sample-xml-namespace' AS x)
SELECT
ID,
XC.value('(x:AStatus)[1]', 'varchar(50)')
FROM
#inputtable
CROSS APPLY
-- get a "pseudo" table of nodes <ns0:Root>/<ns0:Result>
XmlData.nodes('x:Root/x:Result') AS XT(XC)
Basically, you need to have a definition for your XML namespace prefix - and in the SELECT against this XML data, you need to have the same XML namespace (even though - as shown - the prefix assigned to that namespace can be different - but the namespace must match!).
This then selects the data from the table, and for the XML data, it uses the .nodes() XQuery function to get a list of XML element that match this XPath expression - and it gets these nodes as a in-memory pseudo table XT with a single XML column XC from which you can then again fetch values (like reaching into the first <ns:AStatus> element).
I am sending the following xml to a sql server stored procedure to be used in a where clause.
<root>
<id>1</id>
<id>2</id>
<id>3</id>
</root
I am trying to work out a way to have a select statement where the where clause will check for any of the id values in the passed xml parameter.
Something like this:
#xml xml
select * from table
where (if exists (select id in #xml))
sorry i am not very good at sql.
declare #xml xml;
set #xml = CAST('<root><id>1</id><id>2</id><id>3</id></root>' AS XML)
select id from mytable
where exists(select 1 from #xml.nodes('/root/id')as result(node)
where node.value('(.)[1]', 'int') = id)
I always refer to this link from Erland Sommarskog for handling arrays in sql. It provides a good example and contrasts it with other mechanisms of passing in arrays to stored procedures.
Note if you are using SQL Server 2008 the he has another link detailing the use of table valued parameters. It might be overkill for your example, but if you are keen to learn what is available at your disposal, then its all worth a read.
I have a relation which has an XML column storing data in the following structure
<Report id="b5d9b8da-7af4-4257-b825-b28af91dd833">
<CreatedDate>04-12-2012</CreatedDate>
<LastUpdated>04-12-2012</LastUpdated>
<Reference>abc123</Reference>
</Report>
I'm writing a stored procedure to retrieve all reports and join them and wrap them in a root node called reports. I have the following so far;
WITH XMLNAMESPACES(DEFAULT 'http://www.defaultnamespace.com/1.0')
SELECT
#Xml =
(
SELECT
(
SELECT xml.query('.')
FROM
[database].[Reports]
WHERE
ClientId = #clientId
FOR XML PATH(''),
TYPE
)
FOR XML PATH('Reports'),
TYPE
)
Whilst this returns all the reports in the right format, there exists a blank default namespace on the report element like the following;
<Reports xmlns="http://www.defaultnamespace.com/1.0">
<Report xmlns="" id="b5d9b8da-7af4-4257-b825-b28af91dd833">
<CreatedDate>04-12-2012</CreatedDate>
<LastUpdated>04-12-2012</LastUpdated>
<Reference>abc123</Reference>
</Report>
</Reports>
Could someone explain a suitable way of excluding the namespace on the report element?
Any help is greatly appreciated guys :)
It's a little messy and probably not very efficient but you can redefine namespaces with an XQuery over your intermediate XML.
Instead of using SQL Server's WITH XMLNAMESPACES you declare the default namespace in XQuery, for example...
if object_id(N'Reports') is not null drop table [Reports];
go
create table [Reports] (
[ClientId] int not null,
[xml] [xml] not null
)
go
insert [Reports] ([ClientID], [xml])
values (1, N'<Report id="b5d9b8da-7af4-4257-b825-b28af91dd833">
<CreatedDate>04-12-2012</CreatedDate>
<LastUpdated>04-12-2012</LastUpdated>
<Reference>abc123</Reference>
</Report>');
go
declare #clientId int = 1
select (
select [xml].query('/*:Report')
from [Reports]
where ClientId = #clientId
for xml path('Reports'), type
).query('declare default element namespace "http://www.defaultnamespace.com/1.0";
for $x in /*:Reports return
<Reports>
{
for $y in $x/*:Report return
<Report>
{attribute id {$y/#id}}
{element CreatedDate {$y/*:CreatedDate/text()}}
{element LastUpdated {$y/*:LastUpdated/text()}}
{element Reference {$y/*:Reference/text()}}
</Report>
}
</Reports>')
go
This will return the following block of XML:
<Reports xmlns="http://www.defaultnamespace.com/1.0">
<Report id="b5d9b8da-7af4-4257-b825-b28af91dd833">
<CreatedDate>04-12-2012</CreatedDate>
<LastUpdated>04-12-2012</LastUpdated>
<Reference>abc123</Reference>
</Report>
</Reports>
Your issue is that the column was not stored with a default namespace of "http://www.defaultnamespace.com/1.0".
So the logical name of your tag is NS = "", name =Report.
SQL Server is being absolutely correct.
What you would like to do is to say
by the way, that XML data I stored, I want you to rewrite every tag from the "" namespace logically attached to every name, to a tag of the same relative name in the "http://www.defaultnamespace.com/1.0" namespace, and then make that the default namespace
AFAIK, You can't do that (but if you find a way let me know!). The closest you can get is cast it to nvarchar(max) with no namespace, then cast it back again with the desired namespace applied.
I have an XML like:
<?xml version="1.0" encoding="utf-16"?>
<ExportProjectDetailsMessage xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Project">
<CPProjectId>7665699f-6772-424c-8b7b-405b9220a8e7</CPProjectId>
</ExportProjectDetailsMessage>
I'm trying to get the CPProjectId as a Uniqueidentifier using:
DECLARE #myDoc xml
DECLARE #ProdID varchar(max)
SET #myDoc = '<ExportProjectDetailsMessage xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Project"><CPProjectId>7665699f-6772-424c-8b7b-405b9220a8e7</CPProjectId></ExportProjectDetailsMessage>'
SET #ProdID = #myDoc.value('(ExportProjectDetailsMessage/CPProjectId)[1]', 'varchar(max)' )
SELECT #ProdID
All i can receive is NULL =/
I've tried many combinations on #myDoc.value but no results =/
How can i retrieve the value from my XML ?
Thanks!
--EDIT:
Something that i noted, when i remove the namespace declaration from the XML it works fine!
The problem is that i need this namespaces! =/
You're right the namespace is the issue. You're query is looking for a node ExportProjectDetailsMessage but such a node doesn't exist in your document, because there is a namespace declared as a default in your document. Since you can't remove that (nor should you) you should include it in your XPATH query like so:
set #ProdId = #myDoc.value('
declare namespace PD="http://schemas.datacontract.org/2004/07/Project";
(PD:ExportProjectDetailsMessage/PD:CPProjectId)[1]', 'varchar(max)' )
You may also want to consider not using varchar(max) but perhaps uniqueidentifier
A better way to do this is to simply declare the namespace before each of your queries:
;WITH XMLNAMESPACES(DEFAULT 'http://schemas.datacontract.org/2004/07/Project')
It's like a temporary default. When you run the next query in the batch you'll get nulls again if you don't specify this before each of your selects.
So instead of using "SET", you can use "SELECT" to set the value like so:
;WITH XMLNAMESPACES(DEFAULT 'http://schemas.datacontract.org/2004/07/Project')
SELECT #ProdID = #myDoc.value('(ExportProjectDetailsMessage/CPProjectId)[1]', 'VarChar(MAX)')
SELECT #ProdID
Same results, just more readable and maintainable.
I found the solution here: http://www.sqlservercentral.com/Forums/Topic967100-145-1.aspx#bm967325
I have a table called XML (in SQL Server 2008) and it has a field called XmlDocument of type XML. I am trying to to delete an attribute from an XML variable.
Here is how my xml looks like
<clue_personal_auto xmlns="http://cp.com/rules/client">
<admin>
<receipt_date>03/16/2011</receipt_date>
<date_request_ordered>03/16/2011</date_request_ordered>
<report_usage>Personal</report_usage>
</admin>
</clue_personal_auto>
My query
UPDATE XML
SET XmlDocument.modify('delete (/clue_personal_auto/#xmlns)[1]')
WHERE xmlid = 357
When I run this query in query analyzer I see the message "1 row(s) affected" but in reality the xmlns attribute of clue_personal_auto element is not being removed. Any idea what am I doing wrong.
Thanks
BB
You need to use WITH xmlnamespaces, otherwise "/clue_personal_auto" does not match the NAMESPACED clue_personal_auto xmlns="..." node.
Not only that, you cannot actually remove a namespace since it is not a normal attribute.
Example of removing a regular attribute
declare #xml table (xmlid int, xmldocument xml)
insert #xml select 357, '
<clue_personal_auto xmlns="http://cp.com/rules/client" otherattrib="x">
<admin>
<receipt_date>03/16/2011</receipt_date>
<date_request_ordered>03/16/2011</date_request_ordered>
<report_usage>Personal</report_usage>
</admin>
</clue_personal_auto>'
;WITH XMLNAMESPACES ('http://cp.com/rules/client' as ns)
UPDATE #XML
SET XmlDocument.modify('delete (/ns:clue_personal_auto/#otherattrib)[1]')
WHERE xmlid = 357
select * from #xml
UPDATE XML
SET CONVERT(XML, REPLACE(CONVERT(NVARCHAR(MAX), XmlDocument), N' xmlns=...'))
WHERE ID = 357
I can't seem to find an easy way to do this - but the real question remains: why do you want to remove the namespace?? Using the WITH XMLNAMESPACES ... construct, you can easily make use of the namespaces.
Instead of putting a lot of effort in getting rid of it - learn about XML namespaces and start using them!
You can quite easily use that XML namespace in your queries:
;WITH XMLNAMESPACES (DEFAULT 'http://cp.com/rules/client' )
SELECT
XmlDocument.value('(/clue_personal_auto/admin/report_usage)[1]', 'varchar(25)')
FROM XML
WHERE ID = 357
and be happy with it - no need to artificially remove xmlns= declarations anymore!