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
Projectsalso to be inserted same as did but onlyLowbincontent of first Project tag is inserted. Can you correct my code.