-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLAdvanced 7.sql
More file actions
76 lines (76 loc) · 2.7 KB
/
SQLAdvanced 7.sql
File metadata and controls
76 lines (76 loc) · 2.7 KB
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
select * from tblEmployee
select * from tblEmploye
select * from tblDepartment
select * From Employee2
---lets create another table with Foreign key
Create table Employee2
(
ID int primary key,
Name nvarchar(50),
Gender nvarchar(50),
Salary int,
DepartmentId int foreign key references tblDepartment(ID)
)
Go
Insert into Employee2 values (1, 'Tom', 'Male', 4000, 1)
Insert into Employee2 values (2, 'Pam', 'Female', 3000, 3)
Insert into Employee2 values (3, 'John', 'Male', 3500, 1)
Insert into Employee2 values (4, 'Sam', 'Male', 4500, 2)
Insert into Employee2 values (5, 'Todd', 'Male', 2800, 2)
Insert into Employee2 values (6, 'Ben', 'Male', 7000, 1)
Insert into Employee2 values (7, 'Sara', 'Female', 4800, 3)
Insert into Employee2 values (8, 'Valarie', 'Female', 5500, 1)
Insert into Employee2 values (9, 'James', 'Male', 6500, NULL)
Insert into Employee2 values (10, 'Russell', 'Male', 8800, NULL)
-------
Insert into Employee2 values (11, 'Todd', 'Male', 2800, 4)
Insert into Employee2 values (12, 'Ben', 'Male', 7000, 5)
Insert into Employee2 values (13, 'Sara', 'Female', 4800, 6)
Insert into Employee2 values (14, 'Valarie', 'Female', 5500, 7)
Insert into Employee2 values (15, 'James', 'Male', 6500, 8)
Insert into Employee2 values (16, 'Russell', 'Male', 8800, 7)
Go
---check tables again
select * from tblDepartment
select * From Employee2
--lets check foreign key vales greater than id >8 which is not in tbldeparment
Insert into Employee2 values (17, 'Russell34', 'Male', 8800, 9)
---sp proc
create proc spGetEmployee2ByNameGenderSalary
@Name nvarchar(20),
@Gender nvarchar(20)
as
begin
select Name,Gender,Salary from Employee2 where Name=@Name and Gender=@Gender
end
--exec sp
select * From Employee2
exec spGetEmployee2ByNameGenderSalary 'Sara','Female'
exec spGetEmployee2ByNameGenderSalary 'John','Male'
sp_helptext spGetEmployee2ByNameGenderSalary
---lets sp for upodate
create proc spUpEmployeename
@name nvarchar(20),
@gen nvarchar(20)
as
begin
update Employee2 set Name=@name , Gender=@gen where ID>=17
end
exec spUpEmployeename 'ajman','Male'
--create Stored proc with output para
create proc spGetEmployeenameGenderDepartmentid
@gender nvarchar(20),
@TotalEmp int output
as
begin
select @TotalEmp=COUNT(ID) from Employee2 where
Gender=@gender
end
--exexute for male employee
declare @TotalEMPcount int
exec spGetEmployeenameGenderDepartmentid 'Male', @TotalEMPcount output
print @TotalEMPcount
--Execute for Female employee
declare @TotalEMPcount int
exec spGetEmployeenameGenderDepartmentid 'Female', @TotalEMPcount output
print @TotalEMPcount