mysql 存储函数 if_MySQL if语句序列在存储函数中

我在MYSQL中使用IF语句有一个奇怪的问题。

我有一个存储函数,以字符串的形式返回表的内容。为了让我的字符串返回正确数量的结果,我准备了一个基于数字的select语句。如果该数字<= 9则表示一个sql语句并且在另一个之上。

但是,在序列中,我只能将if语句放在函数的末尾。如果我把它放在正确的位置,它现在被注释掉了,它会给我一个语法错误。如果我把if语句放在函数末尾不正确的地方,我不会收到错误。

请帮忙。我不知道这是一个错误还是我自己的错误。

DELIMITER $$

CREATE DEFINER=`root`@`localhost` FUNCTION `returnstring`(IDGrade int) RETURNS varchar(255) CHARSET latin1

BEGIN

Declare fSubjectID, sSubjectID varchar (255);

declare sqlstatement varchar(255);

#Declare variable for done of loop

Declare done int default 0;

#Declare variables from the select statement

#IF IDGRADE <= 9 then set sqlstatement = 'SELECT subjectID FROM subjecttb where sectType = "b" or secttype = "j" order by subjectID';

#ELSEIF IDGRADE > 9 then set sqlstatement = 'SELECT subjectID FROM subjecttb where sectType = "b" or secttype = "s" order by subjectID';

#END IF;

#Declare a cursor to iterate through the table

declare cursor1 cursor for

SELECT subjectID

FROM `marksdb`.`subjecttb`

where sectType = 'b' or secttype = 'j' order by subjectID;

#Continue loop until nothing is found anymore

declare continue handler for not found set done=1;

#Runs the select statement

open cursor1;

#Declares a loop

set fsubjectid='';

SubjectID_loop:loop

Fetch cursor1 into sSubjectID;

if done=1 then # no more rows to fetch

leave SubjectID_loop;

end if;

set fSubjectID = concat(fSubjectID, sSubjectID , ' varchar (255), ');

end loop SubjectID_loop;

set fSubjectID = substring(fSubjectID, 1, length(fsubjectid)-2);

close cursor1;

return fSubjectID;

#Declare variables from the select statement

IF IDGRADE <= 9 then set sqlstatement = 'SELECT subjectID FROM subjecttb where sectType = "b" or secttype = "j" order by subjectID';

ELSEIF IDGRADE > 9 then set sqlstatement = 'SELECT subjectID FROM subjecttb where sectType = "b" or secttype = "s" order by subjectID';

END IF;结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值