3

在如下表模式中

CREATE TABLE [dbo].[Employee](
    [EmployeeId] [uniqueidentifier] NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [Location] [nvarchar](50) NOT NULL,
    [Skills] [xml] NOT NULL
 CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED 
  • 假设列中保存的 xml 如下所示,我将如何让员工具有C#(不区分大小写)编程技能。Skills

  • 您能否就其他功能提出建议,以帮助我在使用 xml 数据类型列时进行过滤、排序

<Skills><Skill>C#</Skill><Skill>ASP.NET</Skill><Skill>VB.NET</Skill></Skills>

4

1 回答 1

3

比较区分大小写,因此您需要与 c# 和 C# 进行比较。在 SQL Server 2008 中,您可以使用大写的.

declare @T table
(
  ID int identity,
  Skills XML
)

insert into @T values
('<Skills><Skill>C#</Skill><Skill>ASP.NET</Skill><Skill>VB.NET</Skill></Skills>')
insert into @T values
('<Skills><Skill>CB.NET</Skill><Skill>ASP.NET</Skill><Skill>c#</Skill></Skills>')
insert into @T values
('<Skills><Skill>F#</Skill><Skill>ASP.NET</Skill><Skill>VB.NET</Skill></Skills>')

select ID
from @T
where Skills.exist('/Skills/Skill[contains(., "C#") or contains(., "c#")]') = 1

结果:

ID
-----------
1
2

更新:

这也将起作用。

select T.ID
from @T as T
  cross apply T.Skills.nodes('/Skills/Skill') as X(N)
where X.N.value('.', 'nvarchar(50)') like '%C#%'
于 2012-01-05T10:36:36.527 回答