I use instructions mergeto insert new data or delete data. I use temp tablelike a sourcetable like target.
the goal table has foreign key with another table ( Table A).
I sometimes encounter an error when executing ( when matched then deletestatement).
error:
Attempting to set a non-NULL-able column value to NULL.
If I comment on this line, the request will execute perfectly.
Also, if I delete the request foreign keybetween requests Material tableand Table Aalso fine.
I applied this hotfix , but I already mentioned the problem.
SQL Server Version: Microsoft SQL Server 2008 R2 (SP1) - 10.50.2550.0 (X64)
**Target Tbale (Material)** **Table A**
PatientIdRef (ForeignKey) PatientId(Primary Key)
VisitNumberRef(Foreign Key) VisitNumber(Primary Key)
Unit_Price Name
Unit_Price_Ins family
....
....
create tabl
(
patinetId [varchar](50) NOT NULL,
[Visit_Number] [smallint] NULL,
[Unit_Price] [float] NULL,
[Unit_Price_Ins] [float] NULL,
......
.....
)
merge materials as target
using(select patinetId ,Visit_Number,Unit_Price,Unit_Price_Ins
from
) as source
on (source.patientid=target.patientid collate arabic_ci_as and source.visit_number=target.visit_number)
when matched then delete
when not matched then insert
(patinetIdref ,Visit_Numberref,Unit_Price,Unit_Price_Ins )
values(patinetId ,Visit_Number,Unit_Price,Unit_Price_Ins)