Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Name: Ajay Patwari CSU ID: 2803705: - Created A New Table Employee For Trigger Test

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Name: Ajay Patwari

CSU ID: 2803705

1)

-- created a new table employee for trigger test


use Company;
create table EMPLOYEE_trig(
Fname varchar(20),
MINIT char(1),
LNAME varchar(20),
ssn char(9),
bdate date,
employeeaddress varchar(30),
sex char(1),
employeesalary decimal,
superssn char(9),
DNO tinyint,
);

Insert into EMPLOYEE_trig values('John','B','Smith','123456789','09-Jan-55','731


Fondren,Houston,TX','M','30000','987654321','5');
Insert into EMPLOYEE_trig values('Franklin','T','Wong','333445555','08-Dec-45','638
Voss,Houston,TX','M','40000','888665555','5');
Insert into EMPLOYEE_trig values('Joyce','A','English','453453453','31-Jul-62','5631 Rice,
Houston,TX','F','25000','333445555','5');
Insert into EMPLOYEE_trig values('Ramesh','K','Narayan','666884444','15-Sep-52','975 Fire
Oak,Humble,TX','M','38000','333445555','5');
Insert into EMPLOYEE_trig values('James','E','Borg','888665555','10-Nov-27','450
Stone,Houston, TX','M','55000',NULL,'1');
Insert into EMPLOYEE_trig values('Jennifer','S','Wallace','987654321','20-Jun-31','291
Berry,Bellarie,TX','F','43000','888665555','4');
Insert into EMPLOYEE_trig values('Ahmad','V','Jabbar','987987987','29-Mar-59','980 Dallas,
Houston,TX','M','25000','987654321','4');
Insert into EMPLOYEE_trig values('Alicia','J','Zelaya','999887777','19-Jul-58','3321 Castle,
SPring,TX','F','25000','987654321','4');

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

--select @old_salary= d.employeesalary from deleted d;


--select @new_salary= i.employeesalary from inserted i;
Declare cursorid cursor for select d.ssn,d.employeesalary,d.DNO from deleted d
Declare cursorid1 cursor for select i.ssn,i.employeesalary,i.DNO from inserted i
open cursorid
fetch next from cursorid into @old_SSN,@old_salary,@old_Dno
open cursorid1
fetch next from cursorid1 into @new_SSN,@new_salary,@new_Dno

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;

select * from Audit_EmpSalary;


Update EMPLOYEE_trig set employeesalary= '99000' where DNO=5;
-- After Update
select * from EMPLOYEE_trig;

select * from Audit_EmpSalary;

Create Trigger Trig_delete_Audit_empsalary


on EMPLOYEE_trig For delete
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;
Declare cursorid cursor for select d.ssn,d.employeesalary,d.DNO from deleted d
open cursorid
fetch next from cursorid into @old_SSN,@old_salary,@old_Dno
while @@fetch_status = 0
begin
set @DML_type ='delete';

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

select * from EMPLOYEE_trig;

select * from Audit_EmpSalary;


Create Trigger Trig_Insert_Audit_empsalary
on EMPLOYEE_trig For Insert
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;

Declare cursorid1 cursor for select i.ssn,i.employeesalary,i.DNO from inserted i


open cursorid1
fetch next from cursorid1 into @new_SSN,@new_salary,@new_Dno
while @@fetch_status = 0
begin
set @DML_type ='Insert';

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;

select * from Audit_EmpSalary;


Insert into EMPLOYEE_trig values(Ajay,'P','Patwari','233232189','20-Sep-96','1700E
13th st','M','80000','987654321','5');
-- after insert
select * from EMPLOYEE_trig;

select * from Audit_EmpSalary;

You might also like