这是学生图书馆项目的一部分。插入语句不会在数据库中插入多于一行
有2个表:alerts
和borrows
。
borrows
包含studentID
,bookID
和date of borrowing
。
alerts
指示哪些学生过期多少本书。
该代码应该为每个过期的学生插入一行,并计算过期的书籍数量。
Est_Return_Date = return_date + 30
insert into dbo.Alert (studentID, AlertCount)
values ((select distinct (studentID)from dbo.Borrows
where Est_Return_Date < GETDATE()
and return_date is null),
(select count(studentID) from dbo.Borrows
where Est_Return_Date < GETDATE()
and return_date is null))
计数将始终返回一个值。而另一个选择可能还会返回一个值,那就是你的问题。你有错误的查询 –
在查询中使用'GetDate()'是追逐一个移动的目标,影响性能,并可能产生好奇的结果。随着日期的变化。在变量中捕获当前日期/时间几乎总是一个更好的主意,然后根据需要使用该值。在存储过程中,这在多个语句中更重要。多次使用'GetDate()'的最常见原因是捕获长时间运行操作的开始和结束时间。 – HABO