[英]SQL View based on a xml column
My problem is to build a SQL view based on a XML column. 我的问题是建立基于XML列的SQL视图。
My current table has two columns. 我当前的表有两列。 One for timestamp and second for data. 一个用于时间戳,第二个用于数据。 The data-column is from XML datatype and the content is: 数据列来自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>
Now I want to create following view: 现在我要创建以下视图:
Timestamp | Parameter1 | Parameter 2 | Parameter 3 | Parameter 4 | Parameter 5
--------------------+------------+-------------+-------------+-------------+---------------------
2016-10-24 11:30:00 | 1 | 0 | 140 | 80 | 530602-455605
How can I do this? 我怎样才能做到这一点?
SELECT TOP 1
Timestamp
Par1...
Par2...
ParN...
FROM
TableName
ORDER BY
Timestamp DESC
Thanks in advance! 提前致谢!
I got the following result (sorry for format): 我得到以下结果(对不起格式):
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
Try it like this 像这样尝试
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
Obviously there is a problem with your data... Try the following statement to find all <entry>
-elements and try to find, which one disturbs the reading process... 显然您的数据有问题...请尝试以下语句查找所有<entry>
-elements并尝试查找,这会干扰读取过程...
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)
My code for reading will pick the value of the entry with the given header name and cast this to the given target type. 我的读取代码将选择具有给定标题名称的条目的值,并将其转换为给定的目标类型。 As long - as you tell me - the long number with the hyphen is sitting in "Parameter5" there cannot be a cast error with bit
... 只要-您告诉我- 带连字符的长数字位于“ Parameter5”中,就不会有bit
错误。
Your posted example data shows clearly, that your XML is not called Parameter1
to Parameter5
but with german words. 您发布的示例数据清楚地表明,您的XML并不是从Parameter1
到Parameter5
而是用德语单词。 Try it like this: 像这样尝试:
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
Ich wünsche viel Glück beim Lösen der Aufgabe :-) IchwünschevielGlückbeimLösender Aufgabe :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.