生成创建触发器所需的SQL(Oracle)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成创建触发器所需的SQL(Oracle)相关的知识,希望对你有一定的参考价值。

Generate the SQL necessary to create triggers that use the sequences listed above.
NOTE: This particular script assumes that the tables all start with ‘tbl’.
It may need to be modified to work with different naming conventions.
  1. SELECT 'CREATE OR REPLACE TRIGGER trg' || substr (TABLE_NAME,4,LENGTH(TABLE_NAME)-3) || '
  2. before insert on tbl' || substr (TABLE_NAME,4,LENGTH(TABLE_NAME)-3) || '
  3. for each row
  4. declare
  5. begin
  6. if (:new.' || substr (TABLE_NAME,4,LENGTH(TABLE_NAME)-3) || 'id =0) or (:new.' || substr (TABLE_NAME,4,LENGTH(TABLE_NAME)-3) || 'id is null) then
  7. begin
  8. select tbl' || substr (TABLE_NAME,4,LENGTH(TABLE_NAME)-3) || '_seq.nextval
  9. into
  10. :new.' || substr (TABLE_NAME,4,LENGTH(TABLE_NAME)-3) || 'id from dual;
  11. end;
  12. end if;
  13. end;
  14. / '
  15. FROM user_tables

以上是关于生成创建触发器所需的SQL(Oracle)的主要内容,如果未能解决你的问题,请参考以下文章