Trigger in mysql updating in another database - mysql

is there something wrong with this trigger ? It suppose to update a datefield on a table in another database. Can anyone give me a hand ? thanks
DROP TRIGGER IF EXISTS depoisDeInserir_invoice;
DELIMITER $$
CREATE TRIGGER depoisDeInserir_invoice AFTER INSERT ON pag_invoice
FOR EACH ROW
BEGIN
DECLARE trial datetime;
SET trial = (select dataTrial
from networkcen_1.companyproductuser
where (CompanyKey = NEW.companyKey)
and (ProductKey = NEW.productKey)
and (UserKey = NEW.productKey));
IF (trial < CURDATE()) THEN
UPDATE networkcen_1.companyproductuser
SET dataVencimento = DATE_ADD(CURDATE(),INTERVAL NEW.dias DAY)
where (CompanyKey = NEW.companyKey)
and (ProductKey = NEW.productKey)
and (UserKey = NEW.productKey);
ELSE
UPDATE networkcen_1.companyproductuser
SET dataVencimento = DATE_ADD(trial,INTERVAL NEW.dias DAY)
where (CompanyKey = NEW.companyKey)
and (ProductKey = NEW.productKey)
and (UserKey = NEW.productKey);
END IF;
END;
$$
DELIMITER ;

Related

Update Data When Trigger Execute in Mysql?

I want WaterRateMeterStart with WaterRateMeterEnd have same value sequentially if data updated.
This is my table
And This is my table structure
This my code and
I do not know how mistakes can occur.
DELIMITER $$
CREATE TRIGGER after_update_rates_water_again AFTER UPDATE ON waterrates FOR EACH ROW
BEGIN
IF
new.WaterRateMeterStart <> old.WaterRateMeterStart THEN
UPDATE waterrates
SET WaterRateMeterEnd = new.WaterRateMeterStart
WHERE
WaterRateId = ( new.WaterRateId - 1 );
ELSEIF new.WaterRateMeterEnd <> old.WaterRateMeterEnd THEN
UPDATE waterrates
SET WaterRateMeterStart = new.WaterRateMeterEnd
WHERE
WaterRateId = ( new.WaterRateId + 1 );
END IF;
END $$
DELIMITER ;
And error if I updated the data
UPDATE `waterrates` SET `WaterRateMeterStart` = '9' WHERE `waterrates`.`WaterRateId` = 2

generated column with condition sql

mysql command to generate a column from other column according to condition
loan_amount | installment | start_date | status
------------------------------------------------------------------
500 | 100 | 2018-1-1 |
if (loan_amount % installment != 0) then month_required to pay = loan_amount / installment + 1
else month_required = loan_amount / installment;
then i want to check by adding month_required to start_date if it has crossed current date. If not then status would generate "incomplete" else "complete" .
delimiter //
set #month = "0";
create trigger `status_check` before insert on `loan`
for each row
begin
if NEW.loan_amount% NEW.installment != 0
then set #month NEW.loan_amount/ NEW.installment +1;
else set #month = NEW.loan_amount/NEW.installment;
end if ;
if NOW() <= dateadd(NEW.start_date,INTERVAL #month MONTH)
then set NEW.status = "incomplete";
else set NEW.status = "complete";
end if;
end;
//
DELIMITER ;
what is the error here? please help.
I Can't make the real answer as you don't provide all your database field nor a data sample, but some BEFORE INSERT and BEFORE UPDATE Trigger will probably do the job.
This is the base for the BEFORE INSERT :
DELIMITER //
DROP TRIGGER IF EXISTS `trg_test_insert`;
//
CREATE TRIGGER `trg_test_insert`
BEFORE INSERT ON `table`
FOR EACH ROW
BEGIN
IF NEW.loan_amount % NEW.installment != 0 THEN
SET NEW.month_required = NEW.loan_amount / NEW.installment + 1;
ELSE
SET NEW.month_required = NEW.loan_amount / NEW.installment;
END IF;
END;
//
DELIMITER ;
Take a look at MySQL 5.7 Reference Manual / ... / Trigger Syntax and Examples
And at :
MYSQL Triggers - how to store the result of a calculated field

Trigger to check whether the record exists or not before insert in mysql

Am trying to write a trigger before inserting into a table and also I need to check if the record exists then the insert should not happen.
DELIMITER $$
DROP trigger IF EXISTS `Before_Insert_igm_vessel_details` $$
CREATE TRIGGER `Before_Insert_igm_vessel_details` BEFORE INSERT ON igm_vessel_details
FOR EACH ROW
BEGIN
IF (NOT EXISTS(SELECT * FROM igm_vessel_details WHERE VD_MESSAGE_TYPE = NEW.VD_MESSAGE_TYPE and
VD_CUSTOM_HOUSE_CODE = NEW.VD_CUSTOM_HOUSE_CODE and VD_IGM_NO = NEW.VD_IGM_NO and VD_IGM_DATE = NEW.VD_IGM_DATE and
VD_IMO_CODE_OF_VESSEL = NEW.VD_IMO_CODE_OF_VESSEL and VD_VESSEL_CODE = NEW.VD_VESSEL_CODE and VD_SHIPPING_LINE_CODE = VD_SHIPPING_LINE_CODE and
VD_SHIPPING_AGENT_CODE = NEW.VD_SHIPPING_AGENT_CODE and VD_MASTER_NAME = NEW.VD_MASTER_NAME and VD_PORT_OF_ARRIVAL = NEW.VD_PORT_OF_ARRIVAL and
VD_LAST_PORT_CALLED = NEW.VD_LAST_PORT_CALLED and VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL1 = NEW.VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL1 and
VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL2 = NEW.VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL2 and VD_VESSEL_TYPE = NEW.VD_VESSEL_TYPE and VD_TOTAL_NO_OF_LINES = NEW.VD_TOTAL_NO_OF_LINES
and VD_BRIEF_CARGO_DESCRIPTION = NEW.VD_BRIEF_CARGO_DESCRIPTION and VD_EXPECTED_ARRIVAL_DATETIME = NEW.VD_EXPECTED_ARRIVAL_DATETIME and VD_LIGHT_HOUSE_DUES = NEW.VD_LIGHT_HOUSE_DUES
and VD_SAME_BOTTOM_CARGO = NEW.VD_SAME_BOTTOM_CARGO and VD_SHIP_STORES_DECLARATION = NEW.VD_SHIP_STORES_DECLARATION and VD_CREW_LIST_DECLARATION = NEW.VD_CREW_LIST_DECLARATION and VD_PASSENGER_LIST = NEW.VD_PASSENGER_LIST
and VD_CREW_EFFECT_DECLARATION = NEW.VD_CREW_EFFECT_DECLARATION and VD_MARITIME_DECLARATION = NEW.VD_MARITIME_DECLARATION and VD_TERMINAL_OPERATOR_CODE = NEW.VD_TERMINAL_OPERATOR_CODE)) THEN
INSERT INTO fifo_test.igm_vessel_details (VD_MESSAGE_TYPE, VD_CUSTOM_HOUSE_CODE, VD_IGM_NO, VD_IGM_DATE, VD_IMO_CODE_OF_VESSEL,
VD_VESSEL_CODE,VD_SHIPPING_LINE_CODE,VD_SHIPPING_AGENT_CODE,VD_MASTER_NAME,VD_PORT_OF_ARRIVAL,
VD_LAST_PORT_CALLED,VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL1,VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL2,VD_VESSEL_TYPE,
VD_TOTAL_NO_OF_LINES,VD_BRIEF_CARGO_DESCRIPTION,VD_EXPECTED_ARRIVAL_DATETIME)
VALUES (NEW.VD_MESSAGE_TYPE, NEW.VD_CUSTOM_HOUSE_CODE, NEW.VD_IGM_NO, NEW.VD_IGM_DATE, NEW.VD_IMO_CODE_OF_VESSEL,
NEW.VD_VESSEL_CODE,NEW.VD_SHIPPING_LINE_CODE,NEW.VD_SHIPPING_AGENT_CODE,NEW.VD_MASTER_NAME,NEW.VD_PORT_OF_ARRIVAL,
NEW.VD_LAST_PORT_CALLED,NEW.VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL1,NEW.VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL2,NEW.VD_VESSEL_TYPE,
NEW.VD_TOTAL_NO_OF_LINES,NEW.VD_BRIEF_CARGO_DESCRIPTION,NEW.VD_EXPECTED_ARRIVAL_DATETIME);
END IF;
END$$
DELIMITER ;
the above script is not working.
regardless to the mysql documentation, the trigger must fails and then the insert operation will be canceled.1
try to make a signal sqlstate

Mysql current date trigger

I have the following trigger
CREATE TRIGGER `qc_date_trigger` AFTER UPDATE ON `brand`
FOR EACH ROW BEGIN
IF NEW.brandQC = '1' THEN
SET #brandQCDate = CURDATE();
ELSE
SET #brandQCDate = NULL;
END IF;
END
For some reason it does not update my Date field into the current date when QC is = 1. I've checked the mysql doc and it should work. Any ideeas?
If your column is named brandQCDate, then remove the #. The # makes it a user defined variable, not the column. Also you want to make this a BEFORE UPDATE trigger.
CREATE TRIGGER `qc_date_trigger` BEFORE UPDATE ON `brand`
FOR EACH ROW BEGIN
IF NEW.brandQC = '1' THEN
SET NEW.brandQCDate = CURDATE();
ELSE
SET NEW.brandQCDate = NULL;
END IF;
END

mysql stored procedure syntax error

Ok, this is only the second stored procedure I've written. I think you'll get the idea, I'm trying to close a credit line, and all invoices, charges, notes, etc with it. But I get a syntax error.
The goal is to CALL close_account_proc(398985994)
DELIMITER $$
CREATE
PROCEDURE `cc`.`close_account_proc`(cid INT)
#uid_usr := uid_usr FROM credit_acc WHERE type_acc = 'init' AND credit_used_acc = cid;
UPDATE credit_acc SET status_acc = 'closed', void_date_acc = NOW() WHERE credit_used_acc = cid;
UPDATE payment_acc SET status_acc = 'voided', void_date_acc = NOW() WHERE creditid_acc = cid;
UPDATE sbal_sbl SET status_sbl = 'voided', void_date_sbl = NOW() WHERE credit_used_acc = cid;
INSERT INTO notes_not SET uid_usr = #uid_usr, initials_not = 'SYS',status_not = 'complete', date_not = NOW(), text_not = 'Closed credit line '.cid;
UPDATE invoices_inv SET status_inv = 'voided', void_date_inv = NOW() WHERE credit_used_acc = cid;
BEGIN
END$$
DELIMITER ;
So, anway, I get this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '#uid_usr := uid_usr from credit_acc where type_acc = 'init' and credit_used_acc ' at line 5.
Any ideas?
DELIMITER $$
CREATE PROCEDURE `cc`.`close_account_proc`(cid INT)
BEGIN
/* Check that it's what you wanted */
SELECT uid_usr
INTO #uid_usr
FROM credit_acc
WHERE type_acc = 'init'
AND credit_used_acc = cid;
UPDATE credit_acc SET status_acc = 'closed', void_date_acc = NOW() WHERE credit_used_acc = cid;
UPDATE payment_acc SET status_acc = 'voided', void_date_acc = NOW() WHERE creditid_acc = cid;
UPDATE sbal_sbl SET status_sbl = 'voided', void_date_sbl = NOW() WHERE credit_used_acc = cid;
/* Check that it's what you wanted */
INSERT
INTO notes_not (uid_usr, initials_not, status_not, date_not, text_not)
VALUES (#uid_usr, 'SYS', 'complete', NOW(), CONCAT('Closed credit line ', cid));
UPDATE invoices_inv SET status_inv = 'voided', void_date_inv = NOW() WHERE credit_used_acc = cid;
END
$$
DELIMITER ;
Hmm, im no expert at stored procedures, but isn't it.
CREATE PROCEDURE `cc`.`close_account_proc`(cid INT)
BEGIN
// your stuff
END$$