2010-07-11 56 views
0

我想查询一个表,其中包含直xml数据的列。在查询中,我正在查询包含直接数据的列(int,vchar等),但我也在查询xml列。在xml列中,我想要在xml中获取一个值,如果它不存在,则返回null。我有以下查询几乎可以工作,但返回重复。需要帮忙!SQL查询/ Xml

我有我的根xml CodeFiveReport然后在它的属性,并在该属性中有一个序列号。我试图抓住序列号,如果它存在并显示它。

select Distinct rs.Id 
     , rs.CaseNumber 
     , rs.StartDate 
     , rs.[Status] 
     , rs.PatrolDistrict 
     , rs.PrimaryUnit 
     , rs.Location 
     , rs.ReportType 
     , rs.IncidentType 
     , rs.UserId 
     , rs.UnitId 
     , rs.UnitCode 
     , rs.IsLocked 
     , rs.LockedBy 
     , rs.AgencyId 
     , rl.ReportName 
     , rl.ParentId 
     , TempTable.Party.value('(SerialNumber/text())[1]', 'varchar(50)') as SerialNumber 
    from dbo.vw_ReportSummary rs OUTER APPLY Report.nodes('/CodeFiveReport/Properties/Property') AS TempTable(Party) 
    left outer join dbo.ReportLookup rl on rs.Id = rl.Id 
    where rs.[Status] = 'Approved' 
    order by rs.Id 
+1

如果DISTINCT返回重复项,那些重复项并不重复 - 请检查您的数据! – 2010-07-11 20:22:54

+0

每个结果返回的序列号是不同的......我需要知道的是,如果特定记录的xml包含任何序列号,而不是全部序列号。这是我无法想象如何去做的 – mint 2010-07-11 22:04:44

回答

1

好,我是能够解决的问题

我改变Report.nodes('/CodeFiveReport/Properties/Property')Report.nodes('/CodeFiveReport/Properties')

反过来我也改变了我的不是Temptable查询:TempTable.Party.value('(Property/SerialNumber/text())[1]', 'varchar(50)') as SerialNumber和似乎解决了重复。

感谢您的帮助。

0

很难说如果不知道您的确切数据库模式。假设这是T-SQL:查看CTE(公用表表达式)并分两步拆分语句。这使得这些陈述通常简单得多,而且通常效率更高。