2011-11-19 21 views
15
alter FUNCTION [Kuri].[fnGetAge](@kuri_cust_Id int,@amt decimal) 
RETURNS SMALLINT 
AS 
    BEGIN 
    DECLARE @isVallid bit = 0 
    declare @payed decimal(14,2) 
    declare @totaltillnow decimal(14,2) 
    select @payed = isnull(SUM(Payment.amt),0) from Kuri.Payment where Payment.Kuri_Cust_ID = @kuri_Cust_id 
    select @totaltillnow = isnull(SUM(NextLotAmount),0) from Kuri.Kuri_GivenDetails 
    inner join Kuri.kuri_Customer 
    on Kuri_GivenDetails.kuri_Id = kuri_Customer.kuri_ID 
    where kuri_Customer.kuri_Cust_id = @kuri_Cust_id 
    if((@payed + @amt) < @totaltillnow) 
     set @isVallid = 1 
     RETURN @isVallid 
    END; 
GO 

ALTER TABLE [Kuri].[Payment] WITH CHECK ADD CONSTRAINT PaymentCheck CHECK (kuri.fnGetAge(kuri_Cust_ID,amt) >= 1) 
GO 

error:La sentencia ALTER TABLE en conflicto

La sentencia ALTER TABLE en conflicto con la restricción CHECK "PaymentCheck". El conflicto ocurrió en la base de datos "MERP", tabla "Kuri.Payment".

estructura de la tabla es así

CREATE TABLE [Kuri].[Payment](
    [payment_ID] [int] IDENTITY(1,1) NOT NULL, 
    [payment_Date] [date] NOT NULL, 
    [bill_No] [nvarchar](25) NOT NULL, 
    [Kuri_Cust_ID] [int] NOT NULL, 
    [vr_ID] [int] NOT NULL, 
    [amt] [decimal](14, 2) NULL, 
    [created_ID] [int] NULL, 
    [created_Date] [datetime] NULL, 
    [modified_ID] [int] NULL, 
    [modified_Date] [datetime] NULL, 
    [authorized_ID] [int] NULL, 
    [authorized_Date] [datetime] NULL, 
    CONSTRAINT [PK_Payment] PRIMARY KEY CLUSTERED 
    ([payment_ID] ASC) 

ALTER TABLE [Kuri].[Payment] WITH CHECK ADD CONSTRAINT [FK_Payment_kuri_Customer] FOREIGN KEY([Kuri_Cust_ID]) 
REFERENCES [Kuri].[kuri_Customer] ([Kuri_Cust_ID]) 

ALTER TABLE [Kuri].[Payment] CHECK CONSTRAINT [FK_Payment_kuri_Customer] 
+0

Creo que 'kuri_Cust_ID' no es un INT en la tabla, o' amt' no es un decimal (o ambos). – GolezTrol

+0

@marc_s ¡muchas gracias funcionó! –

Respuesta

22

Como el error indica claramente: hay filas de la tabla que violan su restricción de comprobación.

Desde sus pruebas de restricción de comprobación para kuri.fnGetAge(kuri_Cust_ID,amt) >= 1, puede encontrar esas filas en violación de esta restricción de comprobación utilizando

SELECT * FROM Kuri.Payment 
    WHERE kuri.fnGetAge(kuri_Cust_ID, amt) < 1 

fijar o borrar esas filas, y entonces debería estar bien y su comando ALTER TABLE debe trabajar

+10

Diría que la forma en que marc_s lo describe es claro. El mensaje de error proporcionado por la aplicación no está claro. No indica que los datos en la tabla existente violen el intento de adición de restricción. Si tuviera dudas, yo o el póster original habría necesitado buscar una respuesta. Pero gracias Marc_s por la aclaración, sin embargo. – teaman

Cuestiones relacionadas