GBASE南大通用分享:
-- 存储过程异常处理
获取错误数命令
get diagnostics @a=number; //包含warning和错误的数量
获取DML操作影响的行数,只影响最后一条DML操作的影响行数
get diagnostics @a=row_count;
获取错误号命令格式
get diagnostics condition 1 @a=gbase_errno;
获取错误状态命令格式
get diagnostics condition 1 @a=returned_sqlstate;
获取错误信息命令格式
get diagnostics condition 1 @a=message_text;
declare done int default 0;
declare errno int ;
declare sstate varchar(10922);
declare message varchar(10922);
declare continue handler for sqlstate '02000' set done = '1';
declare continue handler for sqlexception
begin
GET DIAGNOSTICS CONDITION 1 ERRNO = GBASE_ERRNO, SSTATE = RETURNED_SQLSTATE, MESSAGE = MESSAGE_TEXT;
end;
drop table if exists t1;
get diagnostics @a=number;
select @a;
drop table t2;
select ERRNO,SSTATE,MESSAGE;
create table t4(a int,b varchar(10));
insert into t4 values(1,1),(2,2),(3,3),(4,4);
get diagnostics row_count=row_count;
select row_count;
-- while 循环
drop procedure if exists p3;
delimiter //
create procedure p3()
begin
set @a=10;
while @a>1 do
select @a;
set @a=@a-1;
end while;
-- if循环
if @a <= 3 then
select '一等';
elseif @a<=7 then
select '二等';
else
select '三等';
end if;
-- iterate
label1: LOOP
SET p1 = p1 + 1;
IF p1 < 10 THEN
select p1;
ITERATE label1;
END IF;
LEAVE label1;
END LOOP label1;
SET @x = p1;
select @x;
-- case when 循环
while p1 > 1 do
select case when p1>=80 then p1||'优秀'
when 80>p1 and p1>=60 then p1||'及格'
else p1||'不及格'
end
;
set p1=p1-10;
-- select p1;
end while;
-- repeat
set @a=0;
repeat set @a=@a+1;
select @a;
until @a=p1 end repeat;
-- 静态游标
declare v_a int;
declare done int default 0;
declare cur_1 cursor for select distinct a from t1 order by a desc;
declare continue handler for sqlstate '02000' set done = 1;
open cur_1;
repeat
fetch cur_1 into v_a;
if not done then
-- select v_a;
select done;
end if;
until done end repeat;
close cur_1;
-- 动态游标
end //
delimiter;