-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPayRoll_query.sql
138 lines (94 loc) · 2.94 KB
/
PayRoll_query.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use dbfapproach
create table payroll(id int identity(1,1) primary key,empname varchar(20),dob date,accno int,leave int ,overTime int, detuction int,salary int)
--drop table payroll
--pubcreate table salary(id int identity(1,1) primary key,)
select * from payroll
-- update payroll
alter Procedure update_data
@id int,
@name varchar(20),
@dop date,
@accno int,
@leave int,
@overtime int
as
begin
declare @detuction int ;
declare @salary int ;
set @detuction= @leave*1000;
set @salary=(30000-@leave*1000) + @overtime*100
update payroll set empname=@name,dob = @dop ,accno =@accno,leave = @leave,overtime = @overtime,detuction =@detuction,salary = @salary where id=@id
end
--drop procedure insert_data
--Insert procedure
alter procedure insert_data
@name varchar(20),
@dop date,
@accno int,
@leave int,
@overtime int
as
begin
declare @detuction int;
declare @salary int;
set @detuction= @leave*1000;
set @salary=(30000-@leave*1000) + @overtime*100
INSERT INTO payroll (empname, dob, accno, leave, overtime, detuction, salary)a
VALUES (@name, @dop, @accno, @leave, @overtime, @detuction, @salary);
end
--#############################################################################################################################################
select * from leave
create table leave(l_id int primary key identity(1,1),From_date date,To_date date , reason varchar(20),leaves int , Detuction int , status int default 0 , e_id int foreign key references payroll(id) ON DELETE CASCADE );
alter table leave add status int not null default 0
ALTER TABLE leave
ADD COLUMN status INT;
alter table leave drop column status
select p.empname as name,l.reason from leave l join payroll p on l.e_id = p.id
select * from leave
alter procedure insert_leave
@f_d date,
@t_d date,
@reason varchar(20),
@e_id int
as
begin
declare @detuction int,@leave int , @remaining int
SELECT @leave = DATEDIFF(day, From_date, To_date) FROM leave;
set @detuction=@leave*1000;
INSERT INTO leave (From_date, To_date, reason, leaves, Detuction, e_id)
VALUES (@f_d, @t_d, @reason, @leave, @detuction, @e_id);
end
select * from payroll
select * from leave
alter procedure update_status
@id int,
@status int
as
begin
declare @p_id int , @noleave int , @no int;
SELECT @noleave = leaves FROM [leave] WHERE l_id = @id;
SELECT @p_id = e_id FROM [leave] WHERE l_id = @id;
update leave set status = @status where l_id = @id
if @status = 1
begin
declare @detuction int;
update payroll set leave=leave+@noleave , detuction=(leave+@noleave)*1000 ,salary = 30000-detuction*1000 where id=@p_id
end
end
EXEC update_status @id = 6, @status = 1;
-- join
select p.empName , l.reason from leave l join payroll p on l.e_id=p.id;
ALTER TABLE leave
ADD empname varchar(50);
CREATE TRIGGER trg_insert_leave_empname
ON leave
AFTER INSERT
AS
BEGIN
UPDATE leave
SET empname = p.empname
FROM inserted i
INNER JOIN payroll p ON i.e_id = p.id
WHERE leave.l_id = i.l_id;
END;
truncate table leave