-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocking-user-on-sql-server-based-on-schedule.html
566 lines (498 loc) · 36.4 KB
/
blocking-user-on-sql-server-based-on-schedule.html
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Blocking a user on SQL Server based on a schedule - Sipmann</title>
<meta name="monetization" content="$coil.xrptipbot.com/uH6DA7YAQ7y0_IvXt4p0VQ">
<meta property="og:site_name" content="Sipmann"> <meta property="og:type" content="article">
<link rel="alternate" hreflang="x-default" href="https://www.sipmann.com/blocking-user-on-sql-server-based-on-schedule.html">
<link rel="alternate" hreflang="pt" href="https://www.sipmann.com/../pt/blocking-user-on-sql-server-based-on-schedule.html">
<meta property="og:title" content="Blocking a user on SQL Server based on a schedule">
<meta property="og:url" content="https://www.sipmann.com/blocking-user-on-sql-server-based-on-schedule.html">
<meta property="article:author" content="Maurício Camargo Sipmann">
<meta name="description" content="How to block a SQL Server user based on a schedule using the SQL Server Agent and a Table that holds the user and the blocking time.">
<meta property="og:description" content="How to block a SQL Server user based on a schedule using the SQL Server Agent and a Table that holds the user and the blocking time.">
<meta property="article:published_time" content=" 2020-11-14 ">
<meta property="article:section" content="SQL Server"> <meta property="article:tag" content="SQLServer"> <meta property="article:tag" content="User"> <meta property="article:tag" content="Time Schedule blocking">
<meta property="og:locale" content="en">
<meta property="og:image" content="https://www.sipmann.com//images/block_sql_server_user.png"> <link href="https://www.sipmann.com/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Sipmann ATOM Feed"> <link rel="stylesheet" href="/theme/css/w3.css">
<link href="/theme/css/pygments/native.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
<link rel="preload" as="script" href="https://ssl.google-analytics.com">
<link rel="dns-prefetch" href="https://ssl.google-analytics.com">
<style>
body,
h1,
h2,
h3,
h4,
h5 {
font-family: "Raleway", sans-serif
}
a {
text-decoration: none;
}
pre {
display: block;
padding: 9.5px;
margin: 0 0 10px;
font-size: 13px;
line-height: 1.428571429;
word-break: break-all;
word-wrap: break-word;
color: #333;
border: 1px solid #ccc;
border-radius: 4px;
}
.banner_img {
background-position: 50%;
background-repeat: no-repeat;
background-size: cover;
/*background-attachment: fixed;*/
height: 100px;
}
article img {
max-width: -webkit-fill-available;
}
.highlight {
overflow-x: auto;
}
p > a {
color: blue;
}
.w3-container p {
text-indent: 10px;
text-align: justify;
}
@media (min-width: 993px) {
#mySidebar {
position: initial !important;
background: #f1f1f1 !important;
}
}
@media screen and (prefers-color-scheme: dark) {
.w3-light-gray {
background-color: #2f3237 !important;
color: white !important;
}
#mySidebar {
position: initial !important;
background: #2f3237 !important;
}
hr {
border-color: #1c1d1e;
}
}
</style>
</head>
<body class="w3-light-gray">
<div class="w3-top">
<nav class="w3-bar w3-dark-gray w3-card-4">
<button class="w3-bar-item w3-button w3-hide-large w3-hover-none w3-hover-text-light-grey" onclick="w3_open();" type="button">
<i class="fa fa-bars"></i> Menu</button>
<a href="/" class="w3-bar-item w3-button">Home</a>
<a class="w3-bar-item w3-button w3-light-gray" href="https://www.sipmann.com/en/">en</a>
<a class="w3-bar-item w3-button" href="https://www.sipmann.com/pt/">pt</a>
</nav>
</div>
<header class="w3-container w3-center" style="padding:22px 0 0 0;">
<div style="height: 130px;width:100%; background-image: url('/images/header.webp');background-size: cover;"></div>
</header>
<div class="w3-content w3-margin-top" style="max-width:1400px;">
<div class="w3-row">
<div class="w3-col l9 s12">
<article class="w3-card-4 w3-margin w3-white">
<div class="w3-container">
<h1><b><a href="https://www.sipmann.com/blocking-user-on-sql-server-based-on-schedule.html" title="Permalink to Blocking a user on SQL Server based on a schedule">Blocking a user on SQL Server based on a schedule</a></b></h1>
<h5>
<i class="fa fa-calendar"></i><time class="w3-opacity" datetime="2020-11-14T07:00:00-03:00"> Sat 14 November 2020</time>
</h5>
</div>
<div class="w3-container">
<p><p>Ok, some time ago, I've posted about how you can set limits to connections on SQL Server using the <a href="https://www.sipmann.com/limiting-connection-resources-sql-server.html#.X6Cz8IhKhPY">Resource Governor</a>. But what if you can't use it? You always can block logins using a login trigger, but I don't like the idea of having selects running on every login. So I came across with a solution using a stored procedure, a table and the Agent.</p>
<p>The main idea is, store the time that a user must be blocked and using the Agent, disable or enable the user. Bellow you can see the table (the table is in Portuguese, but I have a few comment blocks to help you).</p>
<div class="highlight"><pre><span></span><code><span class="k">CREATE</span><span class="w"> </span><span class="nc">TABLE</span><span class="w"> </span><span class="n">dbo</span><span class="p">.</span><span class="n">HorariosBloqueio</span><span class="w"> </span><span class="p">(</span><span class="w"></span>
<span class="w"> </span><span class="n">Id</span><span class="w"> </span><span class="nc">INT</span><span class="w"> </span><span class="ow">NOT</span><span class="w"> </span><span class="k">NULL</span><span class="p">,</span><span class="w"></span>
<span class="w"> </span><span class="n">LoginName</span><span class="w"> </span><span class="nc">NVARCHAR</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span><span class="w"> </span><span class="ow">NOT</span><span class="w"> </span><span class="k">NULL</span><span class="p">,</span><span class="w"></span>
<span class="w"> </span><span class="n">HrInicio</span><span class="w"> </span><span class="nc">TIME</span><span class="w"> </span><span class="ow">NOT</span><span class="w"> </span><span class="k">NULL</span><span class="p">,</span><span class="w"> </span><span class="cm">/* Startint block time */</span><span class="w"></span>
<span class="w"> </span><span class="n">HrTermino</span><span class="w"> </span><span class="nc">TIME</span><span class="w"> </span><span class="ow">NOT</span><span class="w"> </span><span class="k">NULL</span><span class="p">,</span><span class="w"> </span><span class="cm">/* Ending block time */</span><span class="w"></span>
<span class="w"> </span><span class="n">Bloqueado</span><span class="w"> </span><span class="nc">INT</span><span class="w"> </span><span class="k">DEFAULT</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="cm">/* 0 = unblocked, 1 = blocked */</span><span class="w"></span>
<span class="w"> </span><span class="k">PRIMARY</span><span class="w"> </span><span class="k">KEY</span><span class="w"> </span><span class="p">(</span><span class="n">Id</span><span class="p">)</span><span class="w"></span>
<span class="p">);</span><span class="w"></span>
<span class="k">GO</span><span class="w"></span>
<span class="cm">/* Don't block the SA user, precautions, you know */</span><span class="w"></span>
<span class="k">ALTER</span><span class="w"> </span><span class="nc">TABLE</span><span class="w"> </span><span class="n">dbo</span><span class="p">.</span><span class="n">HorariosBloqueio</span><span class="w"></span>
<span class="w"> </span><span class="k">ADD</span><span class="w"> </span><span class="k">CONSTRAINT</span><span class="w"> </span><span class="n">chk_users</span><span class="w"> </span><span class="k">CHECK</span><span class="w"> </span><span class="p">(</span><span class="n">LoginName</span><span class="w"> </span><span class="ow">not</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="p">(</span><span class="s1">'sa'</span><span class="p">));</span><span class="w"></span>
<span class="k">ALTER</span><span class="w"> </span><span class="nc">TABLE</span><span class="w"> </span><span class="n">dbo</span><span class="p">.</span><span class="n">HorariosBloqueio</span><span class="w"></span>
<span class="w"> </span><span class="k">ADD</span><span class="w"> </span><span class="k">CONSTRAINT</span><span class="w"> </span><span class="n">chk_hora_final_maior</span><span class="w"> </span><span class="k">CHECK</span><span class="w"> </span><span class="p">(</span><span class="n">HrTermino</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">HrInicio</span><span class="p">);</span><span class="w"></span>
<span class="k">ALTER</span><span class="w"> </span><span class="nc">TABLE</span><span class="w"> </span><span class="n">dbo</span><span class="p">.</span><span class="n">HorariosBloqueio</span><span class="w"></span>
<span class="w"> </span><span class="k">ADD</span><span class="w"> </span><span class="k">CONSTRAINT</span><span class="w"> </span><span class="n">chk_status_bloqueio</span><span class="w"> </span><span class="k">CHECK</span><span class="w"> </span><span class="p">(</span><span class="n">Bloqueado</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">));</span><span class="w"></span>
<span class="k">CREATE</span><span class="w"> </span><span class="k">SEQUENCE</span><span class="w"> </span><span class="n">dbo</span><span class="p">.</span><span class="n">seq_HorariosBloqueio</span><span class="w"> </span><span class="k">START</span><span class="w"> </span><span class="k">WITH</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="n">INCREMENT</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="k">GO</span><span class="w"></span>
</code></pre></div>
<p>After creating the table, let's check the procedure that will handle the enabling/disabling the users. Be aware that on the procedure, I've set the database name where the table was stored, you can change it replacing the <code>DBATOOLS</code> text to the database name where you created the table.</p>
<div class="highlight"><pre><span></span><code><span class="k">IF</span><span class="w"> </span><span class="nf">OBJECT_ID</span><span class="p">(</span><span class="s1">'dbo.sp_ValidarLogin'</span><span class="p">)</span><span class="w"> </span><span class="k">IS</span><span class="w"> </span><span class="k">NULL</span><span class="w"></span>
<span class="w"> </span><span class="k">EXEC</span><span class="w"> </span><span class="p">(</span><span class="s1">'CREATE PROCEDURE dbo.sp_ValidarLogin AS RETURN 0;'</span><span class="p">);</span><span class="w"></span>
<span class="k">GO</span><span class="w"></span>
<span class="k">CREATE</span><span class="w"> </span><span class="ow">OR</span><span class="w"> </span><span class="k">ALTER</span><span class="w"> </span><span class="k">PROC</span><span class="w"> </span><span class="n">dbo</span><span class="p">.</span><span class="n">sp_ValidarLogin</span><span class="w"></span>
<span class="k">AS</span><span class="w"> </span><span class="k">BEGIN</span><span class="w"></span>
<span class="w"> </span><span class="k">DECLARE</span><span class="w"> </span><span class="nv">@LoginName</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="nc">NVARCHAR</span><span class="p">(</span><span class="mi">100</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="k">DECLARE</span><span class="w"> </span><span class="nv">@Momento</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="nc">TIME</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">SET</span><span class="w"> </span><span class="nv">@Momento</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">CAST</span><span class="p">(</span><span class="nf">GETDATE</span><span class="p">()</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="nc">TIME</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="cm">/* Block the ones that aren't blocked already and maches the time */</span><span class="w"></span>
<span class="w"> </span><span class="k">DECLARE</span><span class="w"> </span><span class="n">block_cursor</span><span class="w"> </span><span class="nc">CURSOR</span><span class="w"></span>
<span class="w"> </span><span class="k">FOR</span><span class="w"> </span><span class="k">SELECT</span><span class="w"> </span><span class="n">LoginName</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="o">[</span><span class="n">DBATOOLS</span><span class="o">]</span><span class="p">.</span><span class="o">[</span><span class="n">dbo</span><span class="o">]</span><span class="p">.</span><span class="o">[</span><span class="n">HorariosBloqueio</span><span class="o">]</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">Bloqueado</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="ow">AND</span><span class="w"> </span><span class="n">HrInicio</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="nv">@Momento</span><span class="w"> </span><span class="ow">AND</span><span class="w"> </span><span class="n">HrTermino</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="nv">@Momento</span><span class="w"></span>
<span class="w"> </span><span class="k">OPEN</span><span class="w"> </span><span class="n">block_cursor</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">FETCH</span><span class="w"> </span><span class="k">NEXT</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">block_cursor</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="nv">@LoginName</span><span class="w"></span>
<span class="w"> </span><span class="k">WHILE</span><span class="w"> </span><span class="nb">@@FETCH_STATUS</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"></span>
<span class="w"> </span><span class="k">BEGIN</span><span class="w"></span>
<span class="w"> </span><span class="k">exec</span><span class="w"> </span><span class="p">(</span><span class="s1">'ALTER LOGIN '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nv">@LoginName</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s1">' DISABLE;'</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="k">print</span><span class="w"> </span><span class="s1">'Bloqued usuario '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nv">@LoginName</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">FETCH</span><span class="w"> </span><span class="k">NEXT</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">block_cursor</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="nv">@LoginName</span><span class="w"></span>
<span class="w"> </span><span class="k">END</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">CLOSE</span><span class="w"> </span><span class="n">block_cursor</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">DEALLOCATE</span><span class="w"> </span><span class="n">block_cursor</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">UPDATE</span><span class="w"> </span><span class="o">[</span><span class="n">DBATOOLS</span><span class="o">]</span><span class="p">.</span><span class="o">[</span><span class="n">dbo</span><span class="o">]</span><span class="p">.</span><span class="o">[</span><span class="n">HorariosBloqueio</span><span class="o">]</span><span class="w"> </span><span class="k">SET</span><span class="w"> </span><span class="n">Bloqueado</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">HrInicio</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="nv">@Momento</span><span class="w"> </span><span class="ow">AND</span><span class="w"> </span><span class="n">HrTermino</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="nv">@Momento</span><span class="w"></span>
<span class="w"> </span><span class="cm">/* Enable up who was blocked */</span><span class="w"></span>
<span class="w"> </span><span class="k">DECLARE</span><span class="w"> </span><span class="n">unblock_cursor</span><span class="w"> </span><span class="nc">CURSOR</span><span class="w"></span>
<span class="w"> </span><span class="k">FOR</span><span class="w"> </span><span class="k">SELECT</span><span class="w"> </span><span class="n">LoginName</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="o">[</span><span class="n">DBATOOLS</span><span class="o">]</span><span class="p">.</span><span class="o">[</span><span class="n">dbo</span><span class="o">]</span><span class="p">.</span><span class="o">[</span><span class="n">HorariosBloqueio</span><span class="o">]</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">Bloqueado</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="ow">AND</span><span class="w"> </span><span class="p">(</span><span class="n">HrInicio</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="nv">@Momento</span><span class="w"> </span><span class="ow">OR</span><span class="w"> </span><span class="n">HrTermino</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nv">@Momento</span><span class="p">)</span><span class="w"></span>
<span class="w"> </span><span class="k">OPEN</span><span class="w"> </span><span class="n">unblock_cursor</span><span class="w"> </span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">FETCH</span><span class="w"> </span><span class="k">NEXT</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">unblock_cursor</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="nv">@LoginName</span><span class="w"></span>
<span class="w"> </span><span class="k">WHILE</span><span class="w"> </span><span class="nb">@@FETCH_STATUS</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"></span>
<span class="w"> </span><span class="k">BEGIN</span><span class="w"></span>
<span class="w"> </span><span class="k">exec</span><span class="w"> </span><span class="p">(</span><span class="s1">'ALTER LOGIN '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nv">@LoginName</span><span class="w"> </span><span class="o">+</span><span class="s1">' ENABLE;'</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="k">print</span><span class="w"> </span><span class="s1">'Unbloqued usuario '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nv">@LoginName</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">FETCH</span><span class="w"> </span><span class="k">NEXT</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">unblock_cursor</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="nv">@LoginName</span><span class="w"></span>
<span class="w"> </span><span class="k">END</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">CLOSE</span><span class="w"> </span><span class="n">unblock_cursor</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">DEALLOCATE</span><span class="w"> </span><span class="n">unblock_cursor</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">UPDATE</span><span class="w"> </span><span class="o">[</span><span class="n">DBATOOLS</span><span class="o">]</span><span class="p">.</span><span class="o">[</span><span class="n">dbo</span><span class="o">]</span><span class="p">.</span><span class="o">[</span><span class="n">HorariosBloqueio</span><span class="o">]</span><span class="w"> </span><span class="k">SET</span><span class="w"> </span><span class="n">Bloqueado</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">Bloqueado</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="ow">AND</span><span class="w"> </span><span class="p">(</span><span class="n">HrInicio</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="nv">@Momento</span><span class="w"> </span><span class="ow">OR</span><span class="w"> </span><span class="n">HrTermino</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nv">@Momento</span><span class="p">)</span><span class="w"></span>
<span class="k">END</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>Ok, so now all you have to do, is schedule a job to run that stored procedure from minute to minute. Again, the main idea is tell the procedure when a user must be blocked and when it'll be unblocked. </p>
<div class="highlight"><pre><span></span><code><span class="err"> -- Will block the user protheus from 10 AM till 15 PM</span>
<span class="err"> INSERT INTO dbo.HorariosBloqueio (Id, LoginName, HrInicio, HrTermino) VALUES (NEXT VALUE FOR seq_HorariosBloqueio, 'protheus', '10:00:00', '15:00:00');</span>
</code></pre></div></p>
</div>
<hr>
<section class="w3-padding" id="translations">
<h4>Translations:</h4>
<ul>
<li><a href="../pt/blocking-user-on-sql-server-based-on-schedule.html">pt - Bloqueando um usuário do SQL Server baseado em uma tabela de horários</a></li>
</ul>
</section>
<hr>
<section class="w3-padding" id="related-posts">
<h4>Related Posts:</h4>
<ul>
<li><a href="https://www.sipmann.com/removing-user-from-sql-server-database.html">Removing a user from SQL Server database who owns a schema</a></li>
<li><a href="https://www.sipmann.com/limiting-connection-resources-sql-server.html">Limiting Connection Resources on SQL Server</a></li>
</ul>
<script type="application/ld+json">
{
"@context":"http://schema.org",
"@type":"ItemList",
"name": "Related Posts",
"itemListElement":[
{
"@type":"Article",
"position":1,
"name": "Removing a user from SQL Server database who owns a schema",
"url":"https://www.sipmann.com/removing-user-from-sql-server-database.html",
"headline": "Removing a user from SQL Server database who owns a schema",
"image": ["images/sipmann.com.png"],
"datePublished": "2019-09-06 07:00:00-03:00",
"dateModified": "2019-09-06 07:00:00-03:00",
"author": {
"@type": "Person",
"name": "Maurício Camargo Sipmann"
},
"publisher": {
"@type": "Organization",
"name": "Maurício Camargo Sipmann"
,
"logo": {
"@type": "ImageObject",
"url": "https://s.gravatar.com/avatar/7f717d69fb6d3dbc5490d47369aca3e0?s=180",
"height": 180,
"width": 180
}
}
}
,
{
"@type":"Article",
"position":2,
"name": "Limiting Connection Resources on SQL Server",
"url":"https://www.sipmann.com/limiting-connection-resources-sql-server.html",
"headline": "Limiting Connection Resources on SQL Server",
"image": ["/images/sql-pool.png"],
"datePublished": "2019-08-12 12:00:00-03:00",
"dateModified": "2019-08-12 12:00:00-03:00",
"author": {
"@type": "Person",
"name": "Maurício Camargo Sipmann"
},
"publisher": {
"@type": "Organization",
"name": "Maurício Camargo Sipmann"
,
"logo": {
"@type": "ImageObject",
"url": "https://s.gravatar.com/avatar/7f717d69fb6d3dbc5490d47369aca3e0?s=180",
"height": 180,
"width": 180
}
}
}
]
}
</script>
</section>
</article>
<hr>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"name": "Post: Blocking a user on SQL Server based on a schedule",
"itemListElement": [{
"@type": "ListItem",
"position": 1,
"item": {
"@id": "https://www.sipmann.com",
"name": "Article"
}
},{
"@type": "ListItem",
"position": 2,
"item": {
"@id": "https://www.sipmann.com/category/sql-server.html",
"name": "SQL Server"
}
},{
"@type": "ListItem",
"position": 3,
"item": {
"@id": "https://www.sipmann.com/blocking-user-on-sql-server-based-on-schedule.html",
"name": "Blocking a user on SQL Server based on a schedule"
}
}]
}
</script>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Article",
"@id" : "https://www.sipmann.com/blocking-user-on-sql-server-based-on-schedule.html",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://google.com/article"
},
"headline": "Blocking a user on SQL Server based on a schedule",
"image": ["/images/block_sql_server_user.png"],
"datePublished": "2020-11-14 07:00:00-03:00",
"dateModified": "2020-11-14 07:00:00-03:00",
"author": {
"@type": "Person",
"name": "Maurício Camargo Sipmann"
,
"image": {
"@type": "ImageObject",
"id": "https://www.sipmann.com/#personlogo",
"url": "https://s.gravatar.com/avatar/7f717d69fb6d3dbc5490d47369aca3e0?s=180",
"height": 180,
"width": 180,
"caption": "Maurício Sipmann"
}
},
"publisher": {
"@type": "Organization",
"name": "Maurício Camargo Sipmann"
,
"logo": {
"@type": "ImageObject",
"url": "https://s.gravatar.com/avatar/7f717d69fb6d3dbc5490d47369aca3e0?s=180",
"height": 180,
"width": 180
}
},
"articleSection": "
SQLServer
,
User
,
Time Schedule blocking
"
}
</script>
</div>
<div class="w3-sidebar w3-col l3 w3-collapse w3-animate-right" id="mySidebar" style="right:0;">
<div class="w3-card w3-margin w3-margin-top" role="navigation" aria-labelledby="nav-categories">
<div class="w3-container w3-padding w3-blue-gray">
<h4 id="nav-categories">Categories</h4>
</div>
<ul class="w3-ul w3-hoverable w3-white">
<li style="padding: 0 !important;">
<a class="w3-padding" href="https://www.sipmann.com/category/azure.html" style="display: block">
Azure
</a>
</li>
<li style="padding: 0 !important;">
<a class="w3-padding" href="https://www.sipmann.com/category/flutter.html" style="display: block">
Flutter
</a>
</li>
<li style="padding: 0 !important;">
<a class="w3-padding" href="https://www.sipmann.com/category/front-end.html" style="display: block">
Front-end
</a>
</li>
<li style="padding: 0 !important;">
<a class="w3-padding" href="https://www.sipmann.com/category/java.html" style="display: block">
Java
</a>
</li>
<li style="padding: 0 !important;">
<a class="w3-padding" href="https://www.sipmann.com/category/linux.html" style="display: block">
Linux
</a>
</li>
<li style="padding: 0 !important;">
<a class="w3-padding" href="https://www.sipmann.com/category/mysql.html" style="display: block">
MySQL
</a>
</li>
<li style="padding: 0 !important;">
<a class="w3-padding" href="https://www.sipmann.com/category/net.html" style="display: block">
.NET
</a>
</li>
<li style="padding: 0 !important;">
<a class="w3-padding" href="https://www.sipmann.com/category/powershell.html" style="display: block">
PowerShell
</a>
</li>
<li style="padding: 0 !important;">
<a class="w3-padding" href="https://www.sipmann.com/category/random.html" style="display: block">
Random
</a>
</li>
<li style="padding: 0 !important;">
<a class="w3-padding" href="https://www.sipmann.com/category/sql-server.html" style="display: block">
SQL Server
</a>
</li>
<li style="padding: 0 !important;">
<a class="w3-padding" href="https://www.sipmann.com/category/tools.html" style="display: block">
Tools
</a>
</li>
</ul>
</div>
<div class="w3-card w3-margin w3-margin-top" role="navigation" aria-labelledby="nav-recent-posts">
<div class="w3-container w3-padding w3-blue-gray">
<h4 id="nav-recent-posts">Recent Posts</h4>
</div>
<ul class="w3-ul w3-hoverable w3-white">
<li>
<a class="w3-padding" href="/monitoring-mysql-restarts-with-powershell.html">
Monitoring MySQL restarts with PowerShell
</a>
</li>
<li>
<a class="w3-padding" href="/mysql-dual-password-functionality.html">
Changing a MySQL user password across multiple application instances without downtime
</a>
</li>
<li>
<a class="w3-padding" href="/apache-nifi-json-to-sql-replacing-underscore.html">
Apache Nifi JSON to SQL Replacing underscore
</a>
</li>
<li>
<a class="w3-padding" href="/blocking-user-on-sql-server-based-on-schedule.html">
Blocking a user on SQL Server based on a schedule
</a>
</li>
<li>
<a class="w3-padding" href="/select_specific_fields_with_efcore.html">
How to set columns on select using Entity Framework
</a>
</li>
</ul>
</div>
<div class="w3-margin w3-margin-top" role="navigation" aria-labelledby="buy-me-a-coffee">
<div class="w3-container w3-padding">
<a href="https://www.buymeacoffee.com/sipmann" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-blue.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;"></a>
</div>
</div> </div>
</div>
</div>
<footer class="w3-container w3-dark-gray w3-padding-32 w3-margin-top">
<div class="w3-bar">
<a class="w3-bar-item" href="https://twitter.com/MSipmann" target="_blank" rel="noopener noreferrer"><i class="fa fa-twitter-square fa-lg"></i> Twitter</a>
<a class="w3-bar-item" href="https://github.com/sipmann" target="_blank" rel="noopener noreferrer"><i class="fa fa-github-square fa-lg"></i> Github</a>
<a class="w3-bar-item" href="http://br.linkedin.com/in/sipmann" target="_blank" rel="noopener noreferrer"><i class="fa fa-linkedin-square fa-lg"></i> Linkedin</a>
</div>
</footer>
<!-- <script data-ad-client="ca-pub-3097623931513783" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> -->
<!-- Google Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-8072083-2']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- End Google Analytics Code -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-BW422MP4C1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-BW422MP4C1');
</script>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Person",
"name": "Sipmann",
"url": "https://www.sipmann.com"
, "sameAs": [
"https://twitter.com/MSipmann",
"https://github.com/sipmann",
"http://br.linkedin.com/in/sipmann"
]
}
</script>
<script>
// Get the Sidebar
var mySidebar = document.getElementById("mySidebar");
// Get the DIV with overlay effect
var overlayBg = document.getElementById("myOverlay");
function w3_open() {
if (mySidebar.style.display === 'block') {
mySidebar.style.display = 'none';
overlayBg.style.display = "none";
} else {
mySidebar.style.display = 'block';
overlayBg.style.display = "block";
}
}
function w3_close() {
mySidebar.style.display = "none";
overlayBg.style.display = "none";
}
</script>
</body>
</html>