to sanoul(垃圾): 照你的意思,楼住不用for update/lock table就是没有使用事务,你用了for update/lock table就是使用了事务。 连事务的基本概念都不懂
方法1:使用序列。 但是还应该注意到,因为你的RecID号和时间有关系,那么如果到了第二天,RecID号必须又从1开始,那么你必须在凌晨安排一个作业(Job)自动将序列重新置1才行。 序列的用法(Seq1为序列,每次用nextval时它会自动的加1): insert into Table1 (RecID) values( Seq1.nextval)。方法2: 用for update给查询加锁。以下程序我把获取RecID部分试着修改了,但没有测试,你自己测试一下,如果有问题,再问我!(注:c_test是一个游标类型的变量)open c_test for select RecID from M_RecMaterial for update; --这是第一次查询open c_test for --上下两个游标都打开 select RecID from M_RecMaterial where Substr(RecID,1,2)='YS' AND substr(recid,7,8)=to_char(SYSDATE,'yyyyMMdd') order by RecID desc for update;fetch c_test into mMaxRecID;if c_test%found then --如果存在最大RecID,则取游标的第一条记录
while c_test%found loop
mMaxRecID := Substr(mMaxRecID,1,14) || Substr(to_char(to_Number(Substr(mMaxRecID,15,4))+10001),2,4); --退出循环(取游标的第一条记录即是最大mMaxRecID,因为我们定义游标时排了序) exit; fetch c_test into mMaxRecID; end loop;else --如果不存在最大RecID
mMaxRecID:='YS0000' || to_Char(sysdate,'yyyyMMdd') || '0001'; end if;
然后再做你的东西,commit后自动解锁
照你的意思,楼住不用for update/lock table就是没有使用事务,你用了for update/lock table就是使用了事务。
连事务的基本概念都不懂
但是还应该注意到,因为你的RecID号和时间有关系,那么如果到了第二天,RecID号必须又从1开始,那么你必须在凌晨安排一个作业(Job)自动将序列重新置1才行。
序列的用法(Seq1为序列,每次用nextval时它会自动的加1):
insert into Table1 (RecID) values( Seq1.nextval)。方法2: 用for update给查询加锁。以下程序我把获取RecID部分试着修改了,但没有测试,你自己测试一下,如果有问题,再问我!(注:c_test是一个游标类型的变量)open c_test for select RecID from M_RecMaterial for update; --这是第一次查询open c_test for --上下两个游标都打开
select RecID from M_RecMaterial
where Substr(RecID,1,2)='YS'
AND substr(recid,7,8)=to_char(SYSDATE,'yyyyMMdd')
order by RecID desc
for update;fetch c_test into mMaxRecID;if c_test%found then --如果存在最大RecID,则取游标的第一条记录
while c_test%found loop
mMaxRecID := Substr(mMaxRecID,1,14) || Substr(to_char(to_Number(Substr(mMaxRecID,15,4))+10001),2,4); --退出循环(取游标的第一条记录即是最大mMaxRecID,因为我们定义游标时排了序)
exit;
fetch c_test into mMaxRecID;
end loop;else --如果不存在最大RecID
mMaxRecID:='YS0000' || to_Char(sysdate,'yyyyMMdd') || '0001';
end if;