我也是刚学SQL 语句,下面试着写了一个存储过程,不知是否正确。create procedure GetMaxId(tablename in varchar2(12),NextId out long) as currentid long; begin select current_id into currentid from tableids where table_name = tablename; currentid :=currentid +1; NextId : = currentid; update tableids set currentid =NextId where table_name = tablename; exception when not_date_found currentid := 0; NextId :=0; insert into tableids (table_name,current_id) values (tablename,0); end;
TO: wdslhr(编程渴求者) 呵呵,基本就是这个样子。不过select的时候要加上for update,锁定行。 我把需求改了,要求表中必须有tablename对应的行,否则出错。 3q
as
currentid long;
begin
select current_id into currentid from tableids where table_name = tablename;
currentid :=currentid +1;
NextId : = currentid;
update tableids set currentid =NextId where table_name = tablename;
exception when not_date_found
currentid := 0;
NextId :=0;
insert into tableids (table_name,current_id) values (tablename,0);
end;
呵呵,基本就是这个样子。不过select的时候要加上for update,锁定行。
我把需求改了,要求表中必须有tablename对应的行,否则出错。
3q