2017-07-25 84 views
0

我見過的帖子與我要問的內容類似,但找不到任何能夠真正解決我問題的內容。我已經看到(並重復)將XML標籤獲取到SQL中,但沒有將標籤的屬性獲取到SQL中。XML屬性到SQL

背景:我們使用基於每個事件的計劃運行「事件」的程序。時間表是用XML表示的。最終,我試圖根據其時間表比較上次運行特定事件和上次運行時間。一旦我將計劃從XML中提取出來並放入一個或多個表格中,我相信我可以從中取出它。但是,我在第一步就拼盡全力。

下面是我試圖進入一個或多個表格的XML。任何幫助將不勝感激!!

<Schedule LastModified="2016-06-27T21:02:10.6041531Z" TimeZone="(UTC-06:00) Central Time (US &amp; Canada)" ConvertedToUTC="True" Type="Weekly"> 
    <Beginning StartDate="2016-05-26T22:26:00.0000000" /> 
    <Block BlockType="AllDay" Interval="10" IntervalType="Minute" SetType="Inclusive" Start="15:00:00" End="17:00:00" Duration="02:00:00" /> 
    <Interval Type="Weekly" RecurEveryX="1" Sunday="False" Monday="True" Tuesday="True" Wednesday="True" Thursday="True" Friday="True" Saturday="False" /> 
    <Ending Type="NoEndDate" /> 
</Schedule> 

回答

0

不知道,你有你的問題......讀了這XML是非常不重要的(沒有代碼的所有值的,但你的想法):

DECLARE @mockup TABLE(YourXML XML); 
INSERT INTO @mockup VALUES 
(N'<Schedule LastModified="2016-06-27T21:02:10.6041531Z" TimeZone="(UTC-06:00) Central Time (US &amp; Canada)" ConvertedToUTC="True" Type="Weekly"> 
    <Beginning StartDate="2016-05-26T22:26:00.0000000" /> 
    <Block BlockType="AllDay" Interval="10" IntervalType="Minute" SetType="Inclusive" Start="15:00:00" End="17:00:00" Duration="02:00:00" /> 
    <Interval Type="Weekly" RecurEveryX="1" Sunday="False" Monday="True" Tuesday="True" Wednesday="True" Thursday="True" Friday="True" Saturday="False" /> 
    <Ending Type="NoEndDate" /> 
</Schedule>'); 

SELECT m.YourXML.value(N'(/Schedule/@LastModified)[1]',N'datetime') AS Schedule_LastModified 
     ,m.YourXML.value(N'(/Schedule/@TimeZone)[1]',N'nvarchar(max)') AS Schedule_TimeZone 
     ,m.YourXML.value(N'(/Schedule/Beginning/@StartDate)[1]',N'datetime') AS Beginning_StartDate 
     ,m.YourXML.value(N'(/Schedule/Block/@BlockType)[1]',N'nvarchar(max)') AS Block_BlockType 
     ,m.YourXML.value(N'(/Schedule/Block/@Interval)[1]',N'int') AS Block_Interval 
     ,m.YourXML.value(N'(/Schedule/Interval/@Type)[1]',N'nvarchar(max)') AS Interval_Type 
FROM @mockup AS m; 

如果這不是您的解決方案,請編輯您的問題:添加您自己的嘗試,錯誤的輸出和預期的輸出。你的解釋並沒有讓我清楚......