Mysql trigger with two tables and if statement - mysql

I cannot find any solution for this 'problem'.
My database needs to update a field in one table when another table is updated.
(only when the new value is 5 or 7 and the new prop.key is "status_id")
But the updated table doesn't include the values that I need. It only contains an ID to another table.
I am not sure about the syntax for this statement. This one won't work. Can anybode help me?
This is what I tried:
delimiter $$
CREATE TRIGGER autoClosedOn AFTER INSERT ON journal_details
FOR EACH ROW BEGIN
IF
NEW.prop_key = "status_id"
AND NEW.value = "5"
OR NEW.value = "7"
THEN
UPDATE
issues i
JOIN journals j
ON j.journalized_id = i.id
SET
i.closed_on = j.created_on;
WHERE j.id = NEW.jounal_id;
END IF;
END$$
delimiter ;

You have a superfluous semicolon before the WHERE clause of your update statement.

Related

How to trigger the procedure for the sql database in system files

I have a table that holds a bunch of values for an order, I can do basic calculations on it until I get to a percentage. Right now I have my query as follows
declare #MyNumber decimal
set #MyNumber = (select SalesTax from [OrderHeader] where OrderHeaderID = 20)
select
sum(o.MaterialPrice) as "MatPrice",
sum(o.LaborPrice) as "LaborPrice",
sum(o.MaterialCost) as "MaterialCost",
sum(isnull(o.MaterialPrice,0)) - sum(isnull(o.MaterialCost,0)) - sum(isnull(o.LaborPrice,0)) * #MyNumber as "RESULT"
from [OrderDetail] o
inner join [OrderHeader] oh on oh.OrderHeaderID = o.OrderHeaderID
where o.OrderHeaderID = 20
PLEASE CHANGE AT LEAST NAZOV
DELIMITER $$
CREATE TRIGGER `nazov` BEFORE UPDATE
ON test
FOR EACH ROW BEGIN
SET NEW.OLD_TEXT=OLD.TEXT;
END $$
DELIMITER;

Mysql trigger to update the available balance after inserting records in another table

I have two tables in mysql db, one is account master table and the another one is account transaction table. On insert/update/delete on the transaction table I have to update the available balance and the last transaction date in the account master table ( the account master table contains more than 1 account). Is it possible with a trigger?
I have tried with the following Trigger. But trigger is not getting executed, getting syntax error(MSG 1064 LINE 30 MY SQL DB ERROR).
Please help to resolve if this can be handled through a trigger.
DELIMITER $$
CREATE TRIGGER wlt_bal_upd_insert AFTER INSERT ON wallet_txns
FOR EACH ROW
BEGIN
UPDATE wallet_accounts
SET wlt_bal_available = select sum(IF(wlt_txn_type = 'Expense', -wlt_txn_amount, wlt_txn_amount))from wallet_txns where wlt_name = new.wlt_name,wlt_last_txn_date = select MAX(wlt_txn_date)from wallet_txns where wlt_name = NEW.wlt_name
WHERE wlt_holder_id = NEW.wlt_holder_id
and wlt_name = new.wlt_name;
END $$
DELIMITER ;
I just forgot to put the brackets (). Its working now. Here is the modified code.
DELIMITER $$
CREATE TRIGGER wlt_bal_upd_insert AFTER INSERT ON wallet_txns
FOR EACH ROW
BEGIN
UPDATE wallet_accounts
SET wlt_bal_available = (select sum(IF(wlt_txn_type = 'Expense', -wlt_txn_amount, wlt_txn_amount))from wallet_txns where wlt_name = new.wlt_name),wlt_last_txn_date = (select MAX(wlt_txn_date)from wallet_txns where wlt_name = NEW.wlt_name)
WHERE wlt_holder_id = NEW.wlt_holder_id
and wlt_name = new.wlt_name;
END $$
DELIMITER ;

Update multiple tables with single query, if a condition is met?

How do I update 2 tables in single UPDATE SET, under some condition ?
If the condition is not met, I'd only update one table..
I'd like to do something like this:
UPDATE tab1, tab2 SET
tab1.value2=7,
CASE tab1.value1 IS NOT NULL
WHEN true
THEN tab2.value1 = tab1.value1
END
WHERE tab1.id=1 AND tab2.id = tab1.tab2_fk_id
MySQL workbench complains: Syntax Error: Unexpected CASE_SYM
I think I should do this with a TRIGGER - function
Here how you can do it,
update tab1 ,tab2
join tab2 on tab2.id = tab1.tab2_fk_id
set
tab1.value2=7,
tab2.value1 = case when tab1.value1 IS NOT NULL then tab1.value1 else tab2.value1 end
where tab1.id=1
Many thanx to Abhik Chakraborty for a brillant solution. It will be surely useful in other context. I have though decided to create a trigger in static_conveyor (~ tab1 in the above example) to achieve equal behavior. I use real tables and column names here instead of generic.. :
USE `tca`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` TRIGGER `static_conveyor_AUPD` AFTER UPDATE ON `static_conveyor` FOR EACH ROW
BEGIN
IF (OLD.signal != NEW.signal)
THEN
IF ((NEW.pallet_id IS NOT NULL) AND (NEW.default_empty IS NOT NULL))
THEN
UPDATE pallet SET empty = NEW.default_empty WHERE pallet.id = NEW.pallet_id;
END IF;
END IF;
END
Works nice ..

Assigning the value from a select statement to a vairable in MySQL

I'm fairly new to SQL in general and even more so to MySQL and I've hit a stumbling block. I'm attempting to use a procedure to copy the value of one field to another if the original field is not null, this procedure is then called by triggers whenever the table is updated or has a new row inserted into it. Here is what I have so far:
-- WORK_NOTES_PROCEDURE - This copies the contents of the estimate notes to the work order notes if the original estimate had any notes with it.
DROP PROCEDURE IF EXISTS 'WORK_NOTES_PROCEDURE';
DELIMITER $$
CREATE PROCEDURE WORK_NOTES_PROCEDURE()
BEGIN
DECLARE var_temp VARCHAR(50);
SET var_temp := (SELECT ESTIMATE_NOTES FROM ESTIMATES WHERE ESTIMATES.ESTIMATE_NUMBER = WORK_ORDERS.ESTIMATE_NUMBER);
IF var_temp IS NOT NULL THEN
UPDATE WORK_ORDERS SET WORK_ORDER_NOTES = var_temp WHERE WORK_ORDERS.ESTIMATE NUMBER = ESTIMATES.ESTIMATE_NUMBER;
END IF;
END$$
DELIMITER ;
Absolutely any help would be appreciated, the error I'm getting is a syntax error for the line where I'm assigning a value to var_temp.
try,
SET var_temp = (SELECT ESTIMATE_NOTES
FROM ESTIMATES INNER JOIN WORK_ORDERS
ON ESTIMATES.ESTIMATE_NUMBER = WORK_ORDERS.ESTIMATE_NUMBER
LIMIT 1);

MySQL nested Conditional in Update Trigger

I am trying to figure out a conditional trigger for an update to a MySQL table.
The issue is that the update trigger fires off even when the data being entered is the same as the data that was already there. So if I have a column with a row:value set up like this plus_votes:2 if I update with UPDATE votes SET plus_votes = 2; even though nothing has really changed the UPDATE trigger still gets fired off. So to prevent all of this I want to add a conditional clause to my trigger like this
BEGIN
IF NEW.vote_value <> OLD.vote_value THEN
UPDATE my_other_table
SET plus_votes =
CASE NEW.vote_value WHEN '1'
THEN plus_votes + 1
ELSE plus_votes
END,
minus_votes =
CASE NEW.vote_value WHEN '1'
THEN minus_votes
ELSE minus_votes +1
END
WHERE my_other_table.id=NEW.votes_join_id;
END
END
the second half of the issue is that I have CASE condition inside the IF statement.
Can someone help on this issiu and show how to do nested conditionals inside a TRIGGER?
much appreciated
although the syntax you have posted is completely wrong, I have figured out what you want try this ( not tested )
DROP TRIGGER IF EXISTS upd_vote;
DELIMITER $$
CREATE TRIGGER upd_vote AFTER UPDATE ON `my_other_table`
FOR EACH ROW BEGIN
IF NEW.vote_value <> OLD.vote_value THEN
SET
NEW.plus_votes = IF(NEW.vote_value=1,OLD.plus_votes+1,OLD.plus_votes),
NEW.minus_votes = IF(NEW.vote_value=1,OLD.minus_votes,OLD.minus_votes +1 )
END IF;
END$$
DELIMITER ;