简体   繁体   English

插入值取决于另一列触发器sql的值

[英]Insert value depending on the value of another column trigger sql

So here's my problem. 所以这是我的问题。 I have excel file upload. 我有excel文件上传。 what i need is I need to insert '1' or '2' in one column while populating data from excel file to sql db depend on the value of another column. 我需要的是在将数据从excel文件填充到sql db时,我需要在一列中插入“ 1”或“ 2”,这取决于另一列的值。 is it possible to create trigger that inserts while uploading data in the same table?. 是否可以创建在同一表中上传数据时插入的触发器? Thank you. 谢谢。

 CREATE
/*!50017 DEFINER = 'root'@'localhost' */
TRIGGER `trg_utCode` BEFORE  INSERT ON `exceldisbursements` 
FOR EACH ROW 

BEGIN 
 UPDATE exceldisbursements
 SET ut_id = CASE
 WHEN ex_responsibility_center LIKE '001-05%' THEN 2
 WHEN ex_responsibility_center LIKE '001-5%' THEN 2
 ELSE 1
END;
END;

what you're making there is a full update on your table (you don't have any where clause). 您正在执行的操作在您的表上有完整的更新(您没有任何where子句)。 You just need to use the ":new" bind variable. 您只需要使用“:new”绑定变量。 eg: 例如:

BEGIN 
 :new.ut_id = CASE
 WHEN :new.ex_responsibility_center LIKE '001-05%' THEN 2
 WHEN :new.ex_responsibility_center LIKE '001-5%' THEN 2
 ELSE 1
 END;
END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM