如何在Azure SQL/SQL Server中从数据库系统版本版的表中跟踪已删除的行?

发布于 2025-01-18 05:12:03 字数 943 浏览 0 评论 0原文

我有系统版的客户表,需要跟踪admin用户是否从表中删除了行('tesla')。

客户历史表确实会为“特斯拉”获得新的行,但并未明确判断行已更新或删除。

我需要建议来创建SELECT/INSERT SQL查询,该查询比较了客户和客户历史,以检查已删除了哪个行,并将行('tesla')插入客户的表格。

我有系统版本的表:

CREATE TABLE [sales].[Customers](
    [Customer_PK] [int] IDENTITY(1,1) NOT NULL,
    [Customer_Id] [smallint] NULL,
    [Customer_Name] [nvarchar](150) NULL,

) ON [PRIMARY]
GO

它具有值'1','100','tesla'和'2','200','ford'。 管理用户将删除“特斯拉”行。

我有历史版本的表:

CREATE TABLE [sales].[CustomersHistory](
    [Customer_PK] [int] NOT NULL,
    [Customer_Id] [smallint] NULL,
    [Customer_Name] [nvarchar](150) NULL,

) ON [PRIMARY]
GO

我有第三个表,我想在其中插入已从客户表中删除的行('tesla')。

CREATE TABLE [sales].[CustomersDeleted](
    [Customer_PK] [int] NOT NULL,
    [Customer_Id] [smallint] NULL,
    [Customer_Name] [nvarchar](150) NULL,
    [Deleted_time] [datetime2](7) NULL

) ON [PRIMARY]
GO

I have system versioned Customer table and need to track if admin user deleted the row ('Tesla') from the table.

CustomerHistory table do get new row for 'Tesla', but it does not explicitly tell if row has been updated or deleted.

I would need advice to create SELECT/INSERT SQL query, which compared Customers and CustomerHistory to check which row has been deleted and insert the row ('Tesla') to CustomersDeleted table.

I have System versioned table:

CREATE TABLE [sales].[Customers](
    [Customer_PK] [int] IDENTITY(1,1) NOT NULL,
    [Customer_Id] [smallint] NULL,
    [Customer_Name] [nvarchar](150) NULL,

) ON [PRIMARY]
GO

It has value '1','100','Tesla' and '2','200','Ford'.
Admin user will deleted 'Tesla' row.

I have History versioned table:

CREATE TABLE [sales].[CustomersHistory](
    [Customer_PK] [int] NOT NULL,
    [Customer_Id] [smallint] NULL,
    [Customer_Name] [nvarchar](150) NULL,

) ON [PRIMARY]
GO

I have third table where I would like to insert rows ('Tesla') that has been removed from Customer table.

CREATE TABLE [sales].[CustomersDeleted](
    [Customer_PK] [int] NOT NULL,
    [Customer_Id] [smallint] NULL,
    [Customer_Name] [nvarchar](150) NULL,
    [Deleted_time] [datetime2](7) NULL

) ON [PRIMARY]
GO

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文