create trigger TR_A_IU on A for INSERT, update as
BEGIN
DECLARE @kmlx NUMERIC(10), @xxrs NUMERIC(10)
SELECT @kmlx = 科目类型 FROM inserted
SELECT @xxrs = isnull(sum(选修人数), 0) FROM A WHERE 科目类型 = @kmlx
IF (SELECT count(1) FROM B WHERE 科目类型 = @kmlx) > 0
UPDATE B SET 选修人数 = @xxrs WHERE 科目类型 = @kmlx
ELSE
INSERT B (科目类型, 选修人数) SELECT @kmlx, @xxrs
IF @@ERROR <> 0 GOTO error
RETURN
error:
raiserror('写B表失败', 16, 1)
end
GO
CREATE trigger [dbo].[Tr_Insert_Table]
on [dbo].[表A]
after insert
As
begin
insert into 表B(有科目类型,选修人数)
(
select 科目类型,1 from inserted
)
end
为A表创建Insert,Update类型的after触发器,然后在触发器中添加insert into b(,,,)values(,,,,);