自动投入

时间:2017-07-21 04:18:56

标签: sql sql-server

我有课程安排表:

ID      Lesson    date        time1   time2    Teacher
-------------------------------------------------------
M001    Math     01-08-2017   17:00   19:00
M002    English  01-08-2017   13:00   15:00
M003    Design   01-08-2017   17:00   19:00
M001    Math     02-08-2017   13:00   15:00
M002    English  02-08-2017   17:00   19:00
M003    Design   02-08-2017   17:00   19:00

我也有优先级的教师表

ID      Name     Lesson    Priority
-----------------------------------
001     John     Math         1
002     Mike     Math         2
003     Clara    Math         3
004     Noah     Design       1
005     Jack     Design       2
006     Kath     English      1
007     Steve    English      2

确定谁将按照时间表进行教学的优先级,但如果时间表已经将教师的第二位教师放在时间表上,等等。 我仍然困惑如何使它工作..请帮助..

...谢谢

1 个答案:

答案 0 :(得分:0)

您想尝试使用存储过程插入

看起来像这样

CREATE STORED_PROCEDURE SP_INSERT_TEACHER

@TeachID int = 0,
@TeachName nvarchar(max), 
@TeachLesson nvarchar(max), 
@TeachPriority int

AS
BEGIN

SET NOCOUNT ON;

IF dbo.Teacher.Lesson = @TeachLesson AND dbo.Teacher.Priority = @TeachPriority
@TeachPriority + 1



INSERT INTO dbo.Teacher(ID, Name, Lesson, Priority) 
values(@TeachID, @TeachName, @TeachLesson, @TeachPriority)

END

这基本上会检查现有记录,如果记录已经与课程相匹配,则优先考虑将插入的教师推回1

确保然后始终通过SP插入