我是SQL Server中XML查询的新手。我想写一个
的查询如下面的示例所示:我的列类型为nvarchar(max)
并存储值:
<Trainings>
<Training>
<Id>10</Id>
<Data>test1</Data>
<ItemOrder>1</ItemOrder>
</Training>
<Training>
<Id>12</Id>
<Data>test12</Data>
<ItemOrder>2</ItemOrder>
</Training>
<Training>
<Id>101</Id>
<Data>test111</Data>
<ItemOrder>3</ItemOrder>
</Training>
<Training>
<Id>102</Id>
<Data>test122</Data>
<ItemOrder>4</ItemOrder>
</Training>
</Trainings>
预期结果必须是:
<Trainings>
<Training>
<Id>10</Id>
<Data>test1</Data>
<ItemOrder>1</ItemOrder>
</Training>
<Training>
<Id>101</Id>
<Data>test111</Data>
<ItemOrder>2</ItemOrder>
</Training>
<Training>
<Id>102</Id>
<Data>test122</Data>
<ItemOrder>3</ItemOrder>
</Training>
</Trainings>
我已尝试使用如下查询,但如何在Training
孩子中找到test12
Data
节点号?然后为所有ItemOrder
个节点重新排序所有Training
。
CREATE TABLE Resume ([Training] nvarchar(max));
INSERT INTO Resume ([Training])
VALUES ('<Trainings><Training><Id>10</Id><Data>test1</Data><ItemOrder>1</ItemOrder></Training><Training><Id>12</Id><Data>test12</Data><ItemOrder>2</ItemOrder></Training><Training><Id>101</Id><Data>test111</Data><ItemOrder>3</ItemOrder></Training><Training><Id>102</Id><Data>test122</Data><ItemOrder>4</ItemOrder></Training></Trainings>');
DECLARE @string NVARCHAR(MAX)
SELECT @string = [Training]
FROM Resume
DECLARE @xml XML
SET @xml = CONVERT(XML, @string)
SET @xml.modify('delete (/Trainings/Training)[2]')
SET @string = CONVERT(NVARCHAR(MAX), @xml)
SELECT @string
以下是小提琴链接:http://sqlfiddle.com/#!6/894a0/3
答案 0 :(得分:1)
将XPath与您搜索的谓词一起使用
public class EntityEnterPatientDiagnosisDetails
{
public int PatientId { get; set; }
public string Symptoms { get; set; }
public string DiagnosisProvided { get; set;}
public string AdministeredBy { get; set; }
public DateTime DateofDiagnosis { get; set; }
public string FollowUpRequired { get; set; }
public DateTime FollowUpDate { get; set; }
/// <summary>
/// Bill Part
/// </summary>
public int BillAmount { get; set; }
public string ModeOfPayment { get; set; }
public string CardNumber { get; set; }
}
更新:为了解决第二个问题,我建议使用完全不同的XML关系方法:
set @xml.modify('delete (/Trainings/Training[./Data = "test12"])')