This question already has answers here:
TSQL 2005, XML DML - Update Two Values at once?
(3 answers)
Updating multiple XML nodes using T-SQL [duplicate]
(1 answer)
Closed 8 years ago.
I'm using SQL Server and have Table with XML column. My procedure for insert has input parameter XML document. Is there any way how could I update already existing XML in my table?
This is my OLD XML in my table:
<weather Location="Paris, France">
<forecast>
<description>Sky is clear</description>
<Date>2013-09-19</Date>
<MinTemp>13</MinTemp>
<MaxTemp>20</MaxTemp>
<Humidity>78</Humidity>
<Pressure>1024</Pressure>
<Windspeed>3</WindSpeed>
</forecast>
<forecast>
<description>Sky is clear</description>
<Date>2013-09-20</Date>
<MinTemp>14</MinTemp>
<MaxTemp>21</MaxTemp>
<Humidity>75</Humidity>
<Pressure>1020</Pressure>
<Windspeed>1</WindSpeed>
</forecast> .... 10 times this forecast
</weather>
This is my new one:
<weather Location="Paris, France">
<forecast>
<description>Sky is clear</description>
<Date>2013-09-19</Date>
<MinTemp>14</MinTemp>
<MaxTemp>21</MaxTemp>
<Humidity>70</Humidity>
<Pressure>1000</Pressure>
<Windspeed>5</WindSpeed>
</forecast>
<forecast>
<description>Sky is clear</description>
<Date>2013-09-20</Date>
<MinTemp>17</MinTemp>
<MaxTemp>24</MaxTemp>
<Humidity>68</Humidity>
<Pressure>1024</Pressure>
<Windspeed>3</WindSpeed>
</forecast> .... 10 times this forecast
</weather>
What I want now is to update my old XML with values I read from new one. I know I have to somehow go trough my OLD XML, I think I could do that with ...while(exist) and modify, but how to read value I need from my new XML, how to read let's say data for September 19th, and update September 19th in old XML, then read data for September 20th, then update September 20th in old XML etc etc...?
Thanks for help...
You may try likethis:-
update tbl1
set myXml.modify('replace value of (/weather/forecast/..)[1]
with concat(string(sql:column("columnname")), "value")')
where myXml.exist('/weather/forecast/..') = 1 //some condition
Related
I'm a beginner when it comes to SQL and have no experience with XML so I'm after a little bit of help.
At the moment I am looking at a single table and just using the query below
select
name,
convert(xml, convert(varbinary(max), orders)) ClientOrders
from client;
In the second columns of SQL output, I have a very lengthy bit of XML similar to the example below. I've used "..." just to skip over some of the output and give a general idea.
Name
ClientOrders
Client1
<report ... ><QueryParameter></QueryParameter Name = "#hello1"><commandtext> ...<value>Example1</value>....<value>Example2</value>...<value>Example3</value>...</commandtext></report>
Client2
<report ... ><QueryParameter></QueryParameter Name = "#hello2"><commandtext> ...<value>Example4</value>....<value>Example5</value>...<value>Example6</value>...</commandtext></report>
I have this for a lot of rows and this output is so long that it exceeds the Excel cell character limit. I'm only looking for the values Example1 through to Example6 in the example given above. Is there an SQL command I can run to get the above string between the open and close value?
I am using SSMS version 18.9.1
Cheers
This question already has answers here:
SQL How to Select the most recent date item
(6 answers)
Closed 2 years ago.
I would like to select last inserted data in my table.
I tried :
Source = "SELECT * FROM HistoSpreadLiq WHERE DateSpread=MAX(DateSpread)"
But this doesn't work -> returns : Error Automation..
MAX(DateSpread) should return 04/11/2020
DateSpread is a date format in my AccessDB
And when I hard code:
Source = "SELECT * FROM HistoSpreadLiq WHERE DateSpread=04/11/2020"
It does work, what am I missing?
Please note that I execute this SQL request in Excel and my database is an Access database (.accdb)
Then answer was that I had to do an other select :
"SELECT * FROM HistoSpreadLiq WHERE DateSpread=(SELECT Max(DateSpread) FROM HistoSpreadLiq)"
This question already has answers here:
SQL Server Xml query with multiple namespaces
(3 answers)
Closed 3 years ago.
The problem
I need to take data from XML files provided by another application. These XMLs contain data that is required to be inserted into tables for a database for a different application.
The problem is this, I can't get the values out of the XML file. I keep getting blank/null values instead.
I am using SQL Server 2014 Management Studio.
What I've tried
I've looked into how to read an XML and have that placed into a table. I've gone as far as looking at tutorials that work. But, whenever I try to apply the same process to my tables and using my XML data I keep getting blank values.
Some help would be appreciated as I have been looking into this for a day and researching how to approach this using MS SQL. And I cannot for the life of me understand why I keep getting nulls.
The XML
<DistributionOrder xmlns="a-url">
<Date>2019-03-07T14:38:00</Date>
<Order_Number>ACME01</Order_Number>
<Comment/>
<Status>Active</Status>
<Lines>
<Line>
<Order_Line_Number>1</Order_Line_Number>
<Product>22</Product>
<Status_Line>Active</Status_Line>
<Comment_Line/>
<Location>ENT_78</Location>
</Line>
</Lines>
</DistributionOrder>
The query
DECLARE #xmlData XML;
INSERT INTO dbo.XMLwithOpenXML(XML_data)
SELECT *
FROM OPENROWSET(BULK 'C:\test_file.xml', SINGLE_BLOB) AS ImportSource;
SELECT * FROM XMLwithOpenXML;
SELECT
element.value('(Order_Line_Number/text())[1]', 'nvarchar(80)') AS Order_Line_Number,
element.value('(Product/text())[1]', 'nvarchar(80)') AS Product
FROM
XMLwithOpenXML
CROSS APPLY
XML_data.nodes('DistributionOrder/Lines/Line') AS DO(element)
GO
What I expect
Order_Line_Number | Product
------------------+----------
1 | 22
What I Get
Order_Line_Number | Product
------------------+----------
|
Figured out that the,
<DistributionOrder xmlns="a-url">
</DistributionOrder>
was the problem. I deleted the xmlns="a-url" from the xml and found that my code is working.
Discovered that you can use
;WITH XMLNAMESPACES(DEFAULT 'a-url')
To take the namespace into account.
This question already has answers here:
SQL: How can I get the value of an attribute in XML datatype?
(4 answers)
Closed 4 years ago.
how can i find and split a value between double quotation
Value is like this :
'<Relations mfrid="EnvoeyName_MFR"><Form EC="180" ETC="711" Val="1679" /></Relations>'
value is one filed of table and i want split "EC,ETC,Val" value
i mean 180,711,1679
thanks for your help
You Can try the following
DECLARE #XMLData XML = '<Relations mfrid="EnvoeyName_MFR"><Form EC="180" ETC="711" Val="1679" /></Relations>'
SELECT
EC = Node.Data.value('#EC', 'INT'),
ETC = Node.Data.value('#ETC', 'INT'),
Val = Node.Data.value('#EC', 'INT')
FROM #XMLData.nodes('/Relations/Form') Node(Data)
This question already has an answer here:
Query Data from XML
(1 answer)
Closed 5 years ago.
I have a table in an Oracle 12c Database that contains a XML Type Column and i need to extract a value from this Column.
Here an example of this Column's content:
<AbcMsg xmlns="http://example.org/SCL/CommonTypes">
<Msg>
<Pmnt>
<swif:SWIFT MT="202" xmlns:swif="urn:abc/scl/SWIFT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<swif:Header>
<swif:Input_Output_Identifier>O</swif:Input_Output_Identifier>
<swif:Sender_BIC>XXXXXXXXXXX</swif:Sender_BIC>
<swif:Receiver_BIC>XXXXXXXXXXX</swif:Receiver_BIC>
<swif:Message_Priority>NORM</swif:Message_Priority>
<swif:User_Header>
<swif:Message_User_Reference>XXXXXXXXXXXXXXXXX</swif:Message_User_Reference>
</swif:User_Header>
<swif:Original_Swift>
<swif:Message>DATA TO BE EXTRACTED</swif:Message>
</swif:Original_Swift>
<swif:Create_Date_Time>2016-07-28T15:45:00</swif:Create_Date_Time>
<swif:Msg_ID>POFu7yCXHoN</swif:Msg_ID>
<swif:Sequence_Number>248600</swif:Sequence_Number>
<swif:Session_Number>6184</swif:Session_Number>
</swif:Header>
<swif:Data xsi:type="swif:Data_202">
<swif:F20>
<swif:Sender_s_Reference>POFu7yCXHoN</swif:Sender_s_Reference>
</swif:F20>
<swif:F21>
<swif:Related_Reference>XXXXXXXXXXX</swif:Related_Reference>
</swif:F21>
<swif:F32A>
<swif:Currency>USD</swif:Currency>
<swif:Amount>156020000</swif:Amount>
<swif:Date>2016-07-28</swif:Date>
</swif:F32A>
<swif:Cdrt_F58a_F59a>
<swif:Account>174208531</swif:Account>
<swif:Identifier_Code>XXXXXXXXX</swif:Identifier_Code>
</swif:Cdrt_F58a_F59a>
</swif:Data>
</swif:SWIFT>
</Pmnt>
<Extn/>
</Msg>
</AbcMsg>
I need to retrieve the Value of <swif:Original_Swift>. I've tried the function EXTRACT(xml_column, '/AbcMsg/Msg/Pmnt') but it keeps returning null.
Any clue on how to do that???
Thanks in Advance.
Try xmltable or xmlquery. You have to declare defult and swif namesapce;
select * from xmltable(XMLNAMESPACES('urn:abc/scl/SWIFT' as "swif", default 'http://example.org/SCL/CommonTypes') , '/AbcMsg/Msg/Pmnt/swif:SWIFT/swif:Header/swif:Original_Swift' passing xmltype(your_xml_here));