Repository navigation
Expand file tree
/
Copy pathRestrict_table_update_ssms
More file actions
106 lines (96 loc) · 2.38 KB
/
Copy pathRestrict_table_update_ssms
File metadata and controls
106 lines (96 loc) · 2.38 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
--Trigger based approach to stop insert,update,delete from SSMS for specific user
--Create a user specialaccess
create user specialaccess with password='pass@123'
--Create a test table
Create table Testaccess (id int,name varchar(10),DB varchar(10));
--Insert a record to test table
insert Testaccess values (1,'abc','ms');
insert Testaccess values (1,'def','mys');
Grant insert,select,update,delete on testaccess to specialaccess;
--
ALTER PROCEDURE [dbo].[sprestrict]
as
DECLARE @id int
Declare @seid int=@@SPID;
select @id=session_id from sys.dm_exec_sessions where program_name like 'Microsoft SQL Server Management Studio%'
and login_name='specialaccess';
print @id;
print @seid;
if (@id=@seid)
RAISERROR (15600,-1,-1,'sleep time');
Go
CREATE TRIGGER Trg_TestAccess_insrt
ON Testaccess
INSTEAD OF INSERT AS
BEGIN
DECLARE @id int
Declare @seid int=@@SPID;
select @id=session_id from sys.dm_exec_sessions where program_name like 'Microsoft SQL Server Management Studio%'
and login_name='specialaccess';
print @id;
print @seid;
if (@id=@seid)
BEGIN
RAISERROR (N'This is audit record and you are not allowed to insert or update',16,1)
RETURN
END
INSERT INTO Testaccess (id,name,db)
SELECT * from INSERTED
END
GO
--
CREATE TRIGGER Trg_TestAccess_upd
ON Testaccess
INSTEAD OF UPDATE AS
BEGIN
DECLARE @id int
Declare @seid int=@@SPID;
select @id=session_id from sys.dm_exec_sessions where program_name like 'Microsoft SQL Server Management Studio%'
and login_name='specialaccess';
print @id;
print @seid;
if (@id=@seid)
BEGIN
RAISERROR (N'This is audit record and you are not allowed to insert or update',16,1)
RETURN
END
BEGIN
if (update(name))
BEGIN
update t
set t.name=i.name
from inserted i inner join testaccess t on t.id=i.id;
END
END
BEGIN
if (update(db))
BEGIN
update t
set t.db=i.db
from inserted i inner join testaccess t on t.id=i.id;
END
END
END
GO
--
CREATE TRIGGER Trg_TestAccess_del
ON Testaccess
INSTEAD OF DELETE AS
BEGIN
DECLARE @id int
Declare @seid int=@@SPID;
select @id=session_id from sys.dm_exec_sessions where program_name like 'Microsoft SQL Server Management Studio%'
and login_name='specialaccess';
print @id;
print @seid;
if (@id=@seid)
BEGIN
RAISERROR (N'This is a audit table you are not allowed to delete records',16,1)
RETURN
END
BEGIN
DELETE FROM Testaccess
where id in (select id from deleted)
END
END
GO