Maybe there is a better way to do this. The jist of I want is to have SQL Server raise me 2 types of errors: a WARNING and an ERROR from a trigger when I update a table. If SQL server returns a WARNING the trigger should COMMIT but show the warning to the user (using .NET - preferably through a SQL Exception which is only raised if severity > 10) and if it is an ERROR the trigger should ROLLBACK and show the ERROR to the user (through a SQL Exception). My attempts (needless to say this isn't working) at this was to have a trigger like this:
ALTER TRIGGER [dbo].[TR_TRANSACTION_UPDATE]
ON [dbo].[tTRANSACTION]
FOR UPDATE
AS
BEGIN
...
BEGIN TRY
DECLARE @id INT ,@maxid INT
SELECT @id = 0 ,@maxid = MAX(transID) FROM INSERTED
WHILE @id < @maxid
BEGIN
SELECT @id = MIN([TransID]) FROM INSERTED WHERE [TransID] > @id
EXEC dbo.sp_CheckTransaction @TransID = @id
END
COMMIT TRANSACTION
END TRY
BEGIN CATCH
DECLARE @err_msg VARCHAR(MAX), @err_sev AS INT, @err_state AS INT
SELECT @err_msg = ERROR_MESSAGE(), @err_sev = ERROR_SEVERITY(), @err_state = ERROR_STATE()
IF @err_state <> 120 -- '120 is not a fatal error from STORED_PROC
ROLLBACK TRANSACTION
ELSE
COMMIT TRANSACTION
RAISERROR(@err_msg, @err_sev, @err_state)
END CATCH
END
This trigger doesn't work because it thinks the transaction is uncommittable.
Also, sp_CheckTransaction can RAISE 2 types of errors:
RAISERROR(@msg, 15, 120) -- warning
or RAISERROR(@msg, 15, 121) -- error
The reason I can't use a severity < 15 is because I want a warning to show up in .NET (I can decide based on state & severity if it is a warning or an error). .NET SqlException is raised only for severity > 10.