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;
/