Syntax error in creating a trigger in MYSQL - mysql

I wrote a trigger which get trigered after update statement of table1. If there is no row in table1 based on the conditions, then update table2.
CREATE TRIGGER trigger1
AFTER UPDATE ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF new.column1 = 1 THEN
SELECT COUNT(1) INTO #cnt FROM my_database.table1
WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2
SET isDeleted = 1
WHERE userSeqID = new.userID;
END IF
It gives me error i nline number 4, I couldn't understand the problem

Every IF statement has to ended with END IF clause. Try this statement -
DELIMITER $$
CREATE TRIGGER trigger1
AFTER UPDATE
ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF NEW.column1 = 1 THEN
SELECT COUNT(1) INTO cnt FROM my_database.table1 WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2 SET isDeleted = 1 WHERE userSeqID = NEW.userID;
END IF;
END IF;
END$$
DELIMITER ;

The error on line 4 is because you're not using a delimiter.
Try this:
DELIMITER //
create trigger trigger1 after update on my_database.table1
for each row
begin
declare cnt int;
...
end if;
END;//
DELIMITER ;
You'll also need an END IF on the line after the select statement, and a terminal END; to match your begin, as I have in my example.

Related

how to fix error in mysql code while making count trigger after insert

i am setting triggerafter_inser and i dont know how to resolve it
DELIMITER $$
USE `solocloud_dev_2`$$
DROP TRIGGER IF EXISTS table_10_summary_after_insert $$
CREATE TRIGGER table_10_summary_after_insert AFTER INSERT ON table_10
FOR EACH ROW BEGIN
DECLARE var INT;
SET var=0;
SELECT COUNT(*) FROM table_10_summary WHERE client_id = new.client_id;
IF var > 0
THEN
UPDATE table_10_summary SET records = records+1 WHERE client_id = new.client_id;
ELSE
INSERT INTO table_10_summary (new.client_id,1);
END$$
DELIMITER$$
There are several problems with your trigger :
you need to SET var from the results of the query ; as it is now, you are not setting this value
the IF block must be terminated with END IF
The following codes compiles successfully in this DB Fiddle :
DELIMITER $$
DROP TRIGGER IF EXISTS table_10_summary_after_insert $$
CREATE TRIGGER table_10_summary_after_insert AFTER INSERT ON table_10
FOR EACH ROW
BEGIN
DECLARE var INT;
SET var = (SELECT COUNT(*) FROM table_10_summary WHERE client_id = new.client_id);
IF (var > 0) THEN
UPDATE table_10_summary SET records = records + 1 WHERE client_id = new.client_id;
ELSE
INSERT INTO table_10_summary VALUES(new.client_id,1);
END IF;
END$$
DELIMITER ;

Trigger with if else condition

I am new to MySQL trigger. I want to make a trigger that before the update is made from my emp_audit table will check first if the empno from my employees table already exists. If it exists then do the update otherwise do nothing.
I encounter the following error:
Error 1064
The code is:
DELIMITER $$
CREATE TRIGGER before_emp_audit_update
BEFORE UPDATE ON emp_audit
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM employees WHERE empno = NEW.empno) = 1
THEN
UPDATE INTO emp_audit
SET ACTION = 'update',
empno = NEW.empno,
lastname = NEW.lastname,
changedat = NOW();
END IF;
END$$
DELIMITER;
The UPDATE syntax does not include the INTO keyword.
Please check if the below updated query works.
DELIMITER $$
CREATE TRIGGER before_emp_audit_update
BEFORE UPDATE ON emp_audit
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM employees WHERE empno = NEW.empno) = 1
THEN
UPDATE INTO emp_audit
SET ACTION = 'update',
empno = NEW.empno,
lastname = NEW.lastname,
changedat = NOW()
WHERE empno = NEW.empno;
END IF;
END$$
DELIMITER;
I think, you can even remove the empno from you SET(update) list and try the below query,
DELIMITER $$
CREATE TRIGGER before_emp_audit_update
BEFORE UPDATE ON emp_audit
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM employees WHERE empno = NEW.empno) = 1
THEN
UPDATE INTO emp_audit
SET ACTION = 'update',
lastname = NEW.lastname,
changedat = NOW()
WHERE empno = NEW.empno;
END IF;
END$$
DELIMITER;

Not able to run/execute MySQL Triggers

I am creating Trigger into Mysql Workbanch , but not able to create the trigger.
Can Anyone Help me For This.
DELIMITER $$
create TRIGGER Insert_Test
After Delete ON Test
FOR EACH ROW
BEGIN
START TRANSACTION;
DECLARE v_ID INT;
DECLARE v_Error TINYINT;
DECLARE v_AgentID INT;
SELECT v_Error = 0;
v_ID=Old.id;
BEGIN
DELETE Test2 WHERE id = SELECT id FROM Test where id=v_ID;
Rollback;
SET v_Error = 1;
END
IF v_Error = 0;
THEN
COMMIT;
ELSEIF
v_Error = 1;
THEN
ROLLBACK;
END IF;
END
DELIMITER ;
Sql server Trigger
ALTER TRIGGER [dbo].[tr_DelRecordTypeID] ON [dbo].[luRecordType] FOR DELETE
AS
SET NOCOUNT ON
BEGIN TRANSACTION
DECLARE #ID INT, #GroupTypeID INT, #Error BIT, #Msg VARCHAR(500)
SELECT #Error = 0
SELECT #ID = RecordTypeID FROM deleted
SELECT #GroupTypeID = 30
IF EXISTS ( SELECT g.GroupID
FROM luGroup g,
[luGroupDetail] gd
WHERE g.[GroupID] = gd.[GroupID]
AND g.[GroupTypeID] = #GroupTypeID
AND gd.[MemberID] = #ID )
BEGIN
DELETE [agAgent] WHERE [AgentID] = (SELECT TOP 1 AgentID FROM agAgentPayType)
Rollback transaction
SET #Error = 1
END
IF #Error = 0
BEGIN
COMMIT TRANSACTION
END
ELSE
IF #Error = 1
BEGIN
ROLLBACK TRANSACTION
END
This trigger which i am trying to achieve into mysql workbanch, please check
I shall Be thankful,
Thanks
Aman

Trigger Before Insert MySql

I have a problem with my trigger before insert.
I try to control the row number of my table 'user_search' for one 'id_user' and if this row number > 4, the oldest row should be deleted.
My actual trigger is this :
DELIMITER $$
CREATE TRIGGER before_insert_user_search
BEFORE INSERT
ON user_search FOR EACH ROW
BEGIN
DECLARE sDate DATETIME;
SELECT MIN(date_search) FROM user_search WHERE id_user = NEW.id_user INTO sDate;
IF ((SELECT COUNT(id) FROM user_search WHERE id_user = NEW.id_user) > 4) THEN
BEGIN
DELETE FROM user_search WHERE date_search = sDate;
END;
END IF;
END$$
DELIMITER ;
However, it does not work. Do someone have a solution to help me please.
Thanks.
You can not delete same table row during using INSERT trigger because it holds the lock of table. So try to write a Stored Procedure to insert new record.
Stored Procedure should be something like bellow:
CREATE PROCEDURE insert_user_search(new_id_user INT, ......other variable to inert)
BEGIN
DECLARE #minId INT;
//INSERT using parameter
SET #minId = SELECT MIN(id) FROM user_search WHERE id_user = new_id_user;
IF ((SELECT COUNT(id) FROM user_search WHERE id_user = new_id_user) > 4) THEN
BEGIN
DELETE FROM user_search WHERE id_user = #minId;
END;
END IF;
END;

can't get MySQL query to work

I am trying to create a stored procedure on a Library database for practice mainly.
But it is confusing me slightly. I am using this query to create the procedure, and it says that it is working, and yet when I then call the procedure, it is saying that the procedure does not exist. Note: I am using phpmyadmin on a web database not on a local host.
delimiter $$
create procedure BorrowBook(in theBookID, in theUserID)
begin
declare lim int;
declare num int;
declare loanNumber int;
declare copyNumber int;
set lim = (select Readers.Limit from Readers where Readers.id = theUserID);
set num = (select Count(*) from Loans where Loans.UserID = theUserID);
set loanNumber = (select Count(*) from Loans) + 1;
set copyNumber = (select NumberAvailable from Book where Book.id = theBookID);
if(copyNumber > 0)
if(num<lim)
then
--Add a Loan to the Loans Table
insert into Loans values(loanNumber, theBookId, theUserID, curDate(), 0);
commit;
update Book set Book.NumberAvailable = Book.NumberAvailable - 1 where Book.id = theBookID;
select 'Succesful Update';
else
rollback;
select 'Borrow limit reached';
end if;
else
rollback;
select 'No copies available';
end;
delimiter ;
You are missing the $$ after the last end:
delimiter $$
...
end; $$ <-- add $$ here
delimiter ;
Shouldn't it be
end; $$
delimiter ;
instead of
end;
delimiter ;