在下面的表模式中
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
C#
(不区分大小写)编程技能的员工,假设Skills
列中保存的xml如下。 <Skills><Skill>C#</Skill><Skill>ASP.NET</Skill><Skill>VB.NET</Skill></Skills>
最佳答案
比较是区分大小写的,因此您需要同时比较C#和C#。在SQL Server 2008中,可以使用upper-case。
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#%'
关于sql-server - TSQL如何在xml列中选择具有技能的员工,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8740826/