-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21-AdvantagesOfStoreProcedure.sql
More file actions
57 lines (44 loc) · 1.07 KB
/
21-AdvantagesOfStoreProcedure.sql
File metadata and controls
57 lines (44 loc) · 1.07 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
/*
Advantage of Stored procedures
1. Execution plan relation and reusability
2. Reduces network traffic
3. Code reusability and batter maintainability
4. Better Security
5. Avoid SQL-Injecton attact
*/
use My_new_database
GO
select *from tblEmployee
Create procedure sp_GetEmployeeCountBygender
@gender nvarchar(20),
@EmployeeCount int OUTPUT
AS
BEGIN
Select @EmployeeCount = COUNT(ID)
from tblEmployee
where Gender = @gender
END
DECLARE @totalEmployee int
execute sp_GetEmployeeCountBygender @gender = 'Male', @employeeCount = @totalEmployee output
select @totalEmployee as [Total Employee]
sp_helptext sp_GetEmployeeCountBygender
use My_new_database
GO
select *from tblEmployee
use My_new_database
GO
Create Procedure spGetNameByID
@ID int
as
BEGIN
select Name From tblEmployee where id = @id
end
--Loop in sql
DECLARE @i int = 0
WHILE @i < 10
BEGIN
SET @i = @i + 1
Execute spGetNameByID @ID = @i
END
select Name From tblEmployee where id = 1
select Name From tblEmployee where id = 2