如何修复与Unique Non-Clustered index相关的错误

时间:2014-08-27 11:48:08

标签: sql-server tsql

我的表名为SalFactorHds,它在列FactorNumFHS上有唯一索引。在表中插入值时,我看到了这个错误:

  

无法在对象' dbo.salfactorHds'中插入重复的键行。具有唯一索引UQ_FactorNumFHS

@FactorNumFHS是存储过程正在发送的参数。

以前的代码是:

INSERT INTO SaLFactorHds (FactorNumFHS , column1 , columnb)
VALUES(@FactorNumFHS, 1, 2)

我把它变成了这个:

INSERT INTO SaLFactorHds (FactorNumFHS, column1 , columnb)
VALUES( (CASE
           WHEN NOT EXISTS (SELECT 1
                            FROM SALFactorHds
                            WHERE FactorNumFHS = @FactorNumFHS) 
             THEN @FactorNumFHS
             ELSE ISNULL((SELECT MAX(FactorNumFHS)
                          FROM SALFactorHds  
                          WHERE (CompanyNo = @CompanyNoFHS) 
                            AND (FactorNumFHS <= (SELECT MaxNormalFactorSFT 
                                                  FROM SALFactorTypes 
                                                  WHERE CompanyNo = @CompanyNoFHS 
                                                    AND (@FactorTypeFHS = SerialNoSFT)))
                            AND (FiscalYear = @YearFHS)) ,0) + 1
          END), 1, 2)

但我仍然看到错误!!

0 个答案:

没有答案