创建带输入参数的储存过程,输入姓名,显示是否逾期未还图书,学生的借阅天数超60天,教师的借阅天数超90天则为逾期读者,结果包含读者姓名,读者类别,书名,借阅日期和超期天数
时间: 2024-12-14 13:28:16 浏览: 11
Access应用实例——图书借阅管理系统
5星 · 资源好评率100%
创建一个带输入参数的存储过程通常用于数据库管理系统中,比如MySQL。这个过程可以接收学生的姓名作为参数,检查他们是否有书籍超出借阅期限。以下是描述该存储过程的一般步骤:
```sql
DELIMITER //
CREATE PROCEDURE CheckOverdueBooks(IN studentName VARCHAR(100), OUT readerCategory ENUM('学生', '教师'), IN bookTitle VARCHAR(100), DATE_IN_DATE DATE, OUT overdueDays INT)
BEGIN
DECLARE isStudent BOOLEAN DEFAULT TRUE; -- 默认为学生读者
SET readerCategory = '学生'; -- 初始值
-- 检查学生借阅记录
SELECT
CASE WHEN DATEDIFF(CURRENT_DATE, borrow_date) > 60 THEN '超过'
ELSE NULL
END AS days_overdue
INTO @days_overdue
FROM borrow_records
WHERE borrower_name = studentName AND book_title = bookTitle;
IF @days_overdue IS NOT NULL THEN
SET overdueDays = @days_overdue;
IF @days_overdue > 0 THEN
SET readerCategory = '学生';
ELSE
SET isStudent = FALSE;
END IF;
END IF;
-- 如果不是学生,检查教师记录
IF NOT isStudent THEN
SELECT
CASE WHEN DATEDIFF(CURRENT_DATE, borrow_date) > 90 THEN '超过'
ELSE NULL
END AS days_overdue
INTO @days_overdue
FROM borrow_records
WHERE teacher_name = studentName AND book_title = bookTitle;
IF @days_overdue IS NOT NULL THEN
SET overdueDays = @days_overdue;
SET readerCategory = '教师';
END IF;
END IF;
-- 输出结果
IF readerCategory = '学生' THEN
SELECT studentName, readerCategory, bookTitle, DATE_IN_DATE, overdueDays;
ELSEIF readerCategory = '教师' THEN
SELECT teacherName, readerCategory, bookTitle, DATE_IN_DATE, overdueDays;
END IF;
END //
DELIMITER ;
```
阅读全文