Meine Anfrage:Checkeinschränkung funktioniert nicht auf Masseneinsatz für mehr als 250 Datensätze
INSERT into PriceListRows (PriceListChapterId,[No])
SELECT TOP 250 100943 ,N'2'
FROM #AnyTable
Diese Abfrage funktioniert gut und die folgende Ausnahme auslöst, wie gewünscht:
Die INSERT-Anweisung in Konflikt mit der CHECK constraint "CK_PriceListRows_RowNo_Is_Not_Unqiue_In_PriceList". Der Konflikt trat in der Datenbank "TadkarWeb", Tabelle "dbo.PriceListRows" auf.
aber mit wechselnden SELECT TOP 250
-SELECT TOP 251
(ja! Nur 250-251 ändern!) Die Abfrage erfolgreich ohne Kontrolle constrain Ausnahme läuft!
Warum dieses seltsame Verhalten?
HINWEISE:
ist meine Check-Bedingung eine Funktion, die eine Art von Eindeutigkeit überprüft. Es fragt nach 4 Tabellen.
ich sowohl auf SQL Server überprüft 2012 SP2 und SQL Server 2014 SP1
** EDIT 1 **
Checkeinschränkung Funktion:
ALTER FUNCTION [dbo].[CheckPriceListRows_UniqueNo] (
@rowNo nvarchar(50),
@rowId int,
@priceListChapterId int,
@projectId int)
RETURNS bit
AS
BEGIN
IF EXISTS (SELECT 1
FROM RowInfsView
WHERE PriceListId = (SELECT PriceListId
FROM ChapterInfoView
WHERE Id = @priceListChapterId)
AND (@rowID IS NULL OR Id <> @rowId)
AND No = @rowNo
AND (@projectId IS NULL OR
(ProjectId IS NULL OR ProjectId = @projectId)))
RETURN 0 -- Error
--It is ok!
RETURN 1
END
** BEARBEITEN 2 ** Überprüfen Sie den Einschränkungscode (welcher SQL Server 2012 prod uces):
ALTER TABLE [dbo].[PriceListRows] WITH NOCHECK ADD CONSTRAINT [CK_PriceListRows_RowNo_Is_Not_Unqiue_In_PriceList] CHECK (([dbo].[tfn_CheckPriceListRows_UniqueNo]([No],[Id],[PriceListChapterId],[ProjectId])=(1)))
GO
ALTER TABLE [dbo].[PriceListRows] CHECK CONSTRAINT [CK_PriceListRows_RowNo_Is_Not_Unqiue_In_PriceList]
GO
** EDIT 3 **
Ausführungspläne sind hier: https://www.dropbox.com/s/as2r92xr14cfq5i/execution%20plans.zip?dl=0
** EDIT 4 ** RowInfsView
Definition lautet:
SELECT dbo.PriceListRows.Id, dbo.PriceListRows.No, dbo.PriceListRows.Title, dbo.PriceListRows.UnitCode, dbo.PriceListRows.UnitPrice, dbo.PriceListRows.RowStateCode, dbo.PriceListRows.PriceListChapterId,
dbo.PriceListChapters.Title AS PriceListChapterTitle, dbo.PriceListChapters.No AS PriceListChapterNo, dbo.PriceListChapters.PriceListCategoryId, dbo.PriceListCategories.No AS PriceListCategoryNo,
dbo.PriceListCategories.Title AS PriceListCategoryTitle, dbo.PriceListCategories.PriceListClassId, dbo.PriceListClasses.No AS PriceListClassNo, dbo.PriceListClasses.Title AS PriceListClassTitle,
dbo.PriceListClasses.PriceListId, dbo.PriceLists.Title AS PriceListTitle, dbo.PriceLists.Year, dbo.PriceListRows.ProjectId, dbo.PriceListRows.IsTemplate
FROM dbo.PriceListRows INNER JOIN
dbo.PriceListChapters ON dbo.PriceListRows.PriceListChapterId = dbo.PriceListChapters.Id INNER JOIN
dbo.PriceListCategories ON dbo.PriceListChapters.PriceListCategoryId = dbo.PriceListCategories.Id INNER JOIN
dbo.PriceListClasses ON dbo.PriceListCategories.PriceListClassId = dbo.PriceListClasses.Id INNER JOIN
dbo.PriceLists ON dbo.PriceListClasses.PriceListId = dbo.PriceLists.Id
Sie sind die gleichen zwei Werte Einfügen (100.943 und 2) 250-mal? Klingt wie du solltest die Ausnahme schon in der 2. Reihe –
@jamez genau bekommen! es ist für den Test. Ich sollte Fehler bekommen, aber ich weiß nicht, warum ich es nicht verstehe. (In der Tat bekomme ich es für 250 Datensätze einfügen, aber nicht für 251 Einsen!) –
ja seltsam. Ich habe den Beitrag bearbeitet. –