简体   繁体   English

如何从nvarchar(max)类型的列中存储和提取XML信息,并在联接中使用它?

[英]How to store and extract XML information from an nvarchar(max) type column, and use it in joins?

I have a column of type 'nvarchar(max)' that should now hold XML information instead of just a string. 我有一个类型为'nvarchar(max)'的列,该列现在应包含XML信息,而不仅仅是字符串。

Say: col1 has value 'abc' Now it has values, with additional info: 说:col1具有值'abc'现在它具有值,以及其他信息:

<el1>abc</el2>
<el2>someotherinfo</el2>

Storing the information to the column is fine, since it can still be pushed in as a string. 将信息存储到列中很好,因为仍然可以将其作为字符串推入。 However, extracting the same information and also using/replacing the same information 'abc' from this column that is being used in various other joins from other tables, is something I'm not able to figure out. 但是,我无法弄清楚从同一列中提取相同的信息并使用/替换相同信息“ abc”,而该信息正在其他表的各种其他联接中使用。

how can I also push in this information into abcd when it comes from another table's value 'abcd' without losing other information? 当来自另一个表的值“ abcd”的信息也不会丢失其他信息时,如何将其推入abcd?

I am building an XML from the application side and updating it in a column of type nvarchar(). 我正在从应用程序端构建XML,并在nvarchar()类型的列中对其进行更新。 All the columns have been replaced to hold the XML, so the safe assumption is that the col1 only holds XML similar to that mentioned above. 所有列已被替换以保存XML,因此安全的假设是col1仅保存与上述类似的XML。 Just push the XML as is and it works fine. 只需按原样推送XML,它就可以正常工作。 However, how should I extract the information to use it in joins? 但是,我应该如何提取信息以在联接中使用它?

How do I extract a particular element from this nvarchar() string to use it in a join?? 如何从此nvarchar()字符串中提取特定元素以在联接中使用它? Previously, this column 'Col1' was just used as a string, and a check was done like this: 以前,此列'Col1'只是用作字符串,并且进行了如下检查:

where tablex.colx = table1.col1

or Update Table2 where 或更新Table2,其中

Once you cast the NVARCHAR data to the XML data type, you can use XML functions to get element/attribute values for joining to: 将NVARCHAR数据转换为XML数据类型后,就可以使用XML函数来获取要加入的元素/属性值:

WITH xoutput AS (
  SELECT CONVERT(xml, t.nvarchar_column) AS col
    FROM YOUR_TABLE t)
SELECT x.*
  FROM TABLE x
  JOIN xoutput y ON y.col.value('(/path/to/your/element)[1]', 'int') = x.id

It won't be able to use indexes, because of the data type conversion... 由于数据类型转换,它将无法使用索引...

Alternate version, using IN: 备用版本,使用IN:

WITH xoutput AS (
   SELECT CONVERT(xml, t.nvarchar_column) AS col
     FROM YOUR_TABLE t)
SELECT x.*
  FROM TABLE x
 WHERE x.id IN (SELECT y.col.value('(/path/to/your/element)[1]', 'int')
                  FROM xoutput)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM