(1)创建名为teachernfo1的存储过程。要求:存储过程teachernfo1有3个参数。输入参数为teacherid和type,输出参数为info。满足:根据编号(teacherid)来查询teachernfo表中的记录。如果type的值为1时,将姓名(name)传给输出参数info;如果type的值为2时,将年龄传给输出参数info;如果type为其他值,则返回字符串“Error”。 (2)调用存储过程,参数值teacher id为2,type为1。 (3)使用DROP PRODECURE语句来删除存储过程 (4)创建名为teacherinfo2的存储函数。要求:存储过程teacherinfo2有两个参数:teacher id和type。满足:根据编号(teacher id)来查询teacher表中的记录。如果type的值是1时,则返回姓名(name)值;如果type的值是2时,则返回年龄;如果type为其他值,则返回字符串“Error”。 (5)使用SELECT语句调用teacherinfo2存储函数。
时间: 2023-06-18 22:08:36 浏览: 219
(1) 创建存储过程:
```mysql
CREATE PROCEDURE teachernfo1
(IN teacherid INT, IN type INT, OUT info VARCHAR(50))
BEGIN
IF type = 1 THEN
SELECT name INTO info FROM teachernfo WHERE id = teacherid;
ELSEIF type = 2 THEN
SELECT age INTO info FROM teachernfo WHERE id = teacherid;
ELSE
SET info = 'Error';
END IF;
END;
```
(2) 调用存储过程:
```mysql
SET @info = '';
CALL teachernfo1(2, 1, @info);
SELECT @info;
```
(3) 删除存储过程:
```mysql
DROP PROCEDURE teachernfo1;
```
(4) 创建存储函数:
```mysql
CREATE FUNCTION teacherinfo2
(teacherid INT, type INT)
RETURNS VARCHAR(50)
BEGIN
DECLARE info VARCHAR(50);
IF type = 1 THEN
SELECT name INTO info FROM teacher WHERE id = teacherid;
ELSEIF type = 2 THEN
SELECT age INTO info FROM teacher WHERE id = teacherid;
ELSE
SET info = 'Error';
END IF;
RETURN info;
END;
```
(5) 调用存储函数:
```mysql
SELECT teacherinfo2(2, 1);
```
阅读全文