如果数据是从一个table更新的,数据应该插入另一个table in SQL Server 2017

If the data is updated from a table, the data should be inserted in another table in SQL Server 2017

如果数据是从一个table更新的,数据应该插入到sqlserver2017的另一个table中。我将用这个创建一个 SP

Table 1:dbo.testing_updated

Table 2:dbo.testing_insert

如果使用更新查询在 table 1 中更新了任何数据,则应在 table 2 中插入更新后的列名。

我试过了,

update dbo.testing_updated set name='a' where name='suba';

IF (@@ROWCOUNT > 0)
    BEGIN
        insert into dbo.testing_insert values('1')
    END

仅当任何数据更新时,才应将值插入 table。否则什么都不应该更新。在我的例子中,无论更新

如何,每次都会插入数据

SELECT@@版本;给我 Microsoft SQL Server 2017

我不确定您的 tables 架构,但您可以在 Table 1 上使用触发器来执行此操作,以便在更新时插入到 Table 2 中。这样的东西会起作用。

DROP TABLE IF EXISTS testing_updated
DROP TABLE IF EXISTS testing_insert

CREATE TABLE testing_updated
(
  [NAME] NVARCHAR(50)
)
GO

CREATE TABLE testing_insert
(
  [name] NVARCHAR(50),
  InsertedValue SMALLINT
)
GO

CREATE TRIGGER tblUpdateTrigger ON testing_updated
AFTER UPDATE
AS
BEGIN
  INSERT INTO testing_insert ([name], InsertedValue)
  SELECT T.[name], 1
  FROM  testing_updated T
  INNER JOIN inserted i ON T.[name]=I.[name] 
END
GO

编辑:

根据您的评论,您似乎想要更新多个 table。您可以通过在同一个源 table 上创建多个触发器来实现这一点,这些触发器的条件将插入到单独的 table 中。我不完全清楚您需要的结果是什么,但请参阅下文。我认为这个的一些变化对你有用。

DROP TABLE IF EXISTS testing_updated
DROP TABLE IF EXISTS testing_insert1
DROP TABLE IF EXISTS testing_insert2

CREATE TABLE testing_updated
(
  [NAME] NVARCHAR(50)
)
GO

CREATE TABLE testing_insert1
(
  [name] NVARCHAR(50),
  InsertedValue SMALLINT
)
GO

CREATE TABLE testing_insert2
(
  [name] NVARCHAR(50),
  InsertedValue SMALLINT
)
GO

CREATE TRIGGER tblUpdateTrigger1 ON testing_updated
AFTER UPDATE,INSERT
AS
BEGIN
  INSERT INTO testing_insert1 ([name], InsertedValue)
  SELECT T.[name], 1
  FROM  testing_updated T
  INNER JOIN inserted i ON T.[name]=I.[name] AND I.[NAME]='abc'
END
GO

CREATE TRIGGER tblUpdateTrigger2 ON testing_updated
AFTER UPDATE,INSERT
AS
BEGIN
  INSERT INTO testing_insert2 ([name], InsertedValue)
  SELECT T.[name], 1
  FROM  testing_updated T
  INNER JOIN inserted i ON T.[name]=I.[name] AND I.[NAME]='xyz'
END
GO

INSERT INTO testing_updated VALUES ('abc')

SELECT * FROM testing_updated
SELECT * FROM testing_insert1
SELECT * FROM testing_insert2

INSERT INTO testing_updated VALUES ('xyz')

SELECT * FROM testing_updated
SELECT * FROM testing_insert1
SELECT * FROM testing_insert2

查看时间 tables。他们对每个 update/delete 的记录进行自动备份,您可以相对简单地询问记录在任何给定时间点的情况。

这里有一个(不相关的)示例,说明如何创建这样的 table:

CREATE TABLE [dbo].[TestOrder](
    [TestOrder_ID] [int] IDENTITY(1,1) NOT NULL,
    [TestOrder_OrderNo] [varchar](15) NOT NULL,
    [TestOrder_Details] [nvarchar](max) NOT NULL,
    [TestOrderState_ID] [tinyint] NOT NULL,
    [ValidFromUtc] [datetime2](7) GENERATED ALWAYS AS ROW START NOT NULL,
    [ValidToUtc] [datetime2](7) GENERATED ALWAYS AS ROW END NOT NULL,
 CONSTRAINT [PK_TestOrder] PRIMARY KEY CLUSTERED 
(
    [TestOrder_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = ON) ON [PRIMARY],
    PERIOD FOR SYSTEM_TIME ([ValidFromUtc], [ValidToUtc])
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
WITH
(
SYSTEM_VERSIONING = ON ( HISTORY_TABLE = [dbo].[TestOrder_Archive] )
)