[英]Pull records for Audit Trail
I am implementing audit trail in my application. 我正在我的应用程序中实现审计跟踪。 I followed this and this created History table the tables that only needs to be audited.
我遵循了这个 , 这创建了历史表,只需要审计的表。
I have a following tables to store the Patient information. 我有一个下表来存储患者信息。
TenantId represent each tenant data, since its a multitenant application TenantId代表每个租户数据,因为它是一个多租户应用程序
Person
and Patient
table Person
与Patient
表
Person 人
Id, TenantId, FirstName, LastName, DOB, Mobile, Email,AddedBy,UpdatedBy, IsDeleted Id,TenantId,FirstName,LastName,DOB,Mobile,Email,AddedBy,UpdatedBy,IsDeleted
Patient 患者
Id, PatientIDentifier, IsOP, CanSendSMS, AddedBy, UpdatedBy, IsDeleted Id,PatientIDentifier,IsOP,CanSendSMS,AddedBy,UpdatedBy,IsDeleted
Also another table 还有另一张表
Appointment 约定
Id, PatientId, AppointmentDate, DoctorId, Price, AddedBy, UpdatedBy Id,PatientId,AppointmentDate,DoctorId,Price,AddedBy,UpdatedBy
AUDIT HISTORY TABLE STRUCTURE 审计历史表结构
PersonHistory PersonHistory
AuditId,Id, FirstName, LastName, DOB, AddedBy, UpdatedBy, AuditUserId, AuditDate, Action AuditId,Id,FirstName,LastName,DOB,AddedBy,UpdatedBy,AuditUserId,AuditDate,Action
Here Action
represents A(ADD)/U(update)/D(delete)
这里
Action
表示A(ADD)/U(update)/D(delete)
The same structure has been created for Patient, Appointment
为
Patient, Appointment
创建了相同的结构
Now I got triggers to insert into History table whenever add/delete/update happens. 现在,每当添加/删除/更新发生时,我都会在历史表中插入触发器。
Now I got the data available in the audit history table. 现在,我在审计历史记录表中获得了可用的数据。
I have to write a query for two requirements. 我必须为两个要求编写查询。
Get all the records for a perticular patient. 获取特定患者的所有记录。 I need to pull all the records from PatientHostory, AppointmentHistory, PersonHistory by using a
PatientId
. 我需要使用
PatientId
从PatientHostory,AppointmentHistory,PersonHistory中提取所有记录。 How do we write SQL that takes more records from same table for given id? 对于给定的id,我们如何编写从同一个表中获取更多记录的SQL?
UNION or JOIN
? UNION or JOIN
?
I need to take all the records from all the HistoryTable for the supplied AuditUser Id
. 我需要从
all the HistoryTable for the supplied AuditUser Id
所有记录。
How can i write a query for this? 我该如何为此编写查询?
You would UNION
the log data, also consider adding a HistoryType
column to indicate which table it refers too 您可以
UNION
日志数据,还可以考虑添加HistoryType
列以指示它所引用的表
SELECT AuditId, Id, FirstName, LastName, DOB, AddedBy, UpdatedBy,
AuditUserId, AuditDate, Action, 'Person' HistoryType
FROM PersonHistory ph
JOIN Person p ON ph.id = p.id
UNION ALL
SELECT AuditId,Id, FirstName, LastName, DOB, AddedBy, UpdatedBy,
AuditUserId, AuditDate, Action, 'Patient' HistoryType
FROM PatientHistory
UNION ALL
SELECT AuditId,Id, FirstName, LastName, DOB, AddedBy, UpdatedBy,
AuditUserId, AuditDate, Action, 'Appointment' HistoryType
FROM AppointmentHistory
Turning the above into a VIEW
would allow you to further query the data more easily 将上述内容转换为
VIEW
可以让您更轻松地进一步查询数据
SELECT * FROM vAuditHistory
WHERE AuditUserId = 1234
If you need to get information from the 'originating' data ie PatientId
s then again using the view you could do something along the lines of 如果您需要从“原始”数据中获取信息,即
PatientId
然后再次使用该视图,您可以执行某些操作。
DECLARE @PatientId VARCHAR(10) = '12345ABCDE'
SELECT h.*, p.PatientId
FROM vAuditHistory h
JOIN Patient pt ON pt.id = h.id
WHERE h.HistoryType = 'Patient'
AND pt.PatientId = @PatientId
UNION ALL
SELECT h.*, a.PatientId
FROM vAuditHistory h
JOIN Appointment a t ON a.id = h.id
WHERE h.HistoryType = 'Appointment'
AND pt.PatientId = @PatientId
or without the view 或没有观点
SELECT h.*, p.PatientId
FROM PatientHistory h
JOIN Patient pt ON pt.id = h.id
WHERE pt.PatientId = @PatientId
UNION ALL
SELECT h.*, a.PatientId
FROM AppointmentHistory h
JOIN Appointment a t ON a.id = h.id
WHERE pt.PatientId = @PatientId
Creating the above as an inline function would probably be useful 将上述内容创建为内联函数可能很有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.