您可以尝试以下方法:
create procedure spInsertData
(
@data xml
)
as
begin
set nocount on
if @data.exist('/Items') = 1
begin
insert into Items (Name)
select T.c.value('text()[1]', 'nvarchar(100)')
from @data.nodes('/Items/Item') T(c)
where not exists (select 1 from Items where Name = T.c.value('text()[1]', 'nvarchar(100)'))
return;
end
else if @data.exist('/Products') = 1
begin
insert into Products (Name)
select T.c.value('text()[1]', 'nvarchar(100)')
from @data.nodes('/Products/Product') T(c)
where not exists (select 1 from Products where Name = T.c.value('text()[1]', 'nvarchar(100)'))
return;
end
-- etc.
end