我在Ames.Details
上创建了一个触发器来触发Ames.Sqlclass
,但是当我将数据插入Ames.Details
时,例如'Ken','Math',20,'In',在下面的Sqlclass.Syntax
**中给我显示40分
CREATE SCHEMA Ames
GO
CREATE TABLE Ames.SqlClass
(
Name NVARCHAR(50),
Subject NVARCHAR(50),
Mark INT
)
GO
CREATE TABLE Ames.Details
(
Name NVARCHAR(50),
Subject NVARCHAR(50),
Mark INT,
Status NVARCHAR(20)
)
GO
CREATE TRIGGER SQlDetails
ON Ames.Details
AFTER INSERT
AS
BEGIN
DECLARE @st INT, @Namesql NVARCHAR(50),
@Subject NVARCHAR(50), @pt INT, @Status NVARCHAR(20)
SELECT
@Namesql = i.Name, @Subject = i.subject,
@pt = i.mark, @Status = I.Status
FROM
inserted AS I
IF NOT EXISTS (SELECT * FROM Ames.SqlClass
WHERE @Namesql = Name AND @Subject = Subject)
BEGIN
INSERT INTO Ames.sqlClass (Name, Subject, Mark)
VALUES (@Namesql, @Subject, @pt)
END
SELECT @st = Mark
FROM ames.SqlClass
WHERE @Namesql = Name AND @Subject = Subject
BEGIN
IF @Status = 'IN'
BEGIN
SET @st = @st + @pt
END
IF @Status = 'Out'
BEGIN
SET @st = @st - @pt
END
UPDATE Ames.SqlClass
SET Mark = @st
WHERE Name = @Namesql
END
END