-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLAdvanced 14.sql
More file actions
321 lines (318 loc) · 8.42 KB
/
SQLAdvanced 14.sql
File metadata and controls
321 lines (318 loc) · 8.42 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
select * from Employee2
-----
create proc sp_emplnamegender
as
begin
select ID,
Name,Gender from
Employee2
end
-- exex
exec sp_emplnamegender
----------------------------
select * from test5
-------------------
create proc sp_test5_id_name
(@id int,
@name nvarchar(50),
@age int
)
as
begin
insert into test5(id,name,age)
values(
@id,@name,@age
)
end
---exec
exec sp_test5_id_name 18,'suliman khan',34
exec sp_test5_id_name 19,'Noman khan',36
select * from test5
sp_help sp_test5_id_name
sp_helptext sp_test5_id_name
sp_rename 'sp_test5_id_name' , 'Sp_Test5idname'
----------------------------
CREATE PROCEDURE uspUpdateEmpSalary
(
@empId int
,@salary float
)
AS
BEGIN TRY
UPDATE tblEmploye
SET Salary = @salary
WHERE ID = @empId
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() as ErrorNumber,
ERROR_SEVERITY() as ErrorSeverity,
ERROR_STATE() as ErrorState,
ERROR_MESSAGE() as ErrorMessage;
END CATCH
select * from tblEmploye
exec uspUpdateEmpSalary 3,85000
----
create proc sp_employee_salaraymoney
@id int,
@salary money
as
begin
update tblEmploye
set Salary= @salary
where ID=@id
end
-----------------
exec sp_employee_salaraymoney 2,99000
select * from tblEmploye
----lets take another examples
select * from player
create proc sp_Playerproc
(@rank int,
@name nvarchar(20),
@best int)
as
begin
insert into player(rank,name,best)
values
(@rank,@name,@best)
end
---
exec sp_Playerproc 6,'ajman',198
-----
select * from player
select * from tblDepartment
create proc sp_Departmenthead
@empid int,
@Dephead nvarchar(20) output
as
begin
select DepartmentHead=@Dephead
from tblDepartment
where ID=@empid
end
---execution
declare @Dephead varchar(20)
exec sp_Departmenthead 5,@Dephead output
print @Dephead
--------------create cluster indexes
CREATE TABLE EmployeeDetails(
EmployeeID int NOT NULL,
PassportNumber varchar(50) NULL,
ExpiryDate date NULL)
Insert into EmployeeDetails values(3,'A5423215',null);
Insert into EmployeeDetails values(5,'A5423215',null);
Insert into EmployeeDetails values(2,'A5423215',null);
Insert into EmployeeDetails values(8,'A5423215',null);
Insert into EmployeeDetails values(1,'A5423215',null);
Insert into EmployeeDetails values(4,'A5423215',null);
Insert into EmployeeDetails values(6,'A5423215',null);
Insert into EmployeeDetails values(7,'A5423215',null);
-------
select * from EmployeeDetails
---create clustered index
CREATE CLUSTERED INDEX CIX_EmpDetails_EmpId
ON EmployeeDetails(EmployeeID);
---
select * from EmployeeDetails
----
create clustered index Cix_empdetails_idandPass
on EmployeeDetails(EmployeeID ASC,PassportNumber Desc);
----Drop index
Drop index CIX_EmpDetails_EmpId
on EmployeeDetails;
---create another
create clustered index Cix_empdetails_idandPass
on EmployeeDetails(EmployeeID ASC,PassportNumber Desc);
------check table
select * from EmployeeDetails
-- drop cluter index
select * from tblEmployee
--- create non-clustored index
select * from Person
create nonclustered index ncix_Email
on Person(Email);
--- crete trigger
CREATE TABLE EmpLog (
LogID int IDENTITY(1,1) NOT NULL,
EmpID int NOT NULL,
Operation nvarchar(10) NOT NULL,
UpdatedDate Datetime NOT NULL
)
CREATE TABLE TblEmployeek (
LogID int IDENTITY(1,1) NOT NULL,
EmpID int NOT NULL,
Operation nvarchar(10) NOT NULL,
UpdatedDate Datetime NOT NULL
)
------------------
CREATE TRIGGER trgEmployeeKInsert
ON TblEmployeek2
FOR INSERT
AS
INSERT INTO EmpLog(EmpID, Operation, UpdatedDate)
SELECT DepartmentID ,'INSERT',GETDATE() FROM INSERTED; --virtual table INSERTED
---------------
create table TblEmployeek2(FirstName nvarchar(20),
LastName nvarchar(20)
,EMail nvarchar(20)
,Phone int
,HireDate date
,ManagerID int
,Salary int
,
DepartmentID int )
INSERT INTO TblEmployeek2
VALUES('Manisha2'
,'Dutt2'
,'MD456@abc.com'
,689
,'11/07/2015'
,7
,50000
,30),
('Manisha5'
,'Dutt5'
,'MD456@5abc.com'
,676
,'11/07/2015'
,9
,50000
,35)
----------------
select * from EmpLog
select * from TblEmployeek2
insert into TblEmployeek2 values('ajman','khan','ajman41288@gmail.com',678,'11/08/2022',76,780000,67)
select * from EmpLog
select * from TblEmployeek2
---- create trigger
select * from sales2
select * from sales
create trigger trgsalesinsert
on sales
for insert
as
insert into sales2(customername,productname,Salary,vendorname)
select customername,productname, 'INSERT', getdate() from inserted;
insert into sales values('ajman','perfume',67000,'batta')
----after insert
CREATE TRIGGER trgEmployeeKafterinsert
ON TblEmployeek2
after update
AS
INSERT INTO EmpLog(EmpID, Operation, UpdatedDate)
SELECT DepartmentID ,'INSERT',GETDATE() FROM INSERTED; --virtual table INSERTED
--------------------
insert into TblEmployeek2 values('ajmanullah','khan','ajman41288@gmail.com',698,'11/08/2022',86,980000,87)
select * from EmpLog
select * from TblEmployeek2
CREATE TRIGGER dbo.trgInsteadOfDelete
ON TblEmployeeK2
INSTEAD OF DELETE
AS
INSERT INTO EmpLog(EmpID, Operation, UpdatedDate)
SELECT DepartmentID,'DELETE', GETDATE() FROM DELETED;
DELETE FROM TblEmployeek2
WHERE DepartmentID = 87;
---check both table
select * from EmpLog
select * from TblEmployeek2
------
select * from tblEmployee
select * from tblEmployeeAudit
sp_helptext trforinsertemploye
---
create trigger tr_tblemployee_forinsert
on tblEmployee
for insert
as
begin
select * from inserted
-- declare @id int,
--select @id=id from inserted
--insert into tblEmployeeAudit
--values('new employee with id = '+CAST(@id as nvarchar(5))+ 'is added at '
end
-----------------
INSERT INTO [dbo].[tblEmployee]
([name]
,[gender]
,[salary]
,[city])
VALUES
('ajman','Male',90000,'bannu')
---------
alter trigger tr_tblemployee_forinsert
on tblEmployee
for insert
as
begin
select name,salary,city from inserted
-- declare @id int,
--select @id=id from inserted
--insert into tblEmployeeAudit
--values('new employee with id = '+CAST(@id as nvarchar(5))+ 'is added at '
end
-----------------------insert
INSERT INTO [dbo].[tblEmployee]
([name]
,[gender]
,[salary]
,[city])
VALUES
('zeeshan','Male',90000,'lahore'),
('kalim','male',89000,'bannu')
-------------------
select * from tblEmploye
alter trigger tr_tblemployee_forinsert
on tblEmployee
for insert
as
begin
declare @id int
select @id=id from inserted
insert into tblEmployeeAudit
values('new employee with id = '+ CAST(@id as nvarchar(5))+ 'is added at'+ CAST(getdate() as nvarchar(20)))
end
-------------------------------------------------------------------------
CREATE TRIGGER tr_tblEMploy_ForInsert
ON tblEmployee
FOR INSERT
AS
BEGIN
Declare @Id int
Select @Id = Id from inserted
insert into tblEmployeeAudit
values('New employee with Id = ' + Cast(@Id as nvarchar(5)) + ' is added at ' + cast(Getdate() as nvarchar(20)))
END
--------
select * from tblEmployee
INSERT INTO [dbo].[tblEmployee]
([name]
,[gender]
,[salary]
,[city])
VALUES
('Roshan','Male',90000,'lahore'),('kalim2','male',99000,'bannu')
--------------------------
select * from tblEmployee
select * from tblEmployeeAudit
-------------------------------------------------------------
CREATE TRIGGER tr_tblEMploy_ForDelete
ON tblEmployee
FOR DELETE
AS
BEGIN
Declare @Id int
Select @Id = Id from deleted
insert into tblEmployeeAudit
values('An existing employee with Id = ' + Cast(@Id as nvarchar(5)) +
' is deleted at ' + Cast(Getdate() as nvarchar(20)))
END
----------------
select * from tblEmployeeAudit
DELETE FROM [dbo].[tblEmployee]
WHERE id=15;
---check table both
select * from tblEmployee
select * from tblEmployeeAudit