3

一个学生要去一所学校,他将支付每月的费用。该值将是(FatherSalary + MotherSalary)*0.05

我昨天才开始研究触发器,我做了一个但我得到了错误

消息 515,级别 16,状态 2,过程 TR_Calc_Value,第 25 行
无法将值 NULL 插入到列“IDStudent”、表“HW32.dbo.Enrollment”中;列不允许空值。插入失败。

当我在表中插入值时Enrollment。任何帮助如何解决这个问题?

USE master;

IF DB_ID (N'HW32') IS NOT NULL
   DROP DATABASE HW32;

CREATE DATABASE HW32;

USE HW32
CREATE TABLE Family(
  IDFamily int IDENTITY(1,1),
  FirstName nchar(20) NOT NULL,
  LastName nchar(20) NOT NULL,
  Gender nchar(1)  NOT NULL,
  Salary money,
  CONSTRAINT PK_Family PRIMARY KEY(IDFamily),
  CONSTRAINT CK_Family_Gender CHECK (Gender IN ('M','F'))
) 

CREATE TABLE Student(
  IDStudent int IDENTITY(1,1),
  FirstName nchar(20) NOT NULL,
  LastName nchar(20) NOT NULL,
  CONSTRAINT PK_Student PRIMARY KEY(IDStudent)
)

CREATE TABLE Filiation(
  IDStudent int,
  IDFamily int,
  Filiation nchar(20) NOT NULL,
  CONSTRAINT FK_Filiation_IDStudent FOREIGN KEY (IDStudent)
  REFERENCES Student(IDStudent),
  CONSTRAINT FK_Filiation_IDFamily FOREIGN KEY (IDFamily)
  REFERENCES Family(IDFamily),
  CONSTRAINT PK_Filiation PRIMARY KEY(IDStudent,IDFamily)
)

CREATE TABLE Enrollment(
  IDEnrollment int IDENTITY(1,1),
  IDStudent int NOT NULL,
  Status nchar(20) NOT NULL,
  MonthlyPayment money,
  CONSTRAINT PK_Enrollment PRIMARY KEY(IDStudent), 
  CONSTRAINT FK_Enrollment_IDStudent FOREIGN KEY (IDStudent)
  REFERENCES Student(IDStudent),
  CONSTRAINT CK_Enrollment_Status CHECK(Status IN('Acepted','Rejected')),
  CONSTRAINT UC_Enrollment UNIQUE (IDEnrollment)
)

USE HW32
GO
CREATE TRIGGER TR_Calc_Value 
ON Enrollment 
AFTER INSERT, UPDATE AS
    DECLARE @monthlyPayment money, @sFather money, @sMother money
BEGIN
    SET @sFather = (SELECT FAM.Salary
            FROM Family FAM 
            LEFT JOIN Filiation F ON F.IDFamily = FAM.IDFamily
            LEFT JOIN inserted I ON I.IDStudent = F.IDStudent
            WHERE F.IDStudent = I.IDStudent AND FAM.Gender = 'M')

    SET @sMother = (SELECT FAM.Salary 
            FROM Family FAM 
            LEFT JOIN Filiation F ON F.IDFamily = FAM.IDFamily
            LEFT JOIN inserted I ON I.IDStudent = F.IDStudent
            WHERE F.IDStudent = I.IDStudent AND FAM.Gender = 'F')

    SET @monthlyPayment = ((@sFather + @sMother) * 0.05)

    INSERT INTO Enrollment (MonthlyPayment) VALUES (@monthlyPayment)
END
GO

USE HW32
INSERT INTO Family VALUES('John', 'Smith', 'M', 800)
INSERT INTO Family VALUES('Anna', 'Smith', 'F', 800)

INSERT INTO Student VALUES('Carl', 'Smith')

INSERT INTO Filiation VALUES(1, 1, 'Father')
INSERT INTO Filiation VALUES(1, 2, 'Mother')

INSERT INTO Enrollment (IDStudent, Status) VALUES(1, 'Accepted')
4

1 回答 1

6

您不想在触发器中插入一行 - 这些行已经添加。相反,您想要执行UPDATE

CREATE TRIGGER TR_Calc_Value ON Enrollment AFTER INSERT, UPDATE AS
BEGIN
   UPDATE E 
   set MonthlyPayment = (

      select 0.05 * SUM(Salary) 
      from Family FAM 
      inner join Filiation F 
      on FAM.IDFamily= F.IDFamily 
      where F.IDStudent = I.IDStudent
      )

   from Enrollment E
   inner join inserted I
   on E.IDEnrollment=  I.IDEnrollment

注意事项

  • 以上正确处理inserted包含多行
  • 我不区分父亲和母亲(因为他们似乎被同等对待)
  • 如果学生有多个父亲和母亲,他们都会被包括在内。这是否正确是有争议的,并且可能无法使用更好的约束 - 但最初的触发器也不能很好地应对这种情况。

根据我的评论,如果始终可以从其他表中重新计算此信息,我还建议不要存储此信息(不确定在这种情况下是否正确,但如果它应该反映其他表,则需要更多触发器保持一切一致。

并且作为 per-marc 的已删除答案,在进行插入时始终指定列列表也是一个好习惯。这不是此特定错误的原因,但有助于更好地记录您的意图,并有助于通过目视检查快速消除可能的错误。

于 2012-12-02T12:26:41.343 回答