Name: Ajay Patwari CSU ID: 2803705: - Created A New Table Employee For Trigger Test
Name: Ajay Patwari CSU ID: 2803705: - Created A New Table Employee For Trigger Test
Name: Ajay Patwari CSU ID: 2803705: - Created A New Table Employee For Trigger Test
1)
Page 1 of 16
Page 2 of 16
-- creating a Audit_Empsalary table
create table Audit_EmpSalary(
date_of_change date,
DML_Type varchar(8),
old_SSN char(9),
new_SSN char(9),
old_salary decimal,
new_salary decimal,
old_Dno tinyint,
new_Dno tinyint
);
-- creation of stored procedure
create procedure SP_Audit_Empsalary
-- @date_of_change date,
@DML_Type varchar(8),
@old_SSN char(9),
@new_SSN char(9),
@old_salary decimal,
@new_salary decimal,
@old_Dno tinyint,
@new_Dno tinyint
AS
Begin
Insert into
Audit_EmpSalary(date_of_change,DML_Type,old_SSN,new_SSN,old_salary,new_salary,old_D
no,new_Dno)
select
getdate(),@DML_Type,@old_SSN,@new_SSN,@old_salary,@new_salary,@old_Dno,@new_
Dno
end
Create Trigger Trig_Update_Audit_empsalary
on EMPLOYEE_trig For Update
AS
begin
Declare @DML_Type varchar(8);
Declare @old_SSN char(9);
Declare @new_SSN char(9);
Declare @old_salary decimal;
Declare @new_salary decimal;
Declare @old_Dno tinyint;
Declare @new_Dno tinyint;
if update(employeesalary)
begin
while @@fetch_status = 0
begin
set @DML_type ='Update';
EXEC SP_Audit_Empsalary
@DML_type,@old_SSN,@new_SSN,@old_salary,@new_salary,@old_Dno,@new_Dno
fetch next from cursorid into @old_SSN,@old_salary,@old_Dno
fetch next from cursorid1 into @new_SSN,@new_salary,@new_Dno
end
close cursorid1
close cursorid
end
end
-- before update
select * from EMPLOYEE_trig;
EXEC SP_Audit_Empsalary
@DML_type,@old_SSN,@new_SSN,@old_salary,@new_salary,@old_Dno,@new_Dno
fetch next from cursorid into @old_SSN,@old_salary,@old_Dno
end
close cursorid
end
-- before delete
select * from EMPLOYEE_trig;
select * from Audit_EmpSalary;
Delete from EMPLOYEE_trig where Fname='John' AND LNAME='Smith'
-- after delete
EXEC SP_Audit_Empsalary
@DML_type,@old_SSN,@new_SSN,@old_salary,@new_salary,@old_Dno,@new_Dno
fetch next from cursorid1 into @new_SSN,@new_salary,@new_Dno
end
close cursorid1
end
-- before insert
select * from EMPLOYEE_trig;