It appears truncation error after inserting value
I am having problems (SQL Server 2019) with the last sentence. After inserting product called MEMORIA USB it appears the next error:
Msg 2628, Level 16, State 1, Procedure TR_ProductoInsertado, Line 4 [Batch Start Line 28]
String or binary data would be truncated in table 'inventario.dbo.historial', column 'usuario'. Truncated value: 'DESKTOP-NLVD399\unna'.
The statement has been terminated.
If I delete the trigger and I insert the value then it works fine
create table productos
(
id_Cod int identity primary key,
cod_prod varchar(4) not null,
nombre varchar(50)not null,
existencia int not null,
)
GO
Create table historial
(fecha date,
descripcion varchar(100),
usuario varchar(20))
GO
create table ventas
(cod_prod varchar(4),
precio money,
cantidad int
)
GO
create trigger TR_ProductoInsertado
on productos for insert
as
insert into historial values(getdate(), 'registro insertado', system_user)
go
insert into productos values('A001','MEMORIA USB 32GB',175);
Can someone help me ?
thanks in advance
Solution 1:
Your usuario
has space for 20 characters, but your system_user
is longer than that.
Either increase the length of the column, or truncate the system_user
value before inserting it.
create trigger TR_ProductoInsertado
on productos
for insert
as
insert into historial (fecha, descripcion, usuario)
values(getdate(), 'registro insertado', LEFT(system_user, 20));