Avatar of alexisbr
alexisbr

asked on 

SQL Error when trigger fires in SQL Server 2008 R2

Hi.  I am trying to use triggers to automatically update LastUpdated (datetime) and LastUpdatedBy (varchar(20).  

I wrote this trigger for LastUpdated and it works fine.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trEmailGroupMembersLastUpdated] ON [dbo].[EmailGroupMembers]
FOR UPDATE 
AS
UPDATE EmailGroupMembers SET EmailGroupMembers.LastUpdated=getdate()
FROM EmailGroupMembers INNER JOIN Inserted ON EmailGroupMembers.EmailGroupMemberID= Inserted.EmailGroupMemberID

Open in new window


However, the trigger I wrote for LastUpdatedBy does not work.  
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trEmailGroupMembersLastUpdatedBy] ON [dbo].[EmailGroupMembers]
FOR UPDATE 
AS
UPDATE EmailGroupMembers SET EmailGroupMembers.LastUpdatedBy=(ltrim(rtrim(substring(suser_sname(),(5),len(suser_sname())))))
FROM EmailGroupMembers INNER JOIN Inserted ON EmailGroupMembers.EmailGroupMemberID= Inserted.EmailGroupMemberID

Open in new window


I get this error from the Access 2010 frontend form when it tries to save to SQL Server.  
User generated image
I have tried different ways to write the trigger but I cannot avoid this error.  Does anyone know what I am doing wrong?

Thanks,
Alexis
SQLMicrosoft SQL Server

Avatar of undefined
Last Comment
alexisbr
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of alexisbr
alexisbr

ASKER

Thank you both!  I used SET NOCOUNT ON and the trigger you wrote.  It never occurred to me to combine them. That is brillant!

It's working now.  I thought I had already tried adding SET NOCOUNT ON but I guess not.

Also, I was using the functions to limit the size of the username field since our domain is included if I just use suser_sname() but I just increased that field to cover longer user names.

Thank you again!

Regards,
Alexis
Avatar of alexisbr
alexisbr

ASKER

Thank you again!
Avatar of ste5an
ste5an
Flag of Germany image

When using SUSER_SNAME() redefine your LastUpdateByColumn. The data type of SUSER_SNAME() is SYSNAME (NVARCHAR(128)).
Avatar of alexisbr
alexisbr

ASKER

Thank you!
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo