NutzCN Logo
问答 MYSQL创建函数失败
发布于 3043天前 作者 Kerbores 1746 次浏览 复制 上一个帖子 下一个帖子
标签:
DROP FUNCTION
IF EXISTS getAge;

CREATE FUNCTION getAge (idcard VARCHAR) RETURNS INT
BEGIN
	DECLARE
		age INT;


SET age = 0;

SELECT
	YEAR (curdate()) -
IF (
	length(idcard) = 18,
	substring(idcard, 7, 4),

IF (
	length(idcard) = 15,
	concat('19', substring(idcard, 7, 2)),
	NULL
)
) INTO age
FROM
	DUAL;

RETURN age;


END;

执行报错

[SQL]DROP FUNCTION
IF EXISTS getAge;
受影响的行: 0
时间: 0.095s

[SQL]

CREATE FUNCTION getAge (idcard VARCHAR) RETURNS INT
BEGIN
	DECLARE
		age INT;


SET age = 0;

SELECT
	YEAR (curdate()) -
IF (
	length(idcard) = 18,
	substring(idcard, 7, 4),

IF (
	length(idcard) = 15,
	concat('19', substring(idcard, 7, 2)),
	NULL
)
) INTO age
FROM
	DUAL;

RETURN age;


END;
[Err] 1064 - 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 ') RETURNS INT
BEGIN
	DECLARE
		age INT;


SET age = 0;

SELECT
	YEAR (c' at line 1
4 回复

DUAL不是oracle的东西吗

来自炫酷的 NutzCN

但是报错不是在那里,而且mysql也是可以查的

SELECT
	YEAR (curdate()) -
IF (
	length('500222198606132288') = 18,
	substring('500222198606132288', 7, 4),

IF (
	length('500222198606132288') = 15,
	concat('19', substring('500222198606132288', 7, 2)),
	NULL
)
) 
FROM
	DUAL

这个是可以直接执行的,结果30

已经解决 入参varchar指定长度

添加回复
请先登陆
回到顶部