一般处理方式:
DELIMITER $$
DROP PROCEDURE IF EXISTS `studb`.`numbw_query` $$
CREATE PROCEDURE `studb`.`numbw_query` (in wk int)
BEGIN
declare sCount int default 0;
declare sMax int default 0;
select max(numbb) into sMax from numbe where weekd=wk;
--取最大的那个序号,可能存在为空的情况,
set sCount = 1 + sMax ;
--当sMax为null时,上面那句没作相加,也就是sCount<>1,仍然为0
select * from numbw where numbb=sCount;
END $$
DELIMITER ;
后来改进处理方式:
DELIMITER $$
DROP PROCEDURE IF EXISTS `studb`.`numbw_query` $$
CREATE PROCEDURE `studb`.`numbw_query` (in wk int)
BEGIN
declare sCount int default 0;
declare sMax int default 0;
select count(numbb) into sCount from numbe where weekd=wk;
--先判断是否存在数据
if sCount =0 then
set SCount = 1;
else
select max(numbb) into sMax from numbe where weekd=wk;
set sCount = 1 + sMax ;
end if;
select * from numbw where numbb=sCount;
END $$
DELIMITER ;
发现效率不太高;想了几天,没有结果,又想到用游标,但又太复杂;
又过了几天,发现可以通过is not null来判断是否为null值;
DELIMITER $$
DROP PROCEDURE IF EXISTS `studb`.`numbw_query` $$
CREATE PROCEDURE `studb`.`numbw_query` (in wk int)
BEGIN
declare sCount int default 0;
declare sMax int default 0;
select max(numbb) into sMax from numbe where weekd=wk;
if sMax is not null then
set sCount = 1 + sMax ;
else
set sCount = 1;
end if;
select * from numbw where numbb=sCount;
END $$
DELIMITER ;
问题解决了!
阅读(4010) | 评论(0) | 转发(0) |