есть таблица из 2 колонок id и name. необходим тригер который бы при изменение записей в этой таблице заносил бы значения до и после обновления в другую таблицу.
Нужен тригер на обноление таблицы
1
Спросил
Новые ответы
1
1 таблица
CREATE TABLE [dbo].[test1](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
CONSTRAINT [PK_test1] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
2 таблица
CREATE TABLE [dbo].[test2](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[RelId] [int] NOT NULL,
CONSTRAINT [PK_test2] PRIMARY KEY CLUSTERED
(
[Id] ASC
) WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[test2]
WITH CHECK ADD CONSTRAINT [FK_test2_test1] FOREIGN KEY([RelId])
REFERENCES [dbo].[test1] ([Id])
GO
Сам триггер
CREATE TRIGGER test1_insertupdate
ON dbo.test1
AFTER INSERT, UPDATE
AS
DECLARE @Id int, @Name nvarchar(50)
SELECT @Id = (SELECT Id FROM inserted)
SELECT @Name =(SELECT Name FROM inserted)
BEGIN
INSERT INTO dbo.test2 (RelId, Name) VALUES(@Id,@Name)
END
GO