How to check SQL State in a if statement? - mysql

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

Related

Issue using IF statements within a MySQL INSERT Trigger

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 ;

I am trying to run query but i am not able to get right output and i am getting this error

Error Code: 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 7 0.016 sec
DELIMITER $$
create trigger postponement
BEFORE insert
on flight
for each row BEGIN
IF NEW.Time_of_depart = Flight.Time_of_depart
THEN
SELECT ADDTIME(EW.Time_of_depart, "00:30:00");
END IF;
END $$
DELIMITER ;

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.

SQL trigger giving syntax error in MySQL

DROP TRIGGER IF EXISTS demo_inc_when_viewstatus_one ;
DELIMITER $$
CREATE TRIGGER demo_inc_when_viewstatus_one AFTER UPDATE ON `tbl_ffa_demo`
FOR EACH ROW BEGIN
UPDATE `tbl_stats` SET open_demos=CASE WHEN NEW.demo_status=1 THEN
open_demos+1 ,
total_demos=total_demos+1 WHERE tbl_stats.area_id=NEW.territory AND NEW.view_status=1
END $$
DELIMITER;
The error message I received was:
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 ' total_demos=total_demos+1 WHERE tbl_stats.area_id=NEW.territory AND NE' at line 4
You did not follow the syntax for the case statement:
case when <condition> then <true branch> else <false branch> end
So, you should have something like:
... SET open_demos=CASE WHEN NEW.demo_status=1
THEN open_demos+1 ELSE open_demos=open_demos END, ...

Adding MySQL Trigger: invisible (to me) Syntax Error

NOTE: I initially thought my problem had to do with CONCAT, but since the exact error persisted even when I completely eliminated CONCAT from my query, I figured I should completely over-write of my original post.
I'm trying to set a trigger, and keep getting a Syntax Error.
When I tried this
CREATE TRIGGER set_aka_name
BEFORE INSERT ON sandbox_person
FOR EACH ROW
BEGIN
IF (NEW.aka IS NULL) THEN
SET NEW.aka = 'test value';
END IF;
END
...I got this 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 '' at line 6
And when trying this (without the IF peren's)
CREATE TRIGGER set_aka_name
BEFORE INSERT ON sandbox_person
FOR EACH ROW
BEGIN
IF NEW.aka IS NULL THEN
SET NEW.aka = 'test value';
END IF;
END
...I get the same exact 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 '' at line 6
It's got to be something simple ... but I'm just not seeing it.
I'm on MySQL 5.1.36.
What am I missing?
Any pointers?
try to change delimiter first:
DELIMITER |
CREATE TRIGGER set_aka_name
BEFORE INSERT ON sandbox_person
FOR EACH ROW
BEGIN
IF NEW.aka IS NULL THEN
SET NEW.aka = 'test value';
END IF;
END|
DELIMITER ;
Maybe MySQL sees the first ";" as the end of CREATE TRIGGER
try this:
CREATE TRIGGER set_aka_name
BEFORE INSERT ON sandbox_person
FOR EACH ROW
BEGIN
IF NEW.aka IS NULL THEN
NEW.aka = CONCAT(NEW.fname, ' ', NEW.lname);
END IF;
END
there is a difference between IF () function and IF statement (see this)