Trigger Syntax mySQL - mysql

I want to make the Total column receive the total sum between the other columns, but i keeping getting the error 1193.
I'm new to mySql so i dont know where i should start looking to solve the error.
DELIMITER $$
CREATE TRIGGER Total
BEFORE INSERT ON `despesas` FOR EACH ROW
BEGIN
SET Total = Abertura_Processo+Telefone+Correspondencia+Cartorio+Conservatorio+Servico_Financas+Taxas_Emolumentos+Honorarios;
END;
$$
DELIMITER ;
I've read a bit and for what i've understood I need to create a variable, but i dont know how.
The total column has its values manually inserted i want it to be automatically

You can reference the newly inserted record's field via NEW.column_name in the before insert trigger. By setting a value to such a column, you can change the value being inserted. So, change the value setting line as follows:
SET NEW.Total = NEW.Abertura_Processo+NEW.Telefone+NEW.Correspondencia+NEW.Cartorio+NEW.Conservatorio+NEW.Servico_Financas+NEW.Taxas_Emolumentos+NEW.Honorarios;
Note, you may use generated columns as an alternative to this trigger.

Got it, every thing worked fine.

Related

Using a trigger to automatically assign a value to specific column on a new insert

I'm struggling to find proper information on SQL Triggers.
My previous question got removed for it not being specific enough so hopefully this will do better.
I am trying to create a trigger that will assign RoleID 1 to every newly inserted row to my users table.
But I can't seem to figure it out.
AFTER INSERT on users
on EACH ROW
insert into users.RoleID values(1);
This doesn't seem to work.
And the examples and or information regarding triggers all focus on alerts or sending emails after an insert/drop or update.
Can it actually be done?
Any help would be much appreciated.
Cheers!
It looks like you aren't creating you sql trigger with the correct keyword.
Try this
drop trigger if exists before_insert_users;
DELIMITER $$
CREATE TRIGGER before_insert_users
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
SET NEW.RoleID = 1;
END$$
DELIMITER ;
Note: RoleID will need to actually be a column on your table for this to work.
However, a trigger is not the best way to do this... See this SO post : Add a column with a default value to an existing table in SQL Server

CONCAT from two fields

Wanting to CONCAT two columns to make a new ID, as per the image.
Desired result
The Trigger statement I have is: And I have tried Trigger INSERT and UPDATE events.
Trigger statement
SET new.img_url_id = CONCAT(new.img_metadata_id, new.mag_name_id)
The Trigger statement returns values in the 'mag_name_id' field (10). The Trigger has to happen after field 'img_metadata_id' is inserted because 'img_metadata_id' is auto-incremented, and after 'mag_name_id' is inserted, it's pulled from another table. Changing the Trigger time to After, PHPMyAdmin gives an error. Do I need to do a Procedure?
Thanks.

MySql cannot update a DATETIME field - no record found

I have a table in MySql 5.6.10 defined as this:
When I do a select query (from HeidiSQL client) for a particular record filtering on the id_failed_delivery_log column, the record is found successfully, but when I use the same filter for the UPDATE command, then no record is found and no error reported:
When I update a different column using the same filter, the update works and I can see the updated value. Then there is an issue with an update to this particular column.
I've also tried updating with a time function instead of hard-coded date value, for example with the now() function but I still get the same result and the record is not found.
Could it be caused by the 'Default' value is set to CURRENT_TIMESTAMP?
After further investigation I found the reason why I couldn't update that field. I was not totally familiarized with the database definition and I found that there was a trigger in the same schema that forced to keep the date_created column with the same value:
SET #OLDTMP_SQL_MODE=##SQL_MODE, SQL_MODE='STRICT_ALL_TABLES';
DELIMITER //
CREATE TRIGGER `failed_delivery_log_before_update` BEFORE UPDATE ON
`failed_delivery_log` FOR EACH ROW BEGIN
SET NEW.date_created = OLD.date_created ;
END//
DELIMITER ;
SET SQL_MODE=#OLDTMP_SQL_MODE;
I removed this trigger temporarily in order to test. Once removed, the updated worked fine. The trigger execution was not reported in the SQL client, so it was difficult to find out its execution.

mysql trigger - select count(*) from trigger

I want to count rows created/inside (dont know what it is called) a trigger.
like i have a trigger :
CREATE
DEFINER=`root`#`localhost`
TRIGGER `mydb`.`tn_air_staff_leave_application`
AFTER INSERT ON `mydb`.`staff_leave_application`
FOR EACH ROW
BEGIN
----
----
----
END$$
and want to know how many rows trigger is going to create, I need this value inside trigger:
set #newrows:= (select count(*) from tn_air_staff_leave_application);
is it possible?? if not any other solution out there???
A trigger is a purely passive piece of code which is executed before or after a row is inserted, updated or deleted from a table in your database.
The trigger can not be executed actively anc thus does not have a return value you can read.
What you can do is to let your trigger write a value into another table or into a session variable which then can be queried for in an extra query.
You can not store a whole result row or a result set of several rows into a session variable like you are trying in your example.
In MySQL the trigger is always executed FOR EACH ROW, means within the trigger you will only look at one row, not at several.
EDIT:
Actually your example query returns a single value, so this would work. (would deliver the amount of total records in your tn_air_staff_leave_application table into the local session variable #newrows)
For more specific help you would need to be more specific on what you're trying to do

MySQL Trigger - INSERT on condition of UPDATE

I'm trying to find the most effecient way of inserting data into another table when a particular field is updated on trigger table. The INSERT should only occur on a specific type of update.
The table on which I want to create the trigger is named incremental. The table I'm inserting into is named crm_record
On incremental there is a field called status. By default when a record is initially added to the table the status field is set to new. After billing has processed that value changes to processed. So once this occurs I want to INSERT into crm_record, only if the value of another field (success) is set to 1.
I have considered using both CASE and IF but would like an expert's opinion on the best way to do this.
Ok, I eventually went with this that seemed to work. Thanks for pointing me in the right direction
CREATE TRIGGER `incremental5_after_ins_tr_crmm` AFTER UPDATE ON `incremental5`
FOR EACH ROW
BEGIN
IF Status = 'processed' AND Success = 1 THEN
INSERT INTO crm_master (msisdn,source,contract_type,revenue) VALUE (new.msisdn,'INC5',new.contract_type,revenue=revenue+2.5)
ON DUPLICATE KEY UPDATE contract_type=new.contract_type,revenue=revenue+2.5;
END IF;
END;
All you need to do is to create an AFTER UPDATE trigger and test the value of status and success together. If it's going only going to be one state you're testing for then an IF statement would be the simplest way to go about it.
However before implementing a trigger it's always worth going back a step and checking to see if the row in crm_record shouldn't actually be inserted via the code logic when the status and success columns are updated.