sql - SQL触发器存储过程编译错误

标签 sql stored-procedures compiler-errors

我对什么给我这个错误有疑问:

Msg 547, Level 16, State 0, Procedure AddIntoClass, Line 12
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CourseEnrolled_StudentDemographic". The conflict occurred in database "PK_Final", table "dbo.StudentDemographic", column 'StudentID'.

到目前为止,这是我的代码:
Create Procedure AddIntoClass(@StudentID int, @CourseName nvarchar(30), 

@SectionNumber nvarchar(30), @TimeOfDay nvarchar(30), @Term int)
As 
Begin
    Insert into CourseEnrolled
    Values(@StudentID, @CourseName, @SectionNumber, @TimeOfDay, @Term)
End

EXEC AddIntoClass 2, 'Biology', '1003', '2:00pm', 2

任何帮助将被申请,谢谢!

最佳答案

在插入CourseEnrolled表之前,StudentDemographic表中的学生ID值应为“2”。

您正在将“StudentId”值“2”插入CourseEnrolled表中,该表在“StudentId”上具有对StudentDemographic表的外键引用。

关于sql - SQL触发器存储过程编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34099871/

相关文章:

sql-server - 如果事务由SET XACT_ABORT ON终止,则SET XACT_ABORT OFF

sql - 无法从 PLSQL 表类型批量收集到表中?

mysql - 更新存储过程中的表更新所有行

android - 错误 :Execution failed for task ':app:transformClassesWithJarMergingForDebug' in android + volley libraries

sorting - "nonL value in assignment "错误 C++对链表进行排序

sql - QSqlTableModel 的问题——没有自动更新

mysql - SQL Server : Select rows with dates that correspond to end of year and calculate price

c - 错误 : making pointer from integer in C

sql - SQL Server Management Studio 中是否有 SELECT ... INTO OUTFILE 等效项?

mysql - 将根据另一列中的值忽略具有相同 ID 的记录的 SQL 查询