作业:
创建一个函数func,返回Employees表指定编号的姓名,并将姓名存放到一个变量中,返回姓名的值.调用函数返回010008,102208编号的姓名。
错误代码:
DROP FUNCTION IF EXISTS func
DELIMITER $$
CREATE FUNCTION func(EmplID VARCHAR(20)) RETURNS VARCHAR(50)
DETERMINISTIC
BEGIN
DECLARE NameStr CHAR(50);
SET NameStr=SELECT NAME FROM employees WHERE EmployeeID = EmplID;
RETURN NameStr ;
END;
报错信息:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘SELECT NAME FROM employees WHERE EmployeeID = EmplID;
return NameStr ;
END’ at line 5
查找许多的原因 发现 是
SET NameStr=SELECT NAME FROM employees WHERE EmployeeID = EmplID;
发生报错
处理方法:(对赋值部分添加“()”)
SET NameStr=(SELECT NAME FROM employees WHERE EmployeeID = EmplID);
查询:CREATE FUNCTION func(EmplID varchar(20)) RETURNS varchar(50)
DETERMINISTIC BEGIN DECLARE NameStr CHAR(50); set NameStr=(SELECT N…共 0 行受到影响
创建成功!