'Client error when setting a TSQL trigger: Record not found or changed by another user

I'm trying to set a trigger that generates a txt file after an insert statment. So far, so good, and the code works fine when I run it in SQL Studio, But when I insert a row from the Client UI (a software called A3ERP) the following error appears:

Record not found or changed by another user

Then the file is generated but the table doesn't insert the new row.

Here is the code for the trigger:

USE [BUSINESS]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[TGR_COMUNIDAD_TEST]
   ON  [dbo].[COMUNIDAD] 
   AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON
    EXEC xp_cmdshell 'echo tesline > D:\Datos\Comun\AriesAuto\comunidad_testfile.txt'
END

Any suggestions? I'm stuck in this error.

Thank you.



Solution 1:[1]

I solved the problem.

As @Charlieface point out, the error was caused by a string of text that xp_cmdshell was returning to the client. Adding the argument [ , no_output ] solved it:

USE [BUSINESS]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[TGR_COMUNIDAD_TEST]
   ON  [dbo].[COMUNIDAD] 
   AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON
    EXEC xp_cmdshell 'echo tesline > D:\Datos\Comun\AriesAuto\comunidad_testfile.txt',no_output
END

Also, as everyone seems to agree: this logic should be in the application and not on the SQL Server, but as I didn't write the application and I have no means to implement things there, this solution works for me. Thanks everyone!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1