See my code where i am insert or updating a table from xml using merge statement.
drop table if exists Student
Declare @Data xml
set @Data=
'<Root>
<Student>
<Name>Rakesh</Name>
<Marks>80</Marks>
</Student>
<Student>
<Name>Mahesh</Name>
<Marks>90</Marks>
</Student>
<Student>
<Name>Gowtham</Name>
<Marks>60</Marks>
</Student>
<Student>
<Name>Manoj</Name>
<Marks></Marks>
</Student>
</Root>'
create table Student (
Name varchar(10),
Marks int
)
insert into Student values
('Rakesh',90),
('Mahesh',80),
('Jack',80),
('Manoj',57)
DECLARE @archive TABLE
(
ActionType varchar(10),
Name varchar(10),
Marks int
);
Merge into Student as Trg
Using (select d.x.value('Name[1]','varchar(20)') as Name ,
d.x.value('Marks[1]','int') as Marks from
@data.nodes('/Root/Student')as d(x)) as Src
on Trg.Name=Src.Name
When Matched Then update set
Trg.Marks=Src.Marks
when not matched then
insert (Name,Marks) values (Src.Name,Src.Marks)
OUTPUT
$action ,
inserted.*
INTO @archive;
I want when Name match then UPDATE will perform but when Name match but Marks is empty then that records will be deleted based on name matched.
so tell me how could i customize this.
see my xml there is one student named Manoj whose marks is empty then that records will be removed from table based on name matched.
How could i mention a condition in Merge that when Name matched and marks not empty then update and when Name matched but marks is empty then that records will be removed from the table.
please guide me how to achieve this. thanks