我的表名为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)
但我仍然看到错误!!