如何使用SQL Server 2008在每個XML節點中設置參數?獲取每個XML節點的值並將其設置爲參數
我新的SQL和我有這樣
Set @xml='<root><name>John </name><age>8</age></root>'
我需要設置節點<name>
,<age>
的值參數的XML數據。例如:
Set @name='get value of node <name>'
Set @age='the value of <age>'
這可能嗎?謝謝!
如何使用SQL Server 2008在每個XML節點中設置參數?獲取每個XML節點的值並將其設置爲參數
我新的SQL和我有這樣
Set @xml='<root><name>John </name><age>8</age></root>'
我需要設置節點<name>
,<age>
的值參數的XML數據。例如:
Set @name='get value of node <name>'
Set @age='the value of <age>'
這可能嗎?謝謝!
如果我正確地得到這個結果,你會得到一個XML中的參數鍵值對。你試圖實現的是將值讀入擬合變量。試着這樣說:
DECLARE @xml XML;
SET @xml='<root><name>John </name><age>8</age></root>';
DECLARE @name NVARCHAR(100);
DECLARE @age INT;
SET @[email protected]('(/root/name)[1]','nvarchar(max)');
SET @age [email protected]('(/root/age)[1]','int');
SELECT @name,@age;
如果你需要從一個變量的名稱查詢中值,您可以使用此:
DECLARE @nodeName NVARCHAR(100)='name';
SET @[email protected]('(/root/*[local-name()=sql:variable("@nodeName")])[1]','nvarchar(max)');
的中心思想是使用一個XQuery
-expression (/root/*[local-name()=sql:variable("@nodeName")])[1]
到選擇第一個節點的值,其中元素的名稱與給定的參數相同
*「我需要在參數*中設置節點
@TT。那麼,我不知道......你所說的句子在我看來就像*我想從'
是的,它可以用兩種方式解釋。 –
您想在幾個字符串中替換「」和「」的任何實例,存儲在變量中的XML中的相應元素值...正確?或者,您是否想要獲取XML中的* any *元素的元素值,並用這些字符串中的這些標記的任何*發生替換? –