时间:2021-07-01 10:21:17 帮助过:24人阅读
create or replace trigger pri_stu_test_limit before update or delete or insert on stu.zzz_test DECLARE PRAGMA AUTONOMOUS_TRANSACTION; //自治事务。对于定义成自治事务的Procedure,实际上相当于一段独立运行的程序段,这段程序不依赖于主程序,也不干涉主程序 v_username varchar2(200) default ‘‘; BEGIN select s.USERNAME into v_username from v$session s wheres.audsid=(select userenv(‘SESSIONID‘) from dual) and rownum<2; IF deleting AND (sys_context(‘userenv‘,‘ip_address‘) not in(‘192.168.120.211‘) OR ‘stuuser‘ like v_username) THEN RAISE_APPLICATION_ERROR(-20001, ‘can not delete the table ‘); //用于在plsql使用程序中自定义不正确消息
语法为:raise_application_error(error_number,message[,[true|false]]);
error_number用于定义不正确号,该不正确号必须在-20000到-20999之间的负整数;
message用于指定不正确消息,并且该消息的长度无法超过2048字节;
第三个参数假如为true,则该不正确会被放在先前不正确堆栈中,假如为false(默认值)则会替代先前所有不正确。
ELSIF inserting AND (sys_context(‘userenv‘,‘ip_address‘) not in(‘192.168.120.211‘) OR ‘stuuser‘ like v_username) THEN RAISE_APPLICATION_ERROR(-20001, ‘can not insert the table ‘); ELSIF updating AND (sys_context(‘userenv‘,‘ip_address‘) not in(‘192.168.120.211‘) OR ‘stuuser‘ like v_username) THEN RAISE_APPLICATION_ERROR(-20001, ‘can not update the table ‘); END IF; END;
4)验证:
SQL>
SQL> insert into stu.zzz_testvalues(3,‘zhuren33‘);
insert into stu.zzz_testvalues(3,‘zhuren33‘)
ORA-20001: can not insert the table
ORA-06512: at"stuuser.PRI_STU_ACCT_LIMIT", line 18
ORA-04088: error during execution oftrigger ‘stuuser.PRI_STU_ACCT_LIMIT‘
SQL> commit;
Commit complete
SQL>
SQL> update stu.zzz_test setremark=‘zhuren33_up‘ where id=3;
update stu.zzz_test setremark=‘zhuren33_up‘ where id=3
ORA-20001: can not update the table
ORA-06512: at"stuuser.PRI_STU_ACCT_LIMIT", line 22
ORA-04088: error during execution oftrigger ‘stuuser.PRI_STU_ACCT_LIMIT‘
SQL> commit;
Commit complete
SQL>
SQL> delete from stu.zzz_test where id=3;
delete from stu.zzz_test where id=3
ORA-20001: can not delete the table
ORA-06512: at"stuuser.PRI_STU_ACCT_LIMIT", line 14
ORA-04088: error during execution oftrigger ‘stuuser.PRI_STU_ACCT_LIMIT‘
SQL> commit;
Commit complete
SQL>
// OK增删改都可以被限制住了,应该暂时解决了问题。
ORACLE 限制某些IP、用户的对重要表的恶意操作
标签:can 参数 ddr sql als exe add 替代 ica