Hi @David C ,
May I ask how you tested to put a severity 19+ event in the event log?
You need to use sp_addmessage, and [ @with_log = ] { 'TRUE' | 'FALSE' } Is whether the message is to be written to the Windows application log when it occurs.
Test code on my side as next:
use testpowerapps
go
create TABLE TStudent (
StudentID varchar(10) NOT NULL,
Sname varchar(10) DEFAULT NULL,
sex varchar(4) DEFAULT NULL,
cardID varchar(20) DEFAULT NULL,
Birthday datetime DEFAULT NULL,
Email varchar(40) DEFAULT NULL,
Class varchar(20) DEFAULT NULL,
enterTime datetime DEFAULT NULL
)
go
create trigger insert_TStudent on TStudent for insert
as
declare @sum int
select @sum=count(StudentID) from TStudent where StudentID in (select StudentID from inserted)
if @sum>1
begin
raiserror(999999,21,1) with log
rollback tran
end
insert TStudent (StudentID,Sname,sex) values ('1','A','W')
--check 1
SELECT * FROM sysmessages
--sp_addmessage
USE master;
GO
EXEC sp_addmessage @msgnum=999999, @severity=21,@msgtext=N'The StudentID is already exists', @with_log='true'
GO
--check 2
SELECT * FROM sysmessages
--test, insert one duplicated id
use testpowerapps
go
insert TStudent (StudentID,Sname,sex) values ('1','A','W')
--SSMS error message
--sqlserver error log
--Windows Applicatin Error
BR,
Mia
If the answer is helpful, please click "Accept Answer" and upvote it.