我在两个不同的数据库Lab15DB_1和Lab15DB_2中有两个表。
父表是作者,子表是Book。
他们是:
CREATE TABLE Author
(
AuthorId INT NOT NULL PRIMARY KEY,
AuthorName NVARCHAR(50) NOT NULL,
AuthorSurname NVARCHAR(50) NOT NULL,
)
CREATE TABLE Book
(
BookId INT NOT NULL PRIMARY KEY,
BookName NVARCHAR(50) NOT NULL,
BookType NVARCHAR(50) NOT NULL CHECK(BookType IN ('Science', 'Hobby', 'Cooking', 'Fishing', 'Nature', 'Favorites')),
PublishYear INT NOT NULL,
AuthorId INT NOT NULL,
Price MONEY CHECK (PRICE > 0.0)
)
我想通过触发器创建级联更新。
这是尝试在Author
表上创建级联更新的触发器:
CREATE TRIGGER UpdateAuthorTrigger
ON Author
FOR UPDATE
AS
DECLARE @intRowCount int
SELECT @intRowCount = @@ROWCOUNT
IF @intRowCount > 1
BEGIN
IF UPDATE(AuthorId)
BEGIN
UPDATE Author
SET AuthorId = AuthorId
/* some code for update Book*/
END
END
ELSE
IF @intRowCount = 1
BEGIN
IF UPDATE(AuthorId)
BEGIN
UPDATE Author
SET Author.AuthorId = (SELECT AuthorId FROM INSERTED)
FROM Author
INNER JOIN DELETED ON Author.AuthorId = DELETED.AuthorId
UPDATE Lab15DB_1.dbo.Book
SET Lab15DB_1.dbo.Book.AuthorId = (SELECT AuthorId FROM INSERTED)
FROM Lab15DB_1.dbo.Book
INNER JOIN DELETED ON Lab15DB_1.dbo.Book.AuthorId = DELETED.AuthorId
END
END
GO
案例@intRowCount = 1
没有问题,但案例@intRowCount > 1
遇到了问题。
你能给我一些建议吗?