我创建了两个表。首先是杂志
CREATE TABLE [dbo].[Magzine](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](350) NULL,
[MagzineType] [int] NOT NULL,
[TotalPages] [int] NOT NULL,
[DateCreated] [datetime] NOT NULL,
[Active] [bit] NOT NULL, CONSTRAINT [PK_tblMagzine] 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
它有一个列名 TotalPages 保存杂志中的总页数
这是 MagzinePage 表。
CREATE TABLE [dbo].[MagzinePage](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MagzineID] [int] NOT NULL,
[Title] [nvarchar](255) NULL,
[SubTitle] [nvarchar](255) NULL,
[Photo] [nvarchar](255) NULL,
[Description] [nvarchar](2000) NULL,
[DateCreated] [datetime] NOT NULL,
[CreatedBy] [nvarchar](25) NOT NULL,
[Active] [bit] NOT NULL,
[IsMapped] [bit] NULL, CONSTRAINT [PK_tblPage] 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
我已经创建了一个在 magzinePage 表中插入数据的触发器,它可以根据 ID 处理插入到表中的记录总数这里是触发器
Alter TRIGGER trgAfterInsert ON [dbo].[MagzinePage]
FOR INSERT
AS
Declare @MagzineID int;
Declare @TotalPages int;
Set @MagzineID= (Select Top 1 MagzineID From MagzinePage Order By ID Desc)
Set @TotalPages = (Select Count(MagzineID) from MagzinePage Where MagzineID=@MagzineID Group By MagzinePage.MagzineID)
Update Magzine Set TotalPages=@TotalPages Where ID= @MagzineID
PRINT 'AFTER INSERT trigger fired.'
GO
现在我想创建一个触发器,目的是在删除 magzinePage 表中的记录时帮助减少 Magzine 表中的总页数。
请帮我想办法解决这个问题。谢谢。