2014-02-19 106 views
1

在XML文本的下方,我想寫的更新腳本來2013-12-30T04:30:00.000+00:00修改日期部分與dateadd(minute, 2, getdate())T-SQL代碼編輯XML

  1. 我如何獲得的格式爲XML文本表示。

  2. 連接日期的最佳方法是什麼?

<ScheduleDefinition xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <StartDateTime xmlns="http://schemas.microsoft.com/sqlserver/2006/03/15/reporting/reportingservices">2013-12-30T04:30:00.000+00:00</StartDateTime> 
    <WeeklyRecurrence xmlns="http://schemas.microsoft.com/sqlserver/2006/03/15/reporting/reportingservices"> 
    <WeeksInterval>1</WeeksInterval> 
    <DaysOfWeek> 
     <Sunday>true</Sunday> 
     <Monday>true</Monday> 
     <Tuesday>true</Tuesday> 
     <Wednesday>true</Wednesday> 
     <Thursday>true</Thursday> 
     <Friday>true</Friday> 
     <Saturday>true</Saturday> 
    </DaysOfWeek> 
    </WeeklyRecurrence> 
</ScheduleDefinition> 

回答

1

此: -

declare @xml varchar(max) 

set @xml='<ScheduleDefinition xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <StartDateTime xmlns="http://schemas.microsoft.com/sqlserver/2006/03/15/reporting/reportingservices">2013-12-30T04:30:00.000+00:00</StartDateTime> 
    <WeeklyRecurrence xmlns="http://schemas.microsoft.com/sqlserver/2006/03/15/reporting/reportingservices"> 
    <WeeksInterval>1</WeeksInterval> 
    <DaysOfWeek> 
     <Sunday>true</Sunday> 
     <Monday>true</Monday> 
     <Tuesday>true</Tuesday> 
     <Wednesday>true</Wednesday> 
     <Thursday>true</Thursday> 
     <Friday>true</Friday> 
     <Saturday>true</Saturday> 
    </DaysOfWeek> 
    </WeeklyRecurrence> 
</ScheduleDefinition>' 

declare @pos int 

set @pos=charindex('</StartDateTime>',@xml) 

select left(@xml,@pos-30)+ 
    convert(varchar(23),dateadd(minute,2,sysutcdatetime()),126)+'+00:00'+ 
    substring(@xml,@pos,datalength(@xml)) 

返回: -

<ScheduleDefinition xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <StartDateTime xmlns="http://schemas.microsoft.com/sqlserver/2006/03/15/reporting/reportingservices">2014-02-19T21:27:36.348+00:00</StartDateTime> 
    <WeeklyRecurrence xmlns="http://schemas.microsoft.com/sqlserver/2006/03/15/reporting/reportingservices"> 
    <WeeksInterval>1</WeeksInterval> 
    <DaysOfWeek> 
    <Sunday>true</Sunday> 
    <Monday>true</Monday> 
    <Tuesday>true</Tuesday> 
    <Wednesday>true</Wednesday> 
    <Thursday>true</Thursday> 
    <Friday>true</Friday> 
    <Saturday>true</Saturday> 
    </DaysOfWeek> 
</WeeklyRecurrence> 
</ScheduleDefinition> 

它使用charindex()找到一致的一塊在你的XML文本(</StartDateTime>)的。然後它使用left()來切斷xml的開始(截斷當前日期)。然後它使用sysutcdatetime()來獲取以UTC時間表示的服務器當前時間(以便稍後可以應用+00:00的一致時區偏移量)。然後它使用convert(),風格爲126將時間轉換爲您的xml所需的格式。然後它使用substring()datalength()添加您的xml的結尾(長度不需要精確)。

希望這會給你一些關於如何去切割你的XML以取代你想要的日期的想法。

0

DATETIMEOFFSET數據類型和XML.modify()。如果有方法使用XML,我不建議將XML視爲文本。你的命名空間使它有點棘手。這適用於SQL Server 2008 R2。

DECLARE @XML XML 
DECLARE @NEWVALUE DATETIMEOFFSET 

set @XML='<ScheduleDefinition xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <StartDateTime xmlns="http://schemas.microsoft.com/sqlserver/2006/03/15/reporting/reportingservices">2013-12-30T04:30:00.000+00:00</StartDateTime> 
    <WeeklyRecurrence xmlns="http://schemas.microsoft.com/sqlserver/2006/03/15/reporting/reportingservices"> 
    <WeeksInterval>1</WeeksInterval> 
    <DaysOfWeek> 
     <Sunday>true</Sunday> 
     <Monday>true</Monday> 
     <Tuesday>true</Tuesday> 
     <Wednesday>true</Wednesday> 
     <Thursday>true</Thursday> 
     <Friday>true</Friday> 
     <Saturday>true</Saturday> 
    </DaysOfWeek> 
    </WeeklyRecurrence> 
</ScheduleDefinition>' 

;WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2006/03/15/reporting/reportingservices' AS X) 
SELECT @NEWVALUE = TableAlias.FieldAlias.value('(X:StartDateTime/text())[1]', 'DATETIMEOFFSET') FROM @XML.nodes('//ScheduleDefinition') AS TableAlias(FieldAlias) 

SET @NEWVALUE = DATEADD(MINUTE,2,@NEWVALUE) 
DECLARE @FORMAT VARCHAR(max) = CONVERT(VARCHAR(MAX),@NEWVALUE,126) 

SET @XML.modify(' 
      declare namespace x="http://schemas.microsoft.com/sqlserver/2006/03/15/reporting/reportingservices"; 
      replace value of (/ScheduleDefinition/x:StartDateTime[1]/text())[1] 
      with sql:variable("@FORMAT") 
      ') 
SELECT @XML