-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18-StoreProcedures.sql
More file actions
209 lines (178 loc) · 5.07 KB
/
18-StoreProcedures.sql
File metadata and controls
209 lines (178 loc) · 5.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* WHAT is store procedure
store procedure example
Creating a stored procedure with parameters
Creating a stored Procedure with parameters
Altering sp
Viewing the text of the SP
DROPing the sp
Store Procedure
A store Procedure is a groupe od T-SQL (Transact sql) statement.IF you have
a situatuon, where you write the same query over and over again, You can save
that specify quere as a stored Procedure and call it hust by it's name
1. Use CAREATE PROCESURE or CEATE PROC statement to create store procedure
NOTE: When naming user defined store Procedure, Microsoft recommends not to use
sp_as a prefix. All system stored procedure are prefixed with sp_.This avoids any
ambiguity between user defined and system stored procedures and any conflicte,
with some future system procedure
*/
/*
To execute the store procedure
1: spGetEmployees
2: EXECspGetEmployee
3: Execute spGETEmployee
NOTE: You can also right click on the procedure name, in object explorer in
SQL server Managemnet studio and select EXECUTES STORED PROCEDURE
*/
-- create a store Procedure
CREATE PROCEDURE spGetEmployee
AS
BEGIN
select * from tblEmployee
END
--Store procedure execution
spGetEmployee
-- or
Exec spGetEmployee
-- OR
Execute spGetEmployee
/*
1 Tom Male 4000 London
2 Pam Female 3000 New York
3 John Male 3500 London
4 Sam Male 4500 London
5 Todd Male 2800 Sydney
6 Ben Male 7000 New York
7 Sara Female 4800 Sydney
8 Valarie Female 5500 New York
9 James Male 6500 London
10 Russel Male 8800 London
11 Firoz Male 0 Dhaka
*/
--Store procedure with parameter
CREATE PROCEDURE spGetEMployeeByGender
@gender nvarchar(20),
@city nvarchar(50)
as
begin
Select Name, Gender, Salary, city
from tblEmployee
where Gender = @gender and city = @city
end
--Execute store procedure with parameter
spGetEMployeeByGender 'Male', 'London'
/*
Tom Male 4000 London
John Male 3500 London
Sam Male 4500 London
James Male 6500 London
Russel Male 8800 London
*/
--Execute store procedure with parameter
spGetEMployeeByGender 'Female', 'New York'
/*
Pam Female 3000 New York
Valarie Female 5500 New York
*/
--Store procedure with parameter
CREATE PROCEDURE spGetEMployeeByGenderAndSalary
@gender nvarchar(20),
@salary int
as
begin
Select Name, Gender, Salary, city
from tblEmployee
where Gender = @gender and salary > @salary
end
--execute storeprocedure spGetEMployeeByGenderAndSalary
execute spGetEMployeeByGenderAndSalary @salary = 4000, @gender = "male"
/*
output
Sam Male 4500 London
Ben Male 7000 New York
James Male 6500 London
Russel Male 8800 London
*/
--execute storeprocedure spGetEMployeeByGenderAndSalary
execute spGetEMployeeByGenderAndSalary @gender = "male", @salary = 4000
/*
OUTPUT
Sam Male 4500 London
Ben Male 7000 New York
James Male 6500 London
Russel Male 8800 London
*/
--execute storeprocedure spGetEMployeeByGenderAndSalary
execute spGetEMployeeByGenderAndSalary 'male',4000
/*
Sam Male 4500 London
Ben Male 7000 New York
James Male 6500 London
Russel Male 8800 London
*/
/*
Store Procedure with Parameters
Paremeters and variables have an @prefix in their name
to EXECUTE
execute spGetEMployeeByGenderAndSalary 'male',4000
execute spGetEMployeeByGenderAndSalary @salary = 4000, @gender = "male"
->
to view the text, of the store procedure
1. Use sysytem store procedure sp_helptext'SPname'
OR
2.Right click the SP in Object explorer-> Scrip Procedure as -> Create to -> NEWQUERY EDITOR
WINDO
To change the store procedure, use Alter Procedure statement
TO delete the store Procedure ,use Procedure'SPname' or Drop PROCEDURE'SPname'
To encrypt text of the SP use WITH ENCRYPTION option, IT is not possible
to view the text of n encrypted SP
*/
--TO view Store procedure text
sp_helptext spGetEMployeeByGenderAndSalary
/*
--Store procedure with parameter
CREATE PROCEDURE spGetEMployeeByGenderAndSalary
@gender nvarchar(20),
@salary int
as
begin
Select Name, Gender, Salary, city
from tblEmployee
where Gender = @gender and salary > @salary
end
*/
--Alter store procedure
alter Procedure spGetEMployeeByGenderAndSalary
@gender nvarchar(20),
@salary int
as
begin
Select Name, Gender, Salary, city
from tblEmployee
where Gender = @gender and salary > @salary
Order by Name
end
--execute storeprocedure spGetEMployeeByGenderAndSalary
execute spGetEMployeeByGenderAndSalary 'male',4000
--Drop store procedure
Drop procedure spGetEMployeeByGenderAndSalary
--Alter store procedure
alter Procedure spGetEMployeeByGenderAndSalary
@gender nvarchar(20),
@salary int
WITH encryption
as
begin
Select Name, Gender, Salary, city
from tblEmployee
where Gender = @gender and salary > @salary
Order by Name
end
--execute storeprocedure spGetEMployeeByGenderAndSalary
execute spGetEMployeeByGenderAndSalary 'male',4000
--TO view Store procedure text after encryption
sp_helptext spGetEMployeeByGenderAndSalary
/*
Error message
The text for object 'spGetEMployeeByGenderAndSalary' is encrypted.
*/