1

For a table schema like below

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 

When inserting data into table, i want to insert only child tags of <SkillSet> but seems <SkillSet> along with it's child elements inserts into Skill xml columns.

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

Questions

  • How to insert only child elements of <Skillset> rather than whole tag and its children.

  • I expected Projects also to be inserted same as did but only Lowbin content of first Project tag is inserted. Can you correct my code.

1 Answer 1

1

Since you are on SQL Server 2005 you can make use of the XML data type and do like this instead.

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)

Result:

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

Update:

If you really want to use OPENXML for some reason you can use this.

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
                    )
Sign up to request clarification or add additional context in comments.

3 Comments

do i get to lose anything by declaring @doc to be xml datatype, also could u provide a reference to T(N) what does it mean how does it work
@Deeptechtoons The output is the same but there might be a difference in performance. Might be faster or slower but it is much easier to do what you want. Nodes() shreds the XML into rows and T is the table alias that holds the shredded XML and N is the column name. You can just name them whatever you like.
Sorry to disturb you but i think you are right man who can answer this well stackoverflow.com/questions/8740826/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.