1

对于像下面这样的表模式

CREATE TABLE [dbo].[Employee](
    [EmployeeId] [uniqueidentifier] NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [Location] [nvarchar](50) NOT NULL,
    [Skills] [xml] NOT NULL,
    [Projects] [nvarchar](400) NULL,
 CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED 

将数据插入表时,我只想插入子标签,<SkillSet>但似乎<SkillSet>与它的子元素一起插入到 Skill xml 列中。

declare @doc NVARCHAR(MAX)

declare @idoc INT

select @doc = '<Request Type="InsertEmployee" CRUD="C">
    <Employee>
      <EmployeeId>1</EmployeeId>
      <Name>Deeptechtons</Name>
      <Location>USA</Location>
        <SkillSet>
        <Skill>C#</Skill>
        <Skill>SQL Server 2005</Skill>
        <Skill>ASP.net</Skill>
        </SkillSet>
      <Projects>
        <Project>LowBin</Project>
        <Project>Maskin</Project>
      </Projects>
    </Employee>
  </Request>'

exec sp_xml_preparedocument @idoc output,@doc

insert into Employee (
                        EmployeeId
                        ,[Name]
                        ,Location
                        ,Skills,
                        Projects
                     )
            SELECT      NEWID()
                        ,[Name]
                        ,Location
                        ,Skills
                        ,Projects
            FROM OPENXML(@idoc,'Request/Employee',2)
            WITH
                    (
                        [Name] NVARCHAR(50)
                        ,Location   NVARCHAR(50)
                        ,Skills XML 'SkillSet'
                        ,Projects NVARCHAR(400)
                    )

exec sp_xml_removedocument @idoc

问题

  • 如何仅插入子元素<Skillset>而不是整个标签及其子元素。

  • Projects也希望插入与之前相同的内容,但只Lowbin插入第一个 Project 标记的内容。你能更正我的代码吗?

4

1 回答 1

1

由于您使用的是 SQL Server 2005,因此您可以使用 XML 数据类型并改为这样做。

declare @doc xml

select @doc = '<Request Type="InsertEmployee" CRUD="C">
    <Employee>
      <EmployeeId>1</EmployeeId>
      <Name>Deeptechtons</Name>
      <Location>USA</Location>
        <SkillSet>
        <Skill>C#</Skill>
        <Skill>SQL Server 2005</Skill>
        <Skill>ASP.net</Skill>
        </SkillSet>
      <Projects>
        <Project>LowBin</Project>
        <Project>Maskin</Project>
      </Projects>
    </Employee>
  </Request>'

insert into Employee (
                        EmployeeId
                        ,[Name]
                        ,Location
                        ,Skills,
                        Projects
                     )
select newid(),
       T.N.value('Name[1]',  'nvarchar(50)'),
       T.N.value('Location[1]',  'nvarchar(50)'),
       T.N.query('SkillSet/*'),
       cast(T.N.query('Projects/*') as nvarchar(400))
from @doc.nodes('/Request/Employee') as T(N)

结果:

BC76E37C-0C0D-4B7B-92FD-0F7807F9204B 
Deeptechtons 
USA 
<Skill>C#</Skill><Skill>SQL Server005</Skill>Skill>ASP.net</Skill>
<Project>LowBin</Project><Project>Maskin</Project>

更新:

如果出于某种原因你真的想使用 OPENXML,你可以使用它。

insert into Employee (
                        EmployeeId
                        ,[Name]
                        ,Location
                        ,Skills,
                        Projects
                     )
            SELECT      NEWID()
                        ,[Name]
                        ,Location
                        ,Skills.query('SkillSet/*')
                        ,cast(Projects.query('Projects/*') as nvarchar(max))
            FROM OPENXML(@idoc,'Request/Employee',2)
            WITH
                    (
                        [Name] NVARCHAR(50)
                        ,Location   NVARCHAR(50)
                        ,Skills XML 'SkillSet'
                        ,Projects XML
                    )
于 2012-01-04T16:10:41.450 回答