Advertisement
bluebunny72

Friendly error message for Unique Constraint

May 27th, 2015
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.78 KB | None | 0 0
  1. --UQ_dept_Uf_ILC_CostCodeType
  2. IF NOT EXISTS (SELECT TOP 1 1 FROM ObjectMainMessages WHERE ObjectName = 'UQ_dept_Uf_ILC_CostCodeType' AND MessageType = 17)
  3.     BEGIN
  4.         DECLARE @NextMessageNo  int
  5.  
  6.         SELECT @NextMessageNo   = ISNULL(MAX(messageno),799999)
  7.         FROM ApplicationMessages
  8.         WHERE MessageNo BETWEEN 800000 AND 999999
  9.  
  10.         SET @NextMessageNo = @NextMessageNo + 1
  11.  
  12.         --add main message 100093 = The &1 entered already exists.
  13.         EXEC AddObjectMainMessageSp 'UQ_dept_Uf_ILC_CostCodeType', 17, 100093, 16, 'Dept Cost Code Type UQ violation', 0
  14.  
  15.         --add the substitution text
  16.         EXEC AddApplicationMessageSp @NextMessageNo, 'Cost Code Type', N'1033'
  17.  
  18.         --add substitution text to main message
  19.         EXEC AddObjectBuildMessageSp 'UQ_dept_Uf_ILC_CostCodeType', 17, 1, @NextMessageNo
  20. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement