视图 转化为用Trigger生成的物理表。
CREATE VIEW VW_companies
AS
SELECT
  t.Id,
  t.Name,
  t.FullName,
  t.Phone400,
  t.Phone400Tail,
  t.`Price`,
  t.`DailyBudget`,
  t.`Balance`,
  t.`ActiveTime`,
  t.`Address`,
  t.`Contact`,
  t.`ContractNo`,
  t.`CreationTime`,
  t.`Email`,
  t.`FirstDeployTime`,
  t.`funding`,
  t.`LastUpdateTime`,
  t.`LicenseRef`,
  t.`PostCode`,
  t.`SMSReceiver`,
  t.`StartDate`,
  t.`status`,
  t.`worktime`,
  t.CreationUser,
  t.SalesOwner,
  t.AgentId,
  (SELECT u.Name FROM Users u WHERE (u.Id = t.SalesOwner)) AS SalesOwnerName,
  t.CSOwner,
  (SELECT u.Name FROM Users u WHERE (u.Id = t.CSOwner)) AS CSOwnerName,
  (SELECT u.ReportTo FROM Users u WHERE (u.Id = t.SalesOwner)) AS salesReportTo,
  (SELECT u.ReportTo FROM Users u WHERE (u.Id = t.CSOwner)) AS csReportTo,
  t.CategoryId,
  t.IsMember,
  d.Title as CategoryTitle,
  t.`CompanyTypeId`,
  t.Popularity,
  dim.`TypeName` as CompanyTypeName,
  dim.`TypeDesc` as CompanyTypeDesc
FROM
  companies t,categories d,`DIM_CommonType` dim where (`t`.`CategoryId` = `d`.`Id` and t.`CompanyTypeId`=dim.`TypeID`);

1、 CREATE trigger trig_1 before insert on companies 
 for each row
 insert into test set id=new.id,name=new.name,
 fullname=new.fullname,phone400=new.phone400,phone400tail=new.phone400tail,
 price=new.price,dailybudget=new.dailybudget,balance=new.balance,
 activetime=new.activetime,address=new.address,contact=new.contact,
 contractno=new.contractno,creationtime=new.creationtime,email=new.email,
 FirstDeployTime=new.FirstDeployTime,funding=new.funding,
 LastUpdateTime=new.LastUpdateTime,LicenseRef=new.LicenseRef,PostCode=new.PostCode,
 SMSReceiver=new.SMSReceiver,StartDate=new.StartDate,status=new.status,
 worktime=new.worktime,CreationUser=new.CreationUser,SalesOwner=new.SalesOwner,
 AgentId=new.AgentId,SalesOwnerName=(SELECT u.Name FROM Users u WHERE (u.Id = new.SalesOwner)),
 CSOwner=new.CSOwner,CSOwnerName=(SELECT u.Name FROM Users u WHERE (u.Id = new.CSOwner)),
 salesReportTo=(SELECT u.ReportTo FROM Users u WHERE (u.Id = new.SalesOwner)),
 csReportTo=(SELECT u.ReportTo FROM Users u WHERE (u.Id = new.CSOwner)),
 CategoryId=new.CategoryId,IsMember=new.IsMember,
 CategoryTitle=(select d.Title from categories d where (d.id=new.CategoryId)),
 CompanyTypeId=new.CompanyTypeId,Popularity=new.Popularity,
 CompanyTypeName=(select dim.TypeName from DIM_CommonType dim where (dim.TypeID=new.CompanyTypeId)),
 CompanyTypeDesc=(select dim.TypeDesc from DIM_CommonType dim where (dim.TypeID=new.CompanyTypeId));

2.1、
create trigger trig_2 before insert on ChargeEvents for each row insert into test2 set CompanyId=new.CompanyId, ChargeID=new.ChargeID,ChargeAmount=format(new.ChargeAmount,1),ChargeType=(select d.ChargeType from DIM_ChargeType d where d.ChargeID=new.ChargeID and (new.ChargeID in (1,2,101,102,104,105))),CreationTime=new.CreationTime;

2.2、
create trigger trig_22 before insert on Incentives for each row insert into test2 set CompanyId=new.CompanyId,ChargeID=-1,ChargeAmount=concat(new.Amount, '(', DATE_FORMAT(new.`ExpireTime`, '%Y-%m-%d'), ')'),ChargeType='优惠',CreationTime=new.CreationTime;

3、
### ### ###
create trigger trig_3 before insert on OS_WFENTRY for each row insert into test3 set id=new.id,name=new.name,state=new.state,creationTime=new.creationTime, finishTime=new.finishTime,companyId=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId'), extraAmount=(select int_val from OS_PROPERTYENTRY t4 where t4.entity_id=new.id and t4.entity_key='extraAmount'), openAccount=(select int_val from OS_PROPERTYENTRY t5 where t5.entity_id=new.id and t5.entity_key='openAcount'), companyName=(select FullName from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')), ContractNo=(select ContractNo from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')),CategoryId=(select CategoryId from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')), CategoryTitle=(select Title from categories ca where ca.Id=(select CategoryId from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId'))), SalesOwner=(select SalesOwner from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')), AgentId=(select AgentId from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')), SalesOwnerName=(SELECT u.Name FROM Users u WHERE (u.Id =(select SalesOwner from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')))), creationUser=new.creationUser, creationUserName=(SELECT u.Name FROM Users u WHERE (u.Id = new.creationUser));

4、
b2c > delimiter ||
b2c > create trigger trig_4 before insert on OS_WFENTRY
  -> for each row
  -> BEGIN 
  -> IF new.name in ('新开户','代理商新开户','新开户V2.0') THEN 
  -> insert into test4
  -> set
  -> id=new.id,name=new.name,state=new.state,creationTime=new.creationTime,
  -> finishTime=(select finish_date from OS_HISTORYSTEP h where h.entry_id=new.id and h.action_id=61 and h.step_id=61),
  -> companyId=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId'),
  -> totalAmount=(select int_val from OS_PROPERTYENTRY t4 where t4.entity_id=new.id and t4.entity_key='totalAmount'),
  -> chargetAmount=(select int_val from OS_PROPERTYENTRY t3 where t3.entity_id=new.id and t3.entity_key='chargetAmount'),
  -> openAccount=(select int_val from OS_PROPERTYENTRY t5 where t5.entity_id=new.id and t5.entity_key='openAcount'),
  -> backAmount=(select int_val from OS_PROPERTYENTRY t6 where t6.entity_id=new.id and t6.entity_key='backAmount'),
  -> extraBack=(select int_val from OS_PROPERTYENTRY t7 where t7.entity_id=new.id and t7.entity_key='extraBack'),
  -> commissionAdjustment=(select int_val from OS_PROPERTYENTRY t8 where t8.entity_id=new.id and t8.entity_key='commissionAdjustment'),
  -> extraIncentive=(select int_val from OS_PROPERTYENTRY t9 where t9.entity_id=new.id and t9.entity_key='extraIncentive'),
  -> companyName=(select Name from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')),
  -> companyFullName=(select FullName from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')),
  -> ContractNo=(select ContractNo from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')),
  -> CategoryId=(select CategoryId from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')),
  -> CategoryTitle=(select Title from categories ca where ca.Id=(select CategoryId from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId'))),
  -> SalesOwner=(select SalesOwner from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')),
  -> AgentId=(select AgentId from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')),
  -> SalesOwnerName=(select Name from Users u where u.id=(select SalesOwner from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId'))));
  -> ELSEIF new.name in ('充值续费','代理商充值续费','试用转正常') and new.state=4 THEN
  -> insert into test4
  -> SET
  -> id=new.id,name=new.name,state=new.state,
  -> creationTime=new.creationTime,finishTime=new.finishTime,
  -> companyId=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId'),
  -> totalAmount=(select int_val from OS_PROPERTYENTRY t4 where t4.entity_id=new.id and t4.entity_key='totalAmount'),
  -> chargetAmount=(select int_val from OS_PROPERTYENTRY t3 where t3.entity_id=new.id and t3.entity_key='chargetAmount'),
  -> openAccount=(select int_val from OS_PROPERTYENTRY t10 where t10.entity_id=new.id and t10.entity_key='openAcount'),
  -> backAmount=(select int_val from OS_PROPERTYENTRY t6 where t6.entity_id=new.id and t6.entity_key='backAmount'),
  -> extraBack=(select int_val from OS_PROPERTYENTRY t7 where t7.entity_id=new.id and t7.entity_key='extraBack'),
  -> commissionAdjustment=(select int_val from OS_PROPERTYENTRY t8 where t8.entity_id=new.id and t8.entity_key='commissionAdjustment'),
  -> extraIncentive=(select int_val from OS_PROPERTYENTRY t9 where t9.entity_id=new.id and t9.entity_key='extraIncentive'),
  -> companyName=(select Name from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')),
  -> companyFullName=(select FullName from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')),
  -> ContractNo=(select ContractNo from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')),
  -> CategoryId=(select CategoryId from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')),
  -> CategoryTitle=(select Title from categories ca where ca.Id=(select CategoryId from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId'))),
  -> SalesOwner=(select int_val from OS_PROPERTYENTRY t5 where t5.entity_id=new.id and t5.entity_key='salesMan'),
  -> AgentId=(select AgentId from companies c where c.Id=(select int_val from OS_PROPERTYENTRY t2 where t2.entity_id=new.id and t2.entity_key='companyId')),
  -> 

第四个只写到了一半。。。