问题
I use merge statement for inserting new data or deleting data. I use a temp table as source and a table as target.
the target table has a foreign key with another table(Table A).
I encounter following error sometimes when (when matched then delete statement) is executed.
error:
Attempting to set a non-NULL-able column's value to NULL.
If I comment this line the query is run perfectly.
also If i remove the foreign key between Material table and table A query is run perfectly too.
I have applied this Hotfix but I have mentioned problem yet.
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 #Temp
(
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 #Temp
) 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)
回答1:
Since you are experiencing this problem in a case where the target table has more than one foreign key (FK) constraint, it's most probably due to this bug: FIX: "Attempting to set a non-NULL-able column's value to NULL" error message when you use a MERGE statement in SQL Server 2008, in SQL Server 2008 R2 or in SQL Server 2012
You are running SQL Server 2008 R2 SP1 version 10.50.2550
This is fixed in SQL Server 2008 R2 SP1 version 10.50.2822 and later (Cumulative Update 8).
Download from here: Cumulative update package 8 for SQL Server 2008 R2 Service Pack 1
Or preferably, download the latest Cumulative Update available for SQL Server 2008 R2 SP1: Cumulative update package 13 for SQL Server 2008 R2 SP1
You mention that you have already applied one of the SQL Server hotfixes for this, but I suggest you double check that it is installed properly by executing SELECT @@VERSION and verifying that the version number is 10.50.2822 or later.
回答2:
Based on the following:
- Error message that you cannot set a NON-NULLABLE value to NULL
- Applied suggested hotfix did not address your issue
- Your
MERGEstatement works without thewhen matched then deleteor without the foreign key constraint
I suspect the issue is with a foreign key constraint pointing to your Materials table configured to ON DELETE SET NULL. You can verify if there are any foreign keys pointing to Materials with EXEC sp_fkeys 'Materials'
回答3:
I was able to "fix" this error by moving the select out of the using statement and put it in a temporary table.
That is, I change this:
MERGE dbo.IVS_LIVE t
USING (SELECT PolicyNumber
,EffectiveDate
,cast(null as date) as TerminationDate
,CAST('CRS' as CHAR(4)) as datasource
,VIN
,State
,IVS_RUNHISTORY_ID
FROM dbo.IVS_CRS_STAGING
WHERE PolicyStatus in (10, 60, 70)
OR ( PolicyStatus not in (10, 60, 70, 99)
AND PolicyStatusDate >= (select irh.RunDate
from dbo.IVS_RUNHISTORY irh
where irh.ID = @ivs_runhistory_id
)
)
) as s
to this:
MERGE dbo.IVS_LIVE t
USING #sourcetbl as s
I would guess that the simpler code did not trigger this bug in the very old version of sql server I am forced to use:
https://support.microsoft.com/en-ca/help/981037/fix-attempting-to-set-a-non-null-able-column-s-value-to-null-error-mes
来源:https://stackoverflow.com/questions/26955418/why-is-there-the-error-attempting-to-set-a-non-null-able-columns-value-to-null