Sql Trigger Error解释?

时间:2015-12-04 21:03:20

标签: sql triggers compiler-errors

我一直在研究这个触发器并且似乎被困住了。我觉得我的逻辑有点正确但是当我运行代码时,我收到此错误消息:

Msg 213, Level 16, State 1, Procedure Message_Student_ForInsert, Line 9
Column name or number of supplied values does not match table definition.

这是我的代码:

Create Trigger Message_Student_ForInsert
ON CourseEnrolled

For Insert

AS
Begin
 Declare @Id int
 Select @Id = StudentID from inserted

 insert into CourseAudit
 Values ('New Student with Id = ' + Cast(@Id as nvarchar(10)) + ' is added at ' + cast(GetDate() as nvarchar(30)))
END

我还创建了一个表名CourseAudit,试图尝试解决这个错误。我认为错误意味着我在表格中缺少值 - 因为我还没有插入任何值。任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

Your INSERT statement is incorrect. Learn more here; INSERT()

Based on your code, one assumes the table [CourseAudit] exists and has a nvarchar() column in it.

You need to specify the column name for the insert.

If you omit the column name, then you should suply a value for ALL columns, in the order on which they appear in the table definition.

Example:

insert into CourseAudit (SomeCulumnNameHere)
Values ('New Student with Id = ' + Cast(@Id as nvarchar(10)) 
       + ' is added at ' + cast(GetDate() as nvarchar(30)))