Beste Tweakers,
Iedereen heeft recht op zijn dagelijkse n00b-momentje. Zo heb ik er een vandaag.
Dagelijks loopt er een scriptje op onze database die gebruikers na 18 maanden inactiviteit blokkeert. Er worden 2 tabellen bijgewerkt (geanonimiseerd) na een select van de inactieve users.
Het gaat mij om het "SELECT DISTINCT" gedeelte. Dit komt 2 keer voor en is identiek. Elke aanpassing moet ik dus 2 keer doen, 1 in de bovenste en 1 in de onderste. Dit moet simpeler kunnen dacht ik zo. De gehele select in een variable. Dus met een hoop DECLARE, SET en een partij komma's, quote's etc etc in de weer geweest en ... 0 resultaat: MSMS vreet het niet.
Hoe vlieg ik dit aan??
Iedereen heeft recht op zijn dagelijkse n00b-momentje. Zo heb ik er een vandaag.
Dagelijks loopt er een scriptje op onze database die gebruikers na 18 maanden inactiviteit blokkeert. Er worden 2 tabellen bijgewerkt (geanonimiseerd) na een select van de inactieve users.
Het gaat mij om het "SELECT DISTINCT" gedeelte. Dit komt 2 keer voor en is identiek. Elke aanpassing moet ik dus 2 keer doen, 1 in de bovenste en 1 in de onderste. Dit moet simpeler kunnen dacht ik zo. De gehele select in een variable. Dus met een hoop DECLARE, SET en een partij komma's, quote's etc etc in de weer geweest en ... 0 resultaat: MSMS vreet het niet.
Hoe vlieg ik dit aan??
SQL: query.txt
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
| ------------------------------ -- This script is used to block and anonimyze useraccounts after 18 months of inactivity (GDPR). -- The field [LastActiveDate] is used for this and this field is active since 04-03-2020. -- Therefore, the script itself is active sinds 04-03-2020 + 18 months = 04-09-2021. -- First, it cleans the UserRevision table with the inactive users. -- Afterwards the useraccount itself is being blocked. -- -- Be aware that all admin accounts and accounts from TEST countries are ignored. -- ------------------------------ UPDATE [DP].[dbo].[UserRevisions] SET [Name] = 'Anonymized' ,[Activated] = 0 ,[EmailAddress] = CAST([UserID] as varchar) + '@Anonymized.com' ,[CompanyName] = 'Anonymized' ,[CompanyAddress] = 'Anonymized' ,[CompanyZipCode] = 'Anonymized' ,[CompanyCity] = 'Anonymized' ,[CompanyCountry] = NULL ,[CompanyPhoneNumber] = 0 ,[CompanyEmailAddress] = 'Anonymized' ,[] = 0 ,[PhoneNumber] = 0 ,[JobFunction] = 'Anonymized' ,[LastModifiedByUserId] = 30480 ,[WorkflowState] = 'Deactivated' WHERE [UserID] IN ( SELECT DISTINCT U.[UserId] FROM [DP].[dbo].[Users] U INNER JOIN [DP].[dbo].[TsCountries] C ON U.[TsCountryId] = C.[TsCountryId] INNER JOIN [dbo].[UserAccountGroups] AG ON U.[UserId] = AG.[UserId] WHERE U.[WorkflowState] ='Active' AND -- All active users in state U.[Activated] = 1 AND -- All active users in enabled state U.[Name] NOT LIKE '%admin%' AND -- Excluding names with "Admin" in it AG.AccountGroupId <> 32 AND -- No Account Group "Administrators" AG.AccountGroupId <> 57 AND -- No Account Group "Klantnaam Specificiation" AG.AccountGroupId <> 52 AND -- No Account Group "Klantnaam Support" AG.AccountGroupId <> 34 AND -- No Account Group "User Administrator" U.[LastActiveDate] < DATEADD(MONTH,-18,GETDATE()) AND C.[TsCountryId] <> 124 AND C.[TestCountry] = 0 ) UPDATE [DP].[dbo].[Users] SET [Name] = 'Anonymized', [Activated] = 0, [LoginName] = NULL, [EmailAddress] = CAST([UserID] as varchar) + '@Anonymized.com', [CompanyName] = NULL, [CompanyAddress] = NULL, [CompanyZipCode] = NULL, [CompanyCity] = NULL, [CompanyCountry] = NULL, [CompanyPhoneNumber] = NULL, [CompanyEmailAddress] = 'Anonymized', [OldEmailAddress] = CAST([UserID] as varchar) + '@Anonymized.com', [] = NULL, [PhoneNumber] = NULL, [JobFunction] = NULL, [UserRoleId] = 1, [LastModifiedByUserId] = 30480, [SecurityLastModified] = GETDATE(), [WorkflowState] = 'Deactivated', [MobilePhoneNumber] = 1 WHERE [UserID] IN ( SELECT DISTINCT U.[UserId] FROM [DP].[dbo].[Users] U INNER JOIN [DP].[dbo].[TsCountries] C ON U.[TsCountryId] = C.[TsCountryId] INNER JOIN [dbo].[UserAccountGroups] AG ON U.[UserId] = AG.[UserId] WHERE U.[WorkflowState] ='Active' AND -- All active users in state U.[Activated] = 1 AND -- All active users in enabled state U.[Name] NOT LIKE '%admin%' AND -- Excluding names with "Admin" in it AG.AccountGroupId <> 32 AND -- No Account Group "Administrators" AG.AccountGroupId <> 57 AND -- No Account Group "Klantnaam Specificiation" AG.AccountGroupId <> 52 AND -- No Account Group "Klantnaam Support" AG.AccountGroupId <> 34 AND -- No Account Group "User Administrator" U.[LastActiveDate] < DATEADD(MONTH,-18,GETDATE()) AND C.[TsCountryId] <> 124 AND C.[TestCountry] = 0 ) |