简体   繁体   中英

How can I query a value in SQL Server TEXT column that contains XML (not xml column type)

I have table DOCUMENTS with:

DOCUMENTS
____________________
DOCUMENTID   int
USERID       int
CONTENT      text

I have following XML stored in TEXT column with name CONTENT in a SQL Server database

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IDMSDocument>
    <DocumentContent>
        <Attribute Name="Number" GUID="{FFFFFFFF-0000-0000-0000-000000000001}">
            <IDMSType>3060</IDMSType>
            <Value Type="Integer">
                <Value>122</Value>
            </Value>
        </Attribute>
        <Attribute Name="Date" GUID="{FFFFFFFF-0000-0000-0000-000000000002}">
            <IDMSType>3061</IDMSType>
            <Value Type="DateTime">
                <Date>10-09-2014</Date>
            </Value>
        </Attribute>
        <Attribute Name="Публикуване" GUID="{CA646F55-5229-4FC5-AA27-494B25023F4E}">
            <IDMSType>3062</IDMSType>
            <Value Type="String">
                <Value>Да</Value>
            </Value>
        </Attribute>
        <Attribute Name="Дата" GUID="{AC0465B0-4FB4-49E2-B4FA-70901068FD9B}">
            <IDMSType>3063</IDMSType>
            <Value Type="DateTime">
                <Date>01-10-2014</Date>
            </Value>
        </Attribute>
        <Attribute Name="Предмет на поръчка" GUID="{04A4EC72-6F33-461F-98DD-D8D271997788}">
            <IDMSType>3065</IDMSType>
            <Value Type="String">
                <Value>Избор на консултанти</Value>
            </Value>
        </Attribute>
    </DocumentContent>
</IDMSDocument>

I find a way how to query dingle XML attribute in a single row from table, with heavy conversion :

DECLARE @strContent NVARCHAR(MAX);
DECLARE @xmlContent XML;
SET @strContent = (select content from DOCUMENTS where DocumentID=24);
SET @xmlContent = CAST(REPLACE(CAST(@strContent AS NVARCHAR(MAX)),'utf-8','utf-16') AS XML) 
SELECT @xmlContent.query('/IDMSDocument/DocumentContent/Attribute[5]/Value/Value') 
where @xmlContent.value('(/IDMSDocument/DocumentContent/Attribute[5]/Value/Value)[1]', 'nvarchar(max)') like '%search%'

I need to make query like "select all rows in table that in fifth attribute in XML have value 'search' "

For me the general problem is that column type is not XML, but I have text column with stored xml inside. when I try cast, query, value directly server return that I can use it Only with XML column.

I would be very grateful if someone suggest how to do this!

Thanks!

Kamen

SQL Server doesn't allow inline XML conversion to have functions applied, ie: no CAST(...).query() , use a CTE to handle the conversion:

;WITH cte AS (
  SELECT DocumentID, UserID, CAST(Content AS XML) AS XMLContent
  FROM Documents
)

SELECT XMLContent.query('/IDMSDocument/DocumentContent/Attribute[5]/Value/Value') 
FROM cte
WHERE XMLContent.value('(/IDMSDocument/DocumentContent/Attribute[5]/Value/Value)[1]', 'nvarchar(max)') like '%search%'

One thing though: I saw Unicode (Russian?) characters in the Content column. It may be better to use utf-16 in your XML and ntext for column type.

text and ntext are also on the way out . If this is new code, use nvarchar(max)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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