CREATE TRIGGER update_invoice_complex AFTER UPDATE ON t_sfi_inter_invoice FOR EACH ROW BEGIN INSERT INTO t_sfi_inter_bus_complex (OPER_TIME, OPERATION_TYPE, OLD_OBJECT,NEW_OBJECT) VALUES (NOW(), 'update',JSON_OBJECT(OLD.ID,OLD_OINVOICE_NO,OLD.SUM,OLD.FACT_SUM,OLD.STATUS,OLD.BUSINESS_ID,OLD.REINVOICE,OLD.IS_PRINTED,OLD.PRINT_USER_ID,OLD.PRINT_USER_NAME,OLD.PRINT_DATE,OLD.INVOICE_TYPE,OLD.INVOICE_STATUS,OLD.TOMB_CERTIFICATE),JSON_OBJECT(NEW.ID,OINVOICE_NO,NEW.SUM,NEW.FACT_SUM,NEW.STATUS,NEW.BUSINESS_ID,NEW.REINVOICE,NEW.IS_PRINTED,NEW.PRINT_USER_ID,NEW.PRINT_USER_NAME,NEW.PRINT_DATE,NEW.INVOICE_TYPE,NEW.INVOICE_STATUS,NEW.TOMB_CERTIFICATE)); END;优化一下
时间: 2024-03-27 07:39:37 浏览: 67
可以尝试将触发器中的JSON_OBJECT函数拆分成单独的变量,以提高可读性和易维护性。同时,将INSERT语句中的字段名改为明确的列名,而不是使用通配符。建议修改后的触发器代码如下:
```
CREATE TRIGGER update_invoice_complex
AFTER UPDATE ON t_sfi_inter_invoice
FOR EACH ROW
BEGIN
DECLARE old_invoice_json JSON;
DECLARE new_invoice_json JSON;
SET old_invoice_json = JSON_OBJECT('id', OLD.ID,
'oinvoice_no', OLD.OINVOICE_NO,
'sum', OLD.SUM,
'fact_sum', OLD.FACT_SUM,
'status', OLD.STATUS,
'business_id', OLD.BUSINESS_ID,
'reinvoice', OLD.REINVOICE,
'is_printed', OLD.IS_PRINTED,
'print_user_id', OLD.PRINT_USER_ID,
'print_user_name', OLD.PRINT_USER_NAME,
'print_date', OLD.PRINT_DATE,
'invoice_type', OLD.INVOICE_TYPE,
'invoice_status', OLD.INVOICE_STATUS,
'tomb_certificate', OLD.TOMB_CERTIFICATE);
SET new_invoice_json = JSON_OBJECT('id', NEW.ID,
'oinvoice_no', NEW.OINVOICE_NO,
'sum', NEW.SUM,
'fact_sum', NEW.FACT_SUM,
'status', NEW.STATUS,
'business_id', NEW.BUSINESS_ID,
'reinvoice', NEW.REINVOICE,
'is_printed', NEW.IS_PRINTED,
'print_user_id', NEW.PRINT_USER_ID,
'print_user_name', NEW.PRINT_USER_NAME,
'print_date', NEW.PRINT_DATE,
'invoice_type', NEW.INVOICE_TYPE,
'invoice_status', NEW.INVOICE_STATUS,
'tomb_certificate', NEW.TOMB_CERTIFICATE);
INSERT INTO t_sfi_inter_bus_complex (OPER_TIME, OPERATION_TYPE, OLD_OBJECT, NEW_OBJECT)
VALUES (NOW(), 'update', old_invoice_json, new_invoice_json);
END;
```
阅读全文