2017-08-10 45 views
0

我有一個SQL Server數據庫的XMLUserDetails具有這樣的價值:如何使用SQL查詢編輯XML值?

<fleet> 
    <Setting name="city"/> 
    <UserSettings name="details"> 
     <UserSettings name="A1"> 
      <setting type="1" name="email"/> 
      <setting value="user"/> 
     </UserSettings> 
     <UserSettings name="A2"> 
      <setting type="2" name="phone"/> 
      <setting value="technician"/> 
     </UserSettings> 
    </UserSettings>  
</fleet> 

我會從數據庫中讀取和

<UserSettings name="A2"> . 

有人能指出我添加新設置我應該做什麼?

+1

[這是一個良好的開端(https://docs.microsoft.com/en-us/sql/ t-sql/xml/modify-method-xml-data-type) –

回答

2

您可以使用sql:variable()來引入您在外部創建的值。你也可以使用Xml.exist()發現已經沒有了之前該新條目個XML:

DECLARE @myTbl TABLE(Descr VARCHAR(100),TheXml XML);    
INSERT INTO @myTbl VALUES 
( 
'Setting "A3" doesn''t exist','<fleet> 
    <Setting name="city" /> 
    <UserSettings name="details"> 
     <UserSettings name="A1"> 
      <setting type="1" name="email" /> 
      <setting value="user" /> 
     </UserSettings> 
     <UserSettings name="A2"> 
      <setting type="2" name="phone" /> 
      <setting value="technician" /> 
     </UserSettings> 
    </UserSettings> 
</fleet>' 
) 
,( 
'Setting "A3" exists already','<fleet> 
    <Setting name="city" /> 
    <UserSettings name="details"> 
     <UserSettings name="A1"> 
      <setting type="1" name="email" /> 
      <setting value="user" /> 
     </UserSettings> 
     <UserSettings name="A2"> 
      <setting type="2" name="phone" /> 
      <setting value="technician" /> 
     </UserSettings> 
     <UserSettings name="A3"> 
      <setting type="3" name="A3 setting" /> 
      <setting value="this exists already" /> 
     </UserSettings> 
    </UserSettings> 
</fleet>' 
); 

DECLARE @NewSetting XML= 
     N'<UserSettings name="A3"> 
      <setting type="3" name="new A3" /> 
      <setting value="new data" /> 
     </UserSettings>'; 


UPDATE @myTbl 
SET TheXml.modify('    
insert sql:variable("@NewSetting") 
as last into (/fleet/UserSettings)[1]')   
WHERE TheXml.exist(N'/fleet/UserSettings[@name="details"]/UserSettings[@name="A3"]')=0; 

SELECT * FROM @myTbl  
1

SQL SERVER向modify XML數據提供方法,並提供一種語言來執行此操作(XML DML)。
您需要對insert xml dml語句使用modify方法。

下面是完整的例子:

的樣本數據:

DECLARE @myDoc xml;    
SET @myDoc = 
'<fleet> 
    <Setting name="city" /> 
    <UserSettings name="details"> 
     <UserSettings name="A1"> 
      <setting type="1" name="email" /> 
      <setting value="user" /> 
     </UserSettings> 
     <UserSettings name="A2"> 
      <setting type="2" name="phone" /> 
      <setting value="technician" /> 
     </UserSettings> 
    </UserSettings> 
</fleet>'; 

聲明:

SET @myDoc.modify('    
insert <setting value="NEW SETTINGS" /> 
as first into (/fleet/UserSettings/UserSettings[@name="A2"])[1] ') ;   
SELECT @myDoc;   

結果:

<fleet> 
    <Setting name="city" /> 
    <UserSettings name="details"> 
     <UserSettings name="A1"> 
      <setting type="1" name="email" /> 
      <setting value="user" /> 
     </UserSettings> 
     <UserSettings name="A2"> 
      <setting value="NEW SETTINGS" /> 
      <setting type="2" name="phone" /> 
      <setting value="technician" /> 
     </UserSettings> 
    </UserSettings> 
</fleet> 
+0

感謝您的解釋..不可能將列值作爲xml並插入新的設置,如果它沒有找到? ... – user1687824

+0

我猜是有的,但我不知道如何。我已經回答了您的問題,主要是因爲這是我瞭解有關SQL Server的XML支持的更多機會。我建議使用我在評論和答案中給你們的鏈接,並像我一樣學習。 –