You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+37-4
Original file line number
Diff line number
Diff line change
@@ -8,19 +8,26 @@ Explicit validation to ensure the object, variable or component is in the expect
8
8
You might want to check if a object already exist before creating it. You can use this patterns for table, procedures or any object in the database.
9
9
```sql
10
10
IF EXISTS (SELECT [object_id] FROMtempdb.sys.objects (NOLOCK) WHERE [object_id] = OBJECT_ID('tempdb.dbo.#<table_name>'))
11
-
DROPTABLE#<table_name>;
11
+
DROPTABLE#<table_name>;
12
12
```
13
13
14
14
### Check if database exists
15
15
Does the database exists and is it up and running?
16
16
```sql
17
17
IF EXISTS (SELECT CASE WHEN DATABASEPROPERTYEX(name, 'Status') ='ONLINE' THEN 1 END FROMmaster.dbo.sysdatabases WHERE name =<db_name>)
18
-
PRINT N'<db_name> is available';
18
+
PRINT N'<db_name> is available';
19
19
```
20
20
### Check if is primary node
21
-
Check if the current replica of a Availability Group is the primary one.
21
+
Check if the current replica of a Availability Group is the primary one. First check if AG is enabled.
22
22
```sql
23
-
IF sys.fn_hadr_is_primary_replica ('<db_name>') =
23
+
IF (SELECT SERVERPROPERTY('IsHadrEnabled')) =1
24
+
BEGIN
25
+
IF sys.fn_hadr_is_primary_replica ( @DatabaseName ) =1
26
+
BEGIN
27
+
PRINT N'Database is not the primary';
28
+
END;
29
+
END;
30
+
24
31
```
25
32
26
33
### Null or empty
@@ -67,5 +74,31 @@ END CATCH;
67
74
### Use the database context
68
75
69
76
## Data manipulation
77
+
### Deleting a large number of rows
78
+
It´s a script you can use to purge a large number of rows of a given table. You can start/stop the script freely, it won’t hurt anything. It´s setup to delete 1000 records per loop, but you can change this value based in your needs. The most important thing is to use a loop value which does not cause T-LOG contention.
70
79
```sql
80
+
BEGIN
81
+
SET NOCOUNT, XACT_ABORT ON;
82
+
DECLARE @ErrorMessage NVARCHAR(4000), @rows INT;
83
+
BEGIN TRY
84
+
SET @rows =1;
85
+
WHILE (@rows >0)
86
+
BEGIN
87
+
BEGIN TRANSACTION
88
+
-- purge data older than 90 days
89
+
DELETE TOP(1000) FROM [dbo].[<table_name>] WHERE [LastUpdated] <= DATEADD(M, -3, GETDATE());
0 commit comments