在表上创建触发器时绑定变量错误

时间:2015-05-30 09:07:24

标签: plsql

我是PL / SQL新手。我无法为所有变量创建触发器获取绑定变量错误。

CREATE OR REPLACE TRIGGER ddl_change_after_insert1 AFTER INSERT ON changeddl.stats$ddl_log
FOR EACH ROW
DECLARE
    v_osuser varchar2(100);
    v_current_user varchar2(100);
    v_host varchar2(100);
    v_terminal varchar2(100);
    v_owner varchar2(30);
    v_ddl_date date;
    v_object_type varchar2(50);
    v_object_name varchar2(100);
    v_ddl_type varchar2(30);
    v_sql_txt varchar2(4000);
    v_mail_message varchar2(4000);
BEGIN
    v_osuser := 'osuser = ' || :new.osuser;
    v_current_user := ',current_user = ' || :new.current_user;
    v_host := ',host = ' || :new.host;
    v_terminal := ',terminal = ' || :new.terminal;
    v_owner := ',owner = ' || :new.owner;
    v_ddl_date := sysdate;
    v_object_type := ',object_type ' || :new.object_type;
    v_object_name := ',object_name = ' || :new.object_name;
    v_ddl_type := ',ddl_type = ' || :new.ddl_type;
    v_sql_txt := ',sql_txt = ' || :new.sql_text;

    v_mail_message := 'osuser :' || v_osuser ||
                      ',current_user :' || v_current_user ||            
                      ',host :' || v_host ||                    
                      ',terminal :' || v_terminal ||
                      ',owner :' || v_owner ||
                      ',ddl_date :' || v_ddl_date ||
                      ',object_type :' || v_object_type ||
                      ',object_name :' || v_object_name ||
                      ',ddl_type :' || v_ddl_type ||
                      ',sql_txt :' || v_sql_txt;


    EXECUTE IMMEDIATE 'ALTER SESSION SET smtp_out_server = ''127.0.0.1''';
    UTL_MAIL.send(sender => 'root@localhost',
            recipients => 'chandan@gmail.in',
               subject => 'Database Objects Modified',
               message => v_mail_message,
             mime_type => 'text/html; charset=utf-8');
             END;
/

以下是我的基表。请帮我解决这个问题

CREATE TABLE changeddl.stats$ddl_log (
  ddl_date date,
  OSUSER varchar2(100),
  CURRENT_USER varchar2(100),
  HOST varchar2(100),
  TERMINAL varchar2(100),
  owner varchar2(30),
  object_type varchar2(50),
  object_name varchar2(100),
  ddl_type varchar2(30),
  sql_txt varchar2(2000));

请帮助我,我是plsql的新手..

以下是ddl更改时将数据插入stats$ddl_log表的主要触发器。

create or replace trigger ddlchange_trigger after ddl on database
declare
  sql_text ora_name_list_t;
  stmt VARCHAR2(4000) := '';
  n number;
begin
  if (ora_sysevent='TRUNCATE')
  then
    null;
  else
    n:=ora_sql_txt(sql_text);
    for i in 1..n
    loop
      stmt:=substr(stmt||sql_text(i),1,1000);
    end loop; 
    insert into changeddl.stats$ddl_log(ddl_date, osuser,current_user,host,terminal,owner,object_type,object_name,ddl_type,sql_txt)
    values(
      sysdate,
      sys_context('USERENV','OS_USER') ,
      sys_context('USERENV','CURRENT_USER') ,
      sys_context('USERENV','HOST') , 
      sys_context('USERENV','TERMINAL') ,
      ora_dict_obj_owner,
      ora_dict_obj_type,
      ora_dict_obj_name,
      ora_sysevent,
      stmt
    );
  end if;
end;
/

1 个答案:

答案 0 :(得分:0)

:NEW:OLD是伪列。

如何使用它们?

:NEW.your_table_column

:OLD.your_table_column

在你的触发器中

v_sql_txt := ',sql_txt = ' || :new.sql_text;

而是将上面一行改为下面。您的表格列为sql_txt而不是sql_text

v_sql_txt := ',sql_txt = ' || :new.sql_txt;

<强> ==更新==

newline连接到字符串

'Hi' || chr(10) || 'good' || chr(10) || 'morning'

使用管道||连接两个字符串。