违反了 UNIQUE KEY 约束 'UNQ_CusID_DetachPolicyID'不能在对象 'dbo.CustomerAccount' 中插入重复键 语句已终止 警告: 聚合或其

违反了 UNIQUE KEY 约束 'UNQ_CusID_DetachPolicyID'。不能在对象'dbo.CustomerAccount' 中插入重复键。 语句已终止。 警告: 聚合或其他 SET 操作消除了 Null 值。


我使用存储过程时,在往数据库添加值时,没有数据为null的校验(可能有=null的校验,但是null不能用=)。并且数据库中已存在原先字段为null的值,再插入数据为null的值,就会出现错误


原来的sql语句:

if not exists(select * from CustomerAccount where PolicyNo=@PolicyNo and DetachPolicyID=@DetachPolicyID and CustomerID=@cusId)


因为@cusId为空,校验的时候,=号是不能校验出null值的。


修改后的sql语句:

if not exists(select * from CustomerAccount where PolicyNo=@PolicyNo and DetachPolicyID=@DetachPolicyID and ((CustomerID=@cusId and ISNULL(@cusId,'')!='') or isnull(@cusId,'')=''))

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...