Issue using IF statements within a MySQL INSERT Trigger - mysql

For a uni assignment, I need to make a trigger that sets salary to a specified value if it's less than 50,000 when a new employee row is inserted into the table. Currently what I'm trying is
CREATE TRIGGER empSalInsert
BEFORE INSERT ON emp
FOR EACH ROW
IF (NEW.salary <50000) THEN
SET NEW.salary = 0
END IF;
But that gives the error
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 'END IF' at line 6
SQLState: 42000
ErrorCode: 1064
I've tried to fix the issue by both adding a semicolon after SET NEW.salary = 0 like so,
CREATE TRIGGER empSalInsert
BEFORE INSERT ON emp
FOR EACH ROW
IF (NEW.salary <50000) THEN
SET NEW.salary = 0;
END IF;
But then the error becomes
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 '' at line 5
SQLState: 42000
ErrorCode: 1064
Error occurred in:
CREATE TRIGGER empSalInsert
BEFORE INSERT ON emp
FOR EACH ROW
IF (NEW.salary <50000) THEN
SET NEW.salary = 0
I've also tried removing END IF all together,
CREATE TRIGGER empSalInsert
BEFORE INSERT ON emp
FOR EACH ROW
IF (NEW.salary <50000) THEN
SET NEW.salary = 0;
Which just spits out a similar error
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 '' at line 5
SQLState: 42000
ErrorCode: 1064
Anyone know what's going on?
I'm using SQuirrel if that helps.

You have to add DELIMITERso that mysql knows what belongs to the trigger
DELIMITER //
CREATE TRIGGER empSalInsert
BEFORE INSERT ON emp
FOR EACH ROW
BEGIN
IF (NEW.salary <50000) THEN
SET NEW.salary = 0;
END IF;
END//
DELIMITER ;

Related

Can I update just added row in MySQL with trigger based on condition?

I need to update the salary of a newly added employee in the same table if condition is satisfied. SQL keeps throwing errors about syntax, if I use delimiter change it looks like this:
delimiter //
CREATE TRIGGER fixSalary
AFTER INSERT ON emp
FOR EACH ROW
BEGIN
IF NEW.salary < 50000 THEN
UPDATE emp
SET salary = 5000
WHERE eno = NEW.eno;
END IF;
END;//
delimiter ;
But it throws an 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
'delimiter //
create trigger fixSalary
after insert on emp
for each row
begin
if ' at line 1"
I tried removing the delimiter:
CREATE TRIGGER fixSalary
AFTER INSERT ON emp
FOR EACH ROW
BEGIN
IF NEW.salary < 50000 THEN
UPDATE emp
SET salary = 5000
WHERE eno = NEW.eno;
END IF;
END;
which resulted in another 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 '' at line 8
I've tried all kinds of semicolons combinations with no luck. Can anyone help me to understand what's going on?

How to check SQL State in a if statement?

I'm trying to commit an insert in a trigger based on a sql state based on success of a previous query in a trigger. But I'm getting a syntax error.
Can anyone point out what's wrong I'm doing here.
SET #OLDTMP_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
DELIMITER //
CREATE TRIGGER `insert_exp_register` AFTER INSERT ON `trnInvDtl` FOR EACH ROW begin
insert into trninvdtl_exportregister(comp_code,
inv_type,
inv_no,
item_srl_no) values(
new.comp_code,
new.inv_type,
new.inv_no,
new.item_srl_no);
if (sqlstate='0000') then
rollback;
end if;
end//
DELIMITER ;
SET SQL_MODE=#OLDTMP_SQL_MODE;
Here's the error message for syntax error.
[Window Title]
xxxx PC: Error
[Content]
SQL Error (1064): 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 'sqlstate='0000') then
rollback;
end if;
end' at line 11
[OK]
[Footer]
Find some help on this error

Error MySQL Creating Trigger

I'm getting an odd error every time I try to create one UPDATE trigger into one of my tables...
The Trigger:
CREATE TRIGGER upd_data_nascimento BEFORE UPDATE ON pfi_pessoa_fisica
FOR EACH ROW
BEGIN
IF OLD.date_of_birth = '0000-00-00' THEN
SET NEW.date_of_birth = NULL;
END IF;
END;
And the error:
Error SQL (1064): 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 '' at line 5
Any ideas?
Make sure you are using delimiter
CREATE TRIGGER upd_data_nascimento BEFORE UPDATE ON abc_table
FOR EACH ROW
BEGIN
IF OLD.date_of_birth = '0000-00-00' THEN
SET NEW.date_of_birth = NULL;
END IF;
END
If you are executing it in PHPMyAdmin then set the delimiter to # or something else. And then execute it will work fine I have tested it with above code.

Error when creating mysql trigger

I want to create a trigger to set the value of f_IrrMinCurr based on the IrrMinCurr. This is what I wrote.
CREATE TRIGGER Fuzzification BEFORE INSERT ON fuzzyanalysis
FOR EACH ROW
BEGIN
IF NEW.IrrMinCurr < 200.0 THEN
SET NEW.f_IrrMinCurr = 'H';
ELSE
SET NEW.f_IrrMinCurr = null;
END IF;
END;
I'm getting the error,
Error
SQL query:
CREATE TRIGGER Fuzzification BEFORE INSERT ON fuzzyanalysis
FOR EACH ROW
BEGIN
IF NEW.IrrMinCurr < 200.0 THEN
SET NEW.f_IrrMinCurr = null;
MySQL said: Documentation
#1064 - 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 '' at line 5
Can anyone please help me to solve this.

Error 1064 during trigger creation

I want to create a trigger on one of the tables, it's very simple, just to set one value - but I always get error 1064
CREATE TRIGGER `trigger_TEST` BEFORE INSERT ON `TEST_table`
FOR EACH ROW
BEGIN
IF NEW.as_src = 13335 THEN
SET NEW.peer_as_src = 13335;
END IF
END
And get this error:
Error SQL (1064): 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 '' at line 5
Any idea why ?
You need to change the default delimiter
delimiter |
CREATE TRIGGER `trigger_TEST` BEFORE INSERT ON `TEST_table`
FOR EACH ROW
BEGIN
IF NEW.as_src = 13335 THEN
SET NEW.peer_as_src = 13335;
END IF;
END
|
delimiter ;
Otherwise the statement will end at the first ; which would make the trigger definition incomplete.