xquery
XQuery is a query language for XML. XML is a markup language that provides software and hardware-independent way of storing, transporting, and sharing data. In this case, XML documents are stored in a SQL Server database. XQuery is used to manipulate, find and extract information stored in the XML format. It’s analogous to SQL for databases.
XQuery是XML的查询语言。 XML是一种标记语言,它提供独立于软件和硬件的方式来存储,传输和共享数据。 在这种情况下,XML文档存储在SQL Server数据库中。 XQuery用于处理,查找和提取以XML格式存储的信息。 它类似于数据库SQL。
In this article, we’ll introduce different ways to find and replace XML data using SQL Server XQuery.
在本文中,我们将介绍使用SQL Server XQuery查找和替换XML数据的不同方法。
Previously, a developer would update XML information by replacing the full XML document in the table column where it resided. But in the latest versions of SQL Server, we can use the XML modify() function in XQuery to update the XML data type column. We’ll look at both the old and new ways to query and update the data.
以前,开发人员将通过替换其所在的表列中的完整XML文档来更新XML信息。 但是在最新版本SQL Server中,我们可以在XQuery中使用XML Modify()函数来更新XML数据类型列。 我们将研究查询和更新数据的旧方法和新方法。
Refer to the sample table below, called user_details. It has an XML data type column labeled xml_data, and each row has an XML document containing detailed information. For user_id = 1, the value of the fname key is “Jo.” We will update the same row using different methods.
请参阅下面的示例表,名为user_details。 它有一个标记为xml_data的XML数据类型列,每一行都有一个包含详细信息的XML文档。 对于user_id = 1 , fname键的值为“ Jo”。 我们将使用不同的方法更新同一行。
替换XML文档 (Replace XML document)
Replacing an entire XML document is required in SQL Server when a specific key-set isn’t updating. Here, a full XML document is supplanted by a new one. This generally occurs when a complete XML document is changed by a customer application.
当特定键集未更新时,在SQL Server中需要替换整个XML文档。 在这里,完整的XML文档被一个新的XML文档取代。 当客户应用程序更改了完整的XML文档时,通常会发生这种情况。
In this example, there is a full XML replacement with an update statement:
在此示例中,使用update语句完全替换了XML:
UPDATE user_details
SET xml_data = '<user>
<user_id>1</user_id>
<fname>Josef</fname>
<lname>Brown</lname>
<email_>jo0@adventure-works.com</email_>
</user>'
WHERE user_id = 1
替换XML列中的键值 (Replace key value in the XML column)
To replace a key value in the XML column, a developer would have replaced the XML for the specific key-set:
要替换XML列中的键值,开发人员将用XML替换特定的键集:
UPDATE user_details
SET xml_data = REPLACE(CAST(xml_data AS NVARCHAR(MAX)), '<fname>Jo</fname>', '<fname>Josef</fname>')
WHERE user_id = 1
Note that the replace function is applied with casting on the XML data type column to avoid a SQL Server error, as shown below:
请注意,replace函数与强制转换一起应用于XML数据类型列,以避免SQL Server错误,如下所示:
UPDATE user_details
SET xml_data = REPLACE(xml_data, '<fname>Jo</fname>', '<fname>Josef</fname>')
WHERE user_id = 1
Msg 8116, Level 16, State 1, Line 14
Argument data type xml is invalid for argument 1 of replace function.
Msg 8116,第16级,状态1,第14行
参数数据类型xml对于replace函数的参数1无效。
Replace with an update statement replaces all XML tags in the XML document that can cause an issue. When the client needs to replace a specific XML element or attribute value, XQuery is a good solution for targeting the particular element or attribute.
用更新语句替换会替换XML文档中所有可能引起问题的XML标记。 当客户端需要替换特定的XML元素或属性值时,XQuery是针对特定元素或属性的很好的解决方案。
插入,替换或删除XML标签–使用XQuery在XML数据类型列或XML文档中设置的值 (Insert, replace or delete XML tag – value set in XML data type column or XML document using XQuery)
The modify() function in XQuery allows us to change a value in the XML document stored in the XML data type column. In the traditional model, we retrieve the XML, update the details and replace it. The modify() is the DML based function with an argument of XPATH to perform insert, update or delete the elements or attributes with the XML based column in the table. Now, we’ll look at some alternative approaches.
XQuery中的Modify()函数允许我们更改存储在XML数据类型列中的XML文档中的值。 在传统模型中,我们检索XML,更新细节并替换它。 Modify()是基于DML的函数,带有XPATH的参数,可以执行插入,更新或删除表中基于XML的列的元素或属性。 现在,我们将讨论一些替代方法。
插入XML标签 (Insert XML tag)
Within the modify() function, Insert allows us to add a new XML tag to a specified XML path location. This XQuery command needs two parameters a new XML tag with the value that will be inserted; and the XML Path where the new XML tag will be published:
在Modify()函数中,Insert允许我们将新的XML标签添加到指定的XML路径位置。 该XQuery命令需要两个参数,即带有将插入值的新XML标记; 以及将发布新XML标签的XML路径:
xml.modify(insert "new_tag" into "xml_path(Can be Conditional)")
XML path can be conditional because the path can exist with multiple parent tags. So, the condition can bifurcate to be inserted within the proper address in the XQuery. For example:
XML路径可以是有条件的,因为该路径可以与多个父标记一起存在。 因此,条件可以分叉以插入到XQuery的适当地址中。 例如:
UPDATE user_details
SET xml_data.modify('insert <gender>Male</gender> into (/user)[1]')
WHERE user_id = 2
With the same path, there can be a multiple-element in the XML. To perform the update or delete action on the specific target XML element, XQuery should be defined with the element id with [1], [2]… or apply a condition on an attribute if it exists.
使用相同的路径,XML中可以有多个元素。 要对特定的目标XML元素执行更新或删除操作,应使用元素ID和[1],[2]…来定义XQuery,或者对属性应用条件(如果存在)。
替换XML标签 (Replace XML tag)
Within the modify() function, the Replace parameter allows us to modify the value of a specific XML-tag in the XML data type column or variable. XQuery needs an XML path and a new value with which to update an existing value in the XML tag:
在Modify()函数中,Replace参数允许我们修改XML数据类型列或变量中特定XML标签的值。 XQuery需要一个XML路径和一个新值,用于更新XML标记中的现有值:
xml.modify(replace value of "xml_path" with "new_value")
Here, the XML path can be conditional because it is possible to have the same tags multiple times on the derived path in the XML. If multiple tags exist and the user does not validate the condition to update them, the tags can be updated by the query. See the example below:
在此,XML路径可能是有条件的,因为在XML的派生路径上可能多次具有相同的标签。 如果存在多个标签,并且用户不验证更新它们的条件,则可以通过查询来更新标签。 请参阅以下示例:
UPDATE user_details
SET xml_data.modify('replace value of (/user/fname/text()) with "Josef"')
WHERE user_id = 1
目标XML路径如何有条件? (How can the target XML path be conditional?)
In an XML document, different tags can exist with a similar name on the target XML path mentioned in the modify() function. So, the modify() function will carry out the activity on an inappropriate XML tag which is situated in the same position in the target XML path:
在XML文档中,modify()函数中提到的目标XML路径上可以存在具有相似名称的不同标签。 因此,modify()函数将对目标XML路径中位于相同位置的不合适的XML标记执行活动:
<users>
<user user_id="1">
<fname>Josef</fname>
<lname>Brown</lname>
<email_>jo0@adventure-works.com</email_>
<gender>Male</gender>
</user>
<user user_id="2">
<fname>Katie</fname>
<lname>McAskill-White</lname>
<email_>katie0@adventure-works.com</email_>
</user>
</users>
For example, consider XML that has multiple tags with the same name in the XML tag <users>. When we execute the above statement without a condition, then the first fname will be updated with a new value on the targeted XML path. But when a user wants to modify a specific element of those, then there must be a condition to filter the element. The filter can be applied by adding a condition on attribute or element with a specific value, as shown below.
例如,考虑XML标记<users>中具有多个具有相同名称的标记的XML。 当我们不带条件地执行上述语句时,第一个fname将在目标XML路径上用新值更新。 但是,当用户想要修改其中的特定元素时,则必须存在过滤该元素的条件。 可以通过在属性或元素上添加具有特定值的条件来应用过滤器,如下所示。
xml.modify('replace value of (/users/user[@user_id=("2")]/fname/text()) with "Kat"')
Here, filter applied by <user user_id=”2″> with adding [@user_id=(“2”)] in the XML path. Even there can be multiple filters with different tags in the XML.
在此,由<user user_id =” 2”>应用的过滤器在XML路径中添加[@user_id =(“ 2”)] 。 XML中甚至可以有多个带有不同标签的过滤器。
删除XML标签 (Delete XML tag)
Within the modify() function, the Delete parameter allows us to delete XML-tags in an XML column or XML variable. A query needs the XML path to delete the target XML tags. See the example:
在Modify()函数中,Delete参数允许我们删除XML列或XML变量中的XML标签。 查询需要XML路径才能删除目标XML标记。 参见示例:
UPDATE user_details
SET xml_data.modify('delete (/user/gender)')
WHERE user_id = 2
This article describes the feature of XQuery and related implicit functions with the data model. We’ve explained different ways to update XML in SQL Server and the modify() function support in XQuery.
本文介绍了XQuery的功能以及与数据模型相关的隐式函数。 我们已经解释了在SQL Server中更新XML的不同方法以及XQuery中对Modify()函数的支持。
翻译自: https://www.sqlshack.com/different-ways-to-update-xml-using-xquery-in-sql-server/
xquery