比较两行数据之间的值并仅显示不同的列

发布于 2024-09-30 00:32:31 字数 1873 浏览 10 评论 0原文

在以前的应用程序版本中,我们使用特定字段作为主键,但由于该字段可能代表不同系统中的不同身份,因此我们将其设为非重要字段(即不是主键或复合主键的一部分),但是因为我们没有另一个系统,但用户仍然使用该字段作为主要的识别方法。

问题在于审核...以前,我使用单个表对数据库进行所有审核,使用浮动的通用触发器使用新值旧值模式转储数据。除了一件事之外,这仍然可以正常工作。我已将联系信息移至与原始表的新主键绑定的单独表中。因此,当进行更改时,审核日志中会显示不熟悉且未使用的主键,而不是现在微不足道的foreignSystemID...

我转而采用一对一复制方法进行审核,以便对任何表的任何更改现在都写入镜像在不同的模式中。问题归结为向用户显示更改。他们习惯于查看只显示特定医生更改值的报告...

我的问题是使用 sql 查询和 Crystal 报告,如何只显示审计表中行之间更改的列值。我已经查看了枢纽命令,但我认为这对我没有真正的帮助。我还查看了脚本中的代码,该代码比较列并确定它们是否不同并将它们写入表中。

我真的在沙子里旋转,这是我需要解决的关键问题。预先感谢您的任何帮助...

我们已经足够早地投入生产,如果需要,我可以更改我的变更跟踪方法,但需要尽快。谢谢

编辑:

我的老板和我已经在这方面做了一些工作,这就是我们开始的......我想获得进一步的意见和选择......以及......谢谢......

CREATE TABLE #TEMP (
    DoctorsID bigint, 
    TableName varchar(50), 
    FieldName varchar(50), 
    CurrentFieldValue varchar(255), 
    PreviousFieldValue varchar(255), 
    PreviousValueDate datetime 
    )

DECLARE @sql varchar(MAX)

SELECT 
    @sql = COALESCE(@sql,'') + 
    CAST(
        'INSERT INTO #TEMP ' + 
            'SELECT ' + 
                'o.DoctorsID, ' + 
                '''' + TABLE_NAME + ''' ,' + 
                '''' + COLUMN_NAME + ''',' + 
                'o.' + COLUMN_NAME + ',' +
                'a.' + COLUMN_NAME + ',' +
                'a.AuditDate' +
            ' FROM ' + 
                'dbo.DoctorLicenses o ' + 
                    'INNER JOIN Audit.DoctorLicenses a ON ' + 
                        'o.DoctorsID = a.DoctorsID ' + 
            'WHERE ' + 
                'AuditDate BETWEEN ''10/01/2010'' AND ''10/31/2010'' AND ' + 
                'o.' + COLUMN_NAME + ' <> a.' + COLUMN_NAME + 
            ';'     
    AS varchar(MAX))
FROM 
    INFORMATION_SCHEMA.COLUMNS AS [Fields]      
WHERE 
    TABLE_SCHEMA = 'dbo' AND 
    TABLE_NAME = 'DoctorLicenses' 

PRINT @sql

EXEC(@sql)

SELECT * FROM #TEMP

DROP TABLE #TEMP

In a previous application version we were using a particular field for a primary key, but because the field may represent different identities across various systems we have made it a non significant field(ie not a primary key or part of a composite primary) however since we dont have another system yet users still use that field as a primary method of identification.

The problem is with auditing...previously I used a single table to do all audits for the database dumping the data with a newvalue oldvalue schema using the generic trigger that is floating around. This could still work fine except for one thing. I have moved contactinformation into a separate table that is tied to the new primary key of the original table. So when changes are made the unfamiliar and unused primary key shows in the auditlog instead of the now insignificant foreignSystemID...

I moved to doing a one to one copy method of auditing so that any changes to any table are now written to a mirror image in a different schema. The problem comes down to showing changes to the users. They are used to seeing a report that shows only the changed values for a particular doctor...

My question would be using sql queries and Crystal reports, how could I show only the changed column values between rows in my audit tables. I have looked at the pivot command, but I dont think thats really going to help me. I had also looked at the code within the script that compares the columns and determines if they are different and writes them to the table.

im really spinning in the sand here and this is a critical issue for me to solve. Thanks in advance for ANY help...

we are early enough into production that I could change my changetracking method if need be, but it needs to be soon. thanks

EDIT:

My boss and I have worked on this a bit and this is what we have started with...I would like to get further opinions and options...as well...thanks..

CREATE TABLE #TEMP (
    DoctorsID bigint, 
    TableName varchar(50), 
    FieldName varchar(50), 
    CurrentFieldValue varchar(255), 
    PreviousFieldValue varchar(255), 
    PreviousValueDate datetime 
    )

DECLARE @sql varchar(MAX)

SELECT 
    @sql = COALESCE(@sql,'') + 
    CAST(
        'INSERT INTO #TEMP ' + 
            'SELECT ' + 
                'o.DoctorsID, ' + 
                '''' + TABLE_NAME + ''' ,' + 
                '''' + COLUMN_NAME + ''',' + 
                'o.' + COLUMN_NAME + ',' +
                'a.' + COLUMN_NAME + ',' +
                'a.AuditDate' +
            ' FROM ' + 
                'dbo.DoctorLicenses o ' + 
                    'INNER JOIN Audit.DoctorLicenses a ON ' + 
                        'o.DoctorsID = a.DoctorsID ' + 
            'WHERE ' + 
                'AuditDate BETWEEN ''10/01/2010'' AND ''10/31/2010'' AND ' + 
                'o.' + COLUMN_NAME + ' <> a.' + COLUMN_NAME + 
            ';'     
    AS varchar(MAX))
FROM 
    INFORMATION_SCHEMA.COLUMNS AS [Fields]      
WHERE 
    TABLE_SCHEMA = 'dbo' AND 
    TABLE_NAME = 'DoctorLicenses' 

PRINT @sql

EXEC(@sql)

SELECT * FROM #TEMP

DROP TABLE #TEMP

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单气质女生网名 2024-10-07 00:32:31

在我看来,似乎存在设计问题,但我目前很难想象您的设计是什么。您能否更具体地说明您的表格目前的情况以及您尝试生成报告的数据?

另外,在谈论审核“更改的值”时,您如何跟踪更改的内容?

It sounds to me like there is a design issue, but I have a hard time envisioning what your design is at the moment. Can you be more specific on what your tables look like at the moment and what data you're trying to generate the report(s) on?

Also, when talking about auditing "the changed values", how do you keep track of what's been changed?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文