我的问题是基于XML列构建SQL视图。
我当前的表有两列。一个用于时间戳,另一个用于数据。 数据列来自XML数据类型,内容为:
<data>
<entry header ="Parameter1" value="1" type="System.Boolean"/>
<entry header ="Parameter2" value="0" type="System.Boolean"/>
<entry header ="Parameter3" value="140" type="System.int16"/>
<entry header ="Parameter4" value="80" type="System.int16"/>
<entry header ="Parameter5" value="530602-455605" type="System.String"/>
</data>
现在我要创建以下视图:
Timestamp | Parameter1 | Parameter 2 | Parameter 3 | Parameter 4 | Parameter 5
--------------------+------------+-------------+-------------+-------------+---------------------
2016-10-24 11:30:00 | 1 | 0 | 140 | 80 | 530602-455605
我该怎么做?
SELECT TOP 1
Timestamp
Par1...
Par2...
ParN...
FROM
TableName
ORDER BY
Timestamp DESC
提前致谢!
我得到了以下结果(抱歉格式化):
Timestamp Header Value ValueType
2016-10-24 08:00:00.000 Abguss1 1 System.Boolean
2016-10-24 08:00:00.000 Abguss 0 System.Boolean
2016-10-24 08:00:00.000 Abgussgewicht 140 System.int16
2016-10-24 08:00:00.000 Stopfen 80 System.int16
2016-10-24 08:00:00.000 Werkzeug 530602-455605 System.String
2016-10-24 08:00:20.000 Abguss1 1 System.Boolean
2016-10-24 08:00:20.000 Abguss 0 System.Boolean
2016-10-24 08:00:20.000 Abgussgewicht 138 System.int16
2016-10-24 08:00:20.000 Stopfen 80 System.int16
2016-10-24 08:00:20.000 Werkzeug 530602-455605 System.String
2016-10-24 08:00:40.000 Abguss1 1 System.Boolean
2016-10-24 08:00:40.000 Abguss 0 System.Boolean
2016-10-24 08:00:40.000 Abgussgewicht 178 System.int16
2016-10-24 08:00:40.000 Stopfen 80 System.int16
2016-10-24 08:00:40.000 Werkzeug 530602-455605 System.String
2016-10-24 08:01:20.000 Abguss1 1 System.Boolean
2016-10-24 08:01:20.000 Abguss 0 System.Boolean
2016-10-24 08:01:20.000 Abgussgewicht 158 System.int16
2016-10-24 08:01:20.000 Stopfen 80 System.int16
2016-10-24 08:01:20.000 Werkzeug 530602-455605 System.String
2016-10-24 08:01:43.000 Abguss1 1 System.Boolean
2016-10-24 08:01:43.000 Abguss 0 System.Boolean
2016-10-24 08:01:43.000 Abgussgewicht 162 System.int16
2016-10-24 08:01:43.000 Stopfen 80 System.int16
2016-10-24 08:01:43.000 Werkzeug 530602-455605 System.String
答案 0 :(得分:2)
尝试这样
DECLARE @tbl TABLE(ID INT, YourTime DATETIME, YourData XML);
INSERT INTO @tbl VALUES(1,GETDATE(),N'<data>
<entry header ="Parameter1" value="1" type="System.Boolean"/>
<entry header ="Parameter2" value="0" type="System.Boolean"/>
<entry header ="Parameter3" value="140" type="System.int16"/>
<entry header ="Parameter4" value="80" type="System.int16"/>
<entry header ="Parameter5" value="530602-455605" type="System.String"/>
</data>');
SELECT ID
,YourTime
,YourData.value('(/data/entry[@header="Parameter1"]/@value)[1]','bit') AS Param1
,YourData.value('(/data/entry[@header="Parameter2"]/@value)[1]','bit') AS Param2
,YourData.value('(/data/entry[@header="Parameter3"]/@value)[1]','int') AS Param3
,YourData.value('(/data/entry[@header="Parameter4"]/@value)[1]','int') AS Param4
,YourData.value('(/data/entry[@header="Parameter5"]/@value)[1]','nvarchar(max)') AS Param5
FROM @tbl
显然您的数据存在问题...请尝试以下语句查找所有<entry>
- 元素并尝试查找,哪一个会扰乱阅读过程......
SELECT ID --or TimeStamp-Column or any other row-defining column
,e.value('@header','nvarchar(max)') AS Header
,e.value('@value','nvarchar(max)') AS Value
,e.value('@type','nvarchar(max)') AS ValueType
FROM YourTable
CROSS APPLY YourData.nodes('/data/entry') AS A(e)
我的阅读代码将选择具有给定标题名称的条目的值,并将其转换为给定的目标类型。只要你告诉我 - 带有连字符的长号位于“Parameter5”中,bit
就不会出现投射错误......
您发布的示例数据清楚地表明,您的XML不会被称为Parameter1
到Parameter5
,而是使用德语单词。试试这样:
SELECT ID
,YourTime
,YourData.value('(/data/entry[@header="Abguss1"]/@value)[1]','bit') AS Abguss1
,YourData.value('(/data/entry[@header="Abguss"]/@value)[1]','bit') AS Abguss
,YourData.value('(/data/entry[@header="Abgussgewicht"]/@value)[1]','int') AS Abgussgewicht
,YourData.value('(/data/entry[@header="Stopfen"]/@value)[1]','int') AS Stopfen
,YourData.value('(/data/entry[@header="Werkzeug"]/@value)[1]','nvarchar(max)') AS Werkzeug
FROM YourTable
IchwünschevielGlückbeimLösenderAufgabe: - )