-
Notifications
You must be signed in to change notification settings - Fork 1
/
updatedbtoV2.sql
84 lines (65 loc) · 2.32 KB
/
updatedbtoV2.sql
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
use dabtrial_com_participantdata
go
sp_RENAME 'Users.[ProfessionalRoleId]' , 'ProfessionalRole', 'COLUMN'
go
Alter table Users
alter column ProfessionalRole Int Not Null
go
sp_RENAME 'AdverseEvents.[AdverseEventId]' , 'Id', 'COLUMN'
go
Alter Table ParticipantDeaths
add ReportingUserId int not null
go
ALTER TABLE [dbo].[ParticipantDeaths] WITH CHECK ADD CONSTRAINT [FK_dbo.ParticipantDeaths_dbo.Users_UserId] FOREIGN KEY([ReportingUserId])
REFERENCES [dbo].[Users] ([UserId])
ON DELETE NO ACTION
GO
ALTER TABLE [dbo].[ParticipantDeaths] CHECK CONSTRAINT [FK_dbo.ParticipantDeaths_dbo.Users_UserId]
GO
Alter Table ParticipantDeaths
add ReportingTimeLocal DateTime not null
go
sp_RENAME 'ParticipantDeaths.[Time]' , 'EventTime', 'COLUMN'
go
Alter Table ParticipantWithdrawals
add ReportingUserId int null
go
ALTER TABLE [dbo].[ParticipantWithdrawals] WITH CHECK ADD CONSTRAINT [FK_dbo.ParticipantWithdrawals_dbo.Users_UserId] FOREIGN KEY([ReportingUserId])
REFERENCES [dbo].[Users] ([UserId])
ON DELETE NO ACTION
GO
ALTER TABLE [dbo].[ParticipantWithdrawals] CHECK CONSTRAINT [FK_dbo.ParticipantWithdrawals_dbo.Users_UserId]
GO
Alter Table ParticipantWithdrawals
add ReportingTimeLocal DateTime null
go
update ParticipantWithdrawals
set ReportingUserId = w.UserId, ReportingTimeLocal = w.reportingTimeLocal
from
(select dateadd(hh, case when (a.EventDateUTC < '2014-4-6 00:00:00') then 11
else 10
end, a.EventDateUTC) reportingTimeLocal, u.UserId,
cast(SUBSTRING(
a.NewValue,
CHARINDEX('"', a.NewValue) + 1,
LEN(a.NewValue) - CHARINDEX('"', a.NewValue) - CHARINDEX('"', REVERSE(a.NewValue))
) as int) participantId
FROM [dabtrial_com_participantdata].[dbo].[AuditLogEntries] a
inner join dbo.Users u on a.UserName = u.UserName
where a.TableName='ParticipantWithdrawal' and a.EventType='C') w
inner join ParticipantWithdrawals on w.participantId = ParticipantWithdrawals.Id
go
Alter Table ParticipantWithdrawals
alter column ReportingUserId int not null
go
Alter Table ParticipantWithdrawals
alter column ReportingTimeLocal DateTime not null
go
sp_RENAME 'ParticipantWithdrawals.[Time]' , 'EventTime', 'COLUMN'
go
sp_RENAME 'ParticipantWithdrawals.[Reason]' , 'Details', 'COLUMN'
go
sp_RENAME 'ProtocolViolations.[ViolationId]' , 'Id', 'COLUMN'
go
sp_RENAME 'ProtocolViolations.[TimeOfViolation]' , 'EventTime', 'COLUMN'
go