oracle 逻辑写,请问下,这个逻辑如何写好些

SQL codecreate table tTestData(

C1 Number,

C2 Number,

C3 Number,

C4 Number

);

insert into tTestData(C1, C2, C3, C4)

select 1, 100, 200, 0 from dual

union all

select 2, 100, 200, 0 from dual

union all

select 2, 100, 200, 0 from dual

union all

select 1, 100, 200, 1 from dual

union all

select 8, 100, 200, 0 from dual

union all

select 8, 100, 200, 1 from dual;

Commit;

create table tLogicPar(

sStartValue varchar2(20),

sAllowValue varchar2(200),

sExecuteSql varchar(4000),

sErrorMsg varchar(2000),

iLogicType Integer,

iOrder Integer

);

delete from tLogicPar;

insert into tLogicPar(sStartValue, sAllowValue, sExecuteSql, sErrorMsg, iLogicType, iOrder)

select '1', ',1,', '', '第一条记录必须是1', 0, 0 from dual

union all

select '1', ',2,8,', '你要的Sql', '1的后面只能是2和8', 1, 1 from dual

union all

select '2', ',2,8,', '你要的Sql', '2的后面只能是2和8', 1, 2 from dual

union all

select '8', ',1,', 'Commit', '8的后面只能是1', 1, 3 from dual;

Commit;

create or replace procedure PLogicCheck(iTestType number) is

sCurrentValue varchar2(20);

sAllowValue varchar2(200);

sExecuteSql Varchar2(4000);

sErrorMsg Varchar2(2000);

iRec Number;

iCheck Number;

eError Exception;

begin

iRec:= 0;

for CData in (select C1, C2, C3 from tTestData where C4 <= iTestType) loop

iRec:= iRec + 1;

-- 检查第一条记录是否是正确的起点

if iRec = 1 then

begin

select sStartValue, sAllowValue, sExecuteSql, sErrorMsg

into sCurrentValue, sAllowValue, sExecuteSql, sErrorMsg

from tLogicPar

where iLogicType = 0;

if Nvl(To_Char(CData.C1), 'Check') <> Nvl(sCurrentValue, 'NoCheck') then

Dbms_Output.Put_Line(sErrorMsg);

Raise eError;

end if;

exception

when No_Data_Found then

Dbms_Output.Put_Line('没有找到逻辑检查起点!');

Raise eError;

when Too_Many_Rows then

Dbms_Output.Put_Line('过多逻辑检查起点!');

Raise eError;

when Others then

Dbms_Output.Put_Line('其他错误:' + SqlErrM);

Raise eError;

end;

end if;

-- 检查当前记录是否在上一记录的允许范围值之内

if instr(sAllowValue, ',' || To_Char(CData.C1) || ',') <= 0 then

Dbms_Output.Put_Line('第' || To_Char(iRec) || '条数据错误,当前值是' || To_Char(cData.C1) ||

',上一条是' || sCurrentValue || ',' || sErrorMsg);

Raise eError;

end if;

-- 检查通过未下一条语句的允许值、本条语句的执行语句、出错信息赋值

begin

select sStartValue, sAllowValue, sExecuteSql, sErrorMsg

into sCurrentValue, sAllowValue, sExecuteSql, sErrorMsg

from tLogicPar

where sStartValue = To_Char(cData.C1)

and iLogicType = 1;

-- 执行Sql语句

exception

when No_Data_Found then

Dbms_Output.Put_Line('没有找到' || To_Char(CData.C1) || '的逻辑检查设置!');

Raise eError;

when Too_Many_Rows then

Dbms_Output.Put_Line('过多的' || To_Char(CData.C1) || '的逻辑检查设置!');

Raise eError;

when Others then

Dbms_Output.Put_Line(To_Char(CData.C1) || ' 其他错误:' + SqlErrM);

Raise eError;

end;

end loop;

Dbms_Output.Put_Line('程序执行完毕,没有错误!');

exception

when eError then

Null;

when others then

Dbms_Output.Put_Line('发现未知错误:' + SqlErrM);

end;

/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值