微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sql – ALTER TABLE语句冲突

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

错误

The ALTER TABLE statement conflicted with the CHECK constraint
“PaymentCheck”. The conflict occurred in database “MERP”,table
“Kuri.Payment”.

表结构是这样的

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]

解决方法

由于错误明确指出:您的表中有些行违反了您的检查约束.

由于您的检查约束测试kuri.fnGetAge(kuri_Cust_ID,amt)> = 1,
您可以使用查找违反此检查约束的行

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

修复或删除这些行,然后你应该没问题,你的ALTER TABLE命令应该工作

原文地址:https://www.jb51.cc/mssql/83411.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐