无法检索没有XML的列

时间:2018-06-21 15:02:50

标签: sql sql-server xml tsql split

我在检索非XML列时遇到问题。对于以前的problem,我收到了以下解决方案。我添加了我需要为SQL PM列检索的列。每次运行查询时,都会收到错误的无效列名PM。我缺少什么来检索PM列?

DECLARE @Tabaldata TABLE ( data nvarchar(max), PM nvarchar(max))
INSERT INTO @Tabaldata
SELECT 
'Technology|faa5d304-f2d1-42c3-8d21-e87697b42bdc;Application|56b19e9a-e58a-4c79-a518-b129fb5f499f;Database
|d7425391-8f8c-4aec-be04-9caf2f55584a;Mobile/BYOD|8f0f30e7-d16d-48a3-ad82-cfdd39156760;Networking
|3876dbd8-8cd8-4040-9c67-0633f8477f93;Operating System|10fc2ce4-53fd-4af2-8fd9-9df66a38715f;Reporting|
00307182-43f4-4bbf-9a95-cd8dbf59754a;Security|014e8d4d-4fd9-404c-8db8-13e84c9042fe;User Interface|57d65a47-6ad2-4df7-8d36-acdf3e0a3145;Web Tech|1b9e82eb-5f70-4183-9093-5', 'Testing Project'


SELECT data ActualData, 
        SUBSTRING(data,CHARINDEX(';' ,data)+1,LEN(data)) AS ExpectedData
FROM
(
    SELECT Split.a.value('.','nvarchar(max)') data
    FROM(
    SELECT CAST('<S>'+REPLACE(data,'|','</S><S>')+'</S>'  AS XML) data
    FROM @Tabaldata
    )AS A 
    CROSS APPLY data.nodes('S') AS Split(a)
)dt
WHERE PATINDEX('%[0-9]%',(SUBSTRING(data,CHARINDEX(';' ,data)+1,LEN(data))))=0

2 个答案:

答案 0 :(得分:2)

DECLARE @Tabaldata TABLE ( data nvarchar(max), PM nvarchar(max))
INSERT INTO @Tabaldata
SELECT 
'Technology|faa5d304-f2d1-42c3-8d21-e87697b42bdc;Application|56b19e9a-e58a-4c79-a518-b129fb5f499f;Database
|d7425391-8f8c-4aec-be04-9caf2f55584a;Mobile/BYOD|8f0f30e7-d16d-48a3-ad82-cfdd39156760;Networking
|3876dbd8-8cd8-4040-9c67-0633f8477f93;Operating System|10fc2ce4-53fd-4af2-8fd9-9df66a38715f;Reporting|
00307182-43f4-4bbf-9a95-cd8dbf59754a;Security|014e8d4d-4fd9-404c-8db8-13e84c9042fe;User Interface|57d65a47-6ad2-4df7-8d36-acdf3e0a3145;Web Tech|1b9e82eb-5f70-4183-9093-5', 'Testing Project'


SELECT PM, data ActualData, 
        SUBSTRING(data,CHARINDEX(';' ,data)+1,LEN(data)) AS ExpectedData
FROM
(
    SELECT PM, Split.a.value('.','nvarchar(max)') data
    FROM(
    SELECT PM, CAST('<S>'+REPLACE(data,'|','</S><S>')+'</S>'  AS XML) data
    FROM @Tabaldata
    )AS A 
    CROSS APPLY data.nodes('S') AS Split(a)
)dt
WHERE PATINDEX('%[0-9]%',(SUBSTRING(data,CHARINDEX(';' ,data)+1,LEN(data))))=0

答案 1 :(得分:0)

一举两得更容易:

SELECT PM, CAST('<S><x>'+REPLACE(REPLACE(data,'|','</x><x>'),';','</x></S><S><x>')+'</x></S>'  AS XML) data
FROM @Tabaldata

这导致

<S>
  <x>Technology</x>
  <x>faa5d304-f2d1-42c3-8d21-e87697b42bdc</x>
</S>
<S>
  <x>Application</x>
  <x>56b19e9a-e58a-4c79-a518-b129fb5f499f</x>
</S>
... more of them ...

带有这样的查询

SELECT s.value('x[1]','nvarchar(max)') AS KeyName
      ,s.value('x[2]','nvarchar(max)') AS KeyValue --you might use "uniqueidentifier" to get it typed, but all values must be GUIDs in this case...
FROM
(
    SELECT PM, CAST('<S><x>'+REPLACE(REPLACE(data,'|','</x><x>'),';','</x></S><S><x>')+'</x></S>'  AS XML) data
    FROM @Tabaldata
) AS casted
CROSS APPLY casted.data.nodes(N'/S') AS A(s)

你明白了

KeyName           KeyValue
Technology        faa5d304-f2d1-42c3-8d21-e87697b42bdc
Application       56b19e9a-e58a-4c79-a518-b129fb5f499f
Database          d7425391-8f8c-4aec-be04-9caf2f55584a
...more of them...