Привет, ребята, я делаю 2 таблицы для реализации триггеров -
create table emp(id int primary key identity(1,1),
name char(40),
salary varchar(50),
gender char(40),
departmentid int);
insert into emp(name,salary,gender,departmentid) values ('jimmi',4800,'Male',4);
create table emp_audit
(
id int primary key identity(1,1),
audit varchar(60)
);
alter trigger trigger_em_update on emp for update
as begin
Declare @Id int
Declare @oldname char(40),@newname char(40)
Declare @oldsal int,@newsal int
Declare @oldgen char(40),@newgen char(40)
Declare @olddeptid int,@newdeptid int
Declare @auditstring nvarchar(max)
--select * from deleted;
select * into #temptable from inserted;
while(Exists(select id from #temptable)) --boolean condition if there are rows are not
Begin
set @auditstring =''
--if there are many rows we still select the first one
select Top 1 @Id =id,@newname=name,@newgen=gender,@newsal=salary,@newdeptid=departmentid
from #temptable;
select Top 1 @Id =id,@oldname=name,@oldgen=gender,@oldsal=salary,@olddeptid=departmentid
from deleted where @Id=id;
set @auditstring=' Employee with id= '+CAST(@Id as varchar(20))+ ' changed '
if(@oldname<>@newname)
set @auditstring=@auditstring + 'name from '+ @oldname +' to' +@newname
if(@oldsal<>@newsal)
set @auditstring=@auditstring + ' salary from '+ @oldsal +' to ' +@newsal
if(@oldgen<>@newgen)
set @auditstring=@auditstring + ' gender from ' + @oldgen + ' to ' + @newgen
-- if(@olddeptid<>@newdeptid)
--set @auditstring=@auditstring + ' departmentid from ' + cast(@olddeptid as nvarchar(5))+' to '
insert into emp_audit values(@auditstring)
delete from #temptable where id=@Id
end
end
when i use update query
update emp set name='vishi',gender='male',salary='4000',departmentid=3 where id=3;
выдает ошибку
"Сбой преобразования при преобразовании значения nvarchar" Сотрудник с id = 3 изменил имя с оклада james tovishi с "на тип данных int.
"
я не знаю, как решить это .. вы можете решить это ..