I have a table on our database which is used to log user login attempts.
The table uses a trigger on the insert to write the current Date/Time to a field called CreatedOn
The script for creating the trigger:
CREATE TRIGGER [dbo].[utr_SecurityLog_Insert] ON [dbo].[SecurityLog] FOR
INSERT AS Begin Update SecurityLog SET [CreatedOn] = getdate ()
FROM inserted i, SecurityLog a WHERE i.SecurityLogId = a.SecurityLogId End;
The table looks like this:
CREATE TABLE [dbo].[SecurityLog](
[SecurityLogId] [int] IDENTITY(0,1) NOT NULL,
[SecurityOfficeId] [int] NULL,
[SecurityWebId] [int] NULL,
[IPAddress] [varchar](50) NULL,
[UserName] [varchar](50) NULL,
[UserPass] [varchar](50) NULL,
[CreatedOn] [datetime] NULL,
CONSTRAINT [PK_SecurityLog] PRIMARY KEY CLUSTERED
(
[SecurityLogId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
It's nothing special. I use this trigger code all across the database. Virtually every table has identical CreatedOn
and ModifiedOn
fields driven by similar trigger scripts.
Now when I attempt an INSERT
into the SecurityLog
table I am getting the following error:
Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 523 [code] => 523 [2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]
A trigger returned a resultset and/or was running with SET NOCOUNT OFF while another outstanding result set was active.
[message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]A trigger returned a resultset and/or was running with SET NOCOUNT OFF while another outstanding result set was active. ) )
This database has been in use for twelve years without so much as a hiccup. I understand that somehow the trigger is producing a response which is being classed as output but I have no idea why this is happening in this isolated instance, how to fix it or why this should start happening to one table. Is it possible it is a driver issue?
[EDIT]
I have been able to 'fix' this issue by adding SET NOCOUNT ON
and SET NOCOUNT OFF
to the SQL like this:
SET NOCOUNT ON;INSERT INTO SecurityLog (IPAddress,SecurityWebId,UserName,UserPass) VALUES (?,?,?,?);SET NO COUNT OFF
Whilst it 'fixes' the issue it still does not explain why this one table should behave like this. It is perplexing me as I want to address the issue rather than paper over the cracks.