delimiter //

create or replace view ta_triggers0 as
with e as
(
select t.tab_name,s.operation_type,concat(lower(tab_name),'_a',lower(operation_type),'rtrg') trigger_name,t.tab_id from
tab t
cross join (select 'I' operation_type union all select 'U' union all select 'D') s
where not t.tab_type_id=0
)
SELECT
tab_name,operation_type,trigger_name,
cast(
concat(
rtrim("delimiter //                    "),char(10),
"CREATE OR REPLACE TRIGGER ",trigger_name,char(10),
rtrim(" AFTER"),char(10),
" ",case operation_type when 'I' then 'INSERT' when 'U' then 'UPDATE' when 'D' then 'DELETE' end,char(10),
" ON ",tab_name,char(10),
" FOR EACH ROW",char(10),
rtrim("begin                                                              "),char(10),
rtrim("DECLARE nau MEDIUMINT UNSIGNED;                                    "),char(10),
rtrim("declare bar DATETIME;                                              "),char(10),
rtrim("declare usr MEDIUMINT UNSIGNED;                                    "),char(10),
rtrim("declare ws BIGINT UNSIGNED;                                        "),char(10),
rtrim("declare cut MEDIUMINT UNSIGNED;                                    "),char(10),
rtrim("SELECT NEXTVAL('AU_SEQ'), user_id, ses_id, now() INTO nau, usr, ws, bar FROM st_curr_user;                                                "),char(10),
"SELECT tab_id INTO cut FROM tab WHERE tab_name = '",tab_name,"';",char(10),
"INSERT INTO au (db_time, au_nr, user_id, tab_id, operation_type, ses_id) VALUES (bar,nau,usr,cut,'",operation_type,"',ws);",char(10),
"INSERT INTO gttn (d1,id1,id2,c1,c2,c0) VALUES (bar,nau,cut,'",tab_name,"','",operation_type,"','totoro');",char(10),
 "") as char) ddl
,0 ind
from e


//

delimiter ;