Parsing multiple xml elements from an xmlstring in SQL server - sql

My input xml string is
<users><id>p1</id><id>p2</id><id>p3</id></users>
i want to write an update query to update the column 'Request_Status' in my table if the id matches to the ones in the xml string
I tried this statement:
Update login set
Request_Status='A'
where
EmpId in
(
SELECT Pers.value('(id)[1]', 'nchar(10)') as 'ID'
FROM
#xmlUserId.nodes('/users') as EMP(Pers)
)
This only updates for ID p1 and not for the other 2.
I referred to this,
How to loop and parse xml parameter in sql server stored procedure
(P.S my first question here, so please excuse mistakes, unconventional elements in the post)

Try to change the XML selection query to be as follow :
SELECT Pers.value('self::*', 'nchar(10)') as 'ID'
FROM
#xmlUserId.nodes('/users/id') as EMP(Pers)
Above query should return all <id> element within <users> element
[SQL fiddle demo]

Related

SQL Error 'ORA-00904' When Retrieving Data From an XML File Via XMLQuery

I've been trying for days now to retrieve data from an XML file with a SELECT statement in SQL Developer but I constantly get the 'ORA-00904' when trying to execute the statement. So far, these are the steps I've been following:
Create the table and directory where I want the XML data to be stored in:
CREATE TABLE PLAYER OF XMLTYPE; / CREATE DIRECTORY PLDIR AS 'C:\Users\marta\OneDrive\Escritorio\UOC\Sem3\ABD\PR2'; /
Insert into my PLAYER table said data:
INSERT INTO PLAYER VALUES (XMLTYPE(bfilename('PLDIR', 'InfoPlayersWPT.xml'),nls_charset_id('AL32UTF8')))
/
So far so good. The issue appears when I try to execute the SELECT statement
What could it be? I've changed the $Name parameter a million times as well as the Name field but nothing changes. The thing is that in the XML file, these are the fields:
--Update--
I've modified a little bit the structure and this is the new error I get:
enter image description here
I've reached a point where I don't get if there could be a problem with my database connection or if the variable are incorrect.
Any form of help would be much appreciated.
Your table doesn't have a name or id column. Your query is trying to get those, while also transforming the XML to an info node making the id a node rather than an attribute, but you still don't extract the values from that new XML. You don't need to though.
If the document only has one player (which seems unlikely with the outer node 'Players') then you can get the data with an XMLQuery call for each value:
select XMLQuery('/Players/Player/#id' passing p.object_value returning content) as id,
XMLQuery('/Players/Player/Name/text()' passing p.object_value returning content) as name
from player p
ID
NAME
1
Francinsco Navarro Compán
But it's a bit simpler, particularly if you add more data items, to use a single XMLTable instead:
select x.id, x.name
from player p
cross apply XMLTable(
'/Players/Player'
passing p.object_value
columns id number path '#id',
name varchar2(30) path 'Name'
) x
ID
NAME
1
Francinsco Navarro Compán
fiddle
... which would also handle multiple players in each XML document.

Replace all URLs in SQL Server table column which contains html data

I have a SQL Server table column which contains html contents with links in format href="/page/random_id1/random_id2".
Here random_id1 is a guid and random_id2 is a slug. Now i would like to run a sql query which can replace all href values by converting them to href="/page/random_id2".
Notice here, i'm removing random_id1 part from every herf.
Here's a sample query:
select top 1 id, htmlcode from tenantpage;
03781776-5f64-4c59-a6f7-5873e0b9f128
<div>test</div>Link<div>something else <span>test Link</span></div>Link
I don't know if this is possible, if possible please help here.

Read XML string in SQL and Dynamically select value from XMl string

I have One table "tblXMLData" with One column "coreData".
I am inserting XML string in this table.
I want to read this xml and insert record into another table.
When i write bellow code it is working fine,
Declare #xmlRec xml
Select TOP 1 #xmlRec = coreData FROM tblXMLData
SELECT
tmpTable.col.query ('FName').value('.','varchar(50)') AS FName,
tmpTable.col.query ('LName').value('.','varchar(50)') AS LName,
tmpTable.col.query ('Mobile').value('.','varchar(50)') AS Mobile
FROM #xmlRec.nodes('data/data') AS tmpTable(col)
But I want to create select statement Dynamically. Like I am read column name from another table and creating dynamic select statement.
When i execute that statement it will not work because "#xmlRec.nodes" is giving error.
I want to create dynamic Query and insert selected record into another table.
Please help....

SQL xpath for level 1 node values

I'm trying to create a stored procedure in sql 2008 to select the id values from some xml like this.
DECLARE #idPolygonXML XML
SET #idPolygonXML =
'<polygons>
<id>35</id>
<id>36</id>
<id>37</id>
<id>38</id>
<id>39</id>
<id>40</id>
</polygons>'
I can get the id for a specific index but I need all of them and I keep getting 'value() requires a singleton'.
Does anyone know how I can get all these values without changing the xml?
Take a look at this: http://msdn.microsoft.com/en-us/library/ms188282.aspx
This isn't tested, but your query would look something like this
SELECT T2.ID.query('.')
CROSS APPLY #idPolygonXML.nodes('/polygons/id') as T2(ID)

FOR XML AUTO and sql server 2005

SELECT * FROM emp FOR XML AUTO , ROOT ('Employees') , ELEMENTS
this query return value in xml format but total xml is showing in one column and column name is showing dynamically. if i want that i will specify the column name like data etc. so xml value will show as single row & column but header or output column name will be "DATA"
is it possible if yes the please show me the way.thanks
You need to check out the new FOR XML PATH feature in SQL Server 2005 and up - read all about it in SQL Server Books Online.
Basically, with FOR XML PATH, you can define the shape of your XML very easily, e.g.
SELECT
EmployeeID AS '#EmployeeID',
FirstName,
LastName
FROM dbo.Employees
FOR XML PATH('Employee'), ROOT('AllEmployees')
will generate XML something like:
<AllEmployees>
<Employee EmployeeID="12345">
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Employee>
</AllEmployees>
FOR XML PATH is very flexible and allows you to do a lot of things quite easily and intuitively.