mysql trigger syntax error in maria db - mysql

I was trying to make one trigger , so that i can check for any negative insertion of age .
I tried below query but its showing below error .
CREATE TRIGGER agecheck
BEFORE INSERT ON people
FOR EACH ROW
BEGIN
(CASE WHEN people.age > 0 THEN people.age ELSE 0 END )
END
ERROR CODE:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CASE WHEN people.age > 0 THEN 1 ELSE 0 END )
END' at line 5
Am i doing anything wrong in the syntax?

The statement you have in your trigger is not a statement. It's just an expression. This is not a thing you can use as a statement by itself.
I'm just guessing at what you are trying to do: make sure age is not negative. You could probably do this by declaring the column as TINYINT UNSIGNED, but if you want to do it with a trigger:
FOR EACH ROW
BEGIN
SET NEW.age = CASE WHEN NEW.age > 0 THEN NEW.age ELSE 0 END;
END
In a trigger, always reference the columns of the respective triggered row with NEW.<columnname> or OLD.<columnname>.
Another expression that works just as well:
FOR EACH ROW
BEGIN
SET NEW.age = GREATEST(NEW.age, 0);
END
There's no specific advantage for this alternative, I'm just showing another use of an expression.

Related

How to use strcmp in if statement in mysql?

I have to compare two string and process based on the result of strcmp in my sql.
sample:
Set #login='USC00010';
set #user='USC00010';
select STRCMP(#login,#user);//returns 0
if(STRCMP(#login,#user)= 0)
THEN
//process1
else
//process2
end if;
and the above code throws 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 'if(STRCMP(#login,#user)= 0)
Actual requirement:
if(//matches)
begin
if exists(select X from X )
begin
if exists(select x from x where y=a)
begin
update x
end
end
else//doesn't match
begin
//process2
end
Set #login='USC00010';
set #user='USC00010';
select if(STRCMP(#login,#user)= 0,"match","no-match") as str_compare

MySQL TRIGGER ON INSERT issue

i'm going mad with a MySQL trigger. MySQL says there's an error in the code but i can't figure out what's the problem.
this is the TRIGGER
CREATE TRIGGER UPDATE_COUNTRY
AFTER INSERT
ON `dog`FOR EACH ROW
BEGIN
IF (NEW.export=1)
THEN
IF (NEW.year > (SELECT MAX(`updated_year`) FROM `dog` WHERE `code`=NEW.origin))
THEN
UPDATE `dog` SET `updated_year`= NEW.year, `updated_month`= NEW.month WHERE `code`= NEW.origin;
ELSEIF (NEW.year = (SELECT MAX(`updated_year`) FROM `dog` WHERE `code`=NEW.origine)
AND NEW.month > (SELECT MAX(`updated_month`) FROM `dog` WHERE `code`=NEW.origine AND `updated_year`=NEW.year))
THEN UPDATE `dog` SET `updated_month`=NEW.month WHERE `code`=NEW.origin;
ELSE
RETURN NEW;
END IF;
END IF;
RETURN NEW;
END;
My SQL says
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 13
Thank you all!
Your trigger is:
AFTER INSERT ON `dog`FOR EACH ROW
And your line 13 is
UPDATE `dog` SET `updated_year`= NEW.year, `updated_month`= NEW.month WHERE `code`= NEW.origin;
This kinda suck, but you cannot update/insert/delete with a trigger on the same table that you are currently inserting/updating/deleting data because the table is locked.
This is a limitation from MySQL (yeah and not all SGDB actually have this limitation...(
You will need to work around a different way if you want to update your "dog" table. You could use a store procedure to do both the insert and the update.

MySQL Syntax error near ''

I'm a lot more used to T-SQL than MySQL and it seems as though there are slight syntax issues that I just can't quite figure out. I'm getting an error message that seems quite meaningless to me and I would really appreciate it if someone could just tell me what I'm doing wrong here. Am I perhaps not allowed to do an UPDATE in an UPDATE TRIGGER?
The idea is that I want to just keep track of whether or not my current German log has been corrected or not and record the times based on whether or not I'm updating my portion of the log or my tutor is updating their portion.
My Code is:
DELIMITER //
CREATE TRIGGER updateTimes
AFTER UPDATE ON Logs
FOR EACH ROW
BEGIN
IF (Old.TimGerman <> New.TimGerman OR
Old.TimComment <> New.TimComment)
THEN
UPDATE Logs
SET DateUpdated = CURRENT_TIMESTAMP, Corrected = 0
WHERE LogID = New.LogID;
ELSE IF (Old.TutorGerman <> New.TutorGerman OR
Old.TutorComment <> New.TutorComment)
THEN
UPDATE Logs
SET DateMarked = CURRENT_TIMESTAMP, Corrected = 1
WHERE LogID = New.LogID;
END IF;
END //
DELIMITER ;
Me error message says:
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 near '' at line 21.
Line 21 is 4th from the bottom: WHERE LogID = New.LogID;
Thanks very much for the help!
Syntactically your trigger is correct except on ELSE IF part. You need an extra END IF if you want to use it. Otherwise modify it as ELSEIF.
And also it seems you need a BEFORE trigger but not AFTER trigger.
Apart from that, calling the explicit update in an update trigger on the same table is meaning less as it would cause a circular event. Which is not supported and throws an error.
Change your trigger definition as below:
DELIMITER //
CREATE TRIGGER updateTimes BEFORE UPDATE ON Logs
FOR EACH ROW
BEGIN
IF ( OLD.LogID = New.LogID ) THEN
IF ( Old.TimGerman <> New.TimGerman OR
Old.TimComment <> New.TimComment )
THEN
SET NEW.DateUpdated = CURRENT_TIMESTAMP, NEW.Corrected = 0;
ELSEIF ( Old.TutorGerman <> New.TutorGerman OR
Old.TutorComment <> New.TutorComment )
THEN
SET NEW.DateMarked = CURRENT_TIMESTAMP, NEW.Corrected = 1;
END IF;
END IF;
END;
//
DELIMITER ;

using IF in MySQL (not the function)

I have what feels like a simple question, but can't seem to get it right. I'm just trying to execute a regular IF ... THEN ... logic, but can't seem to get it right:
set #var:=2;
if #var=1 THEN select 'hello';
I get:
ERROR 1064 (42000):
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 'if #var=1 THEN select 'hello'' at line 1
What am I missing?
You can use CASE instead.
SET #var:=2;
SELECT CASE WHEN #var=1 THEN 'hello' ELSE 'no hello' END;
--prints 'no hello'
SET #var:=2;
SELECT CASE WHEN #var:=1 THEN 'hello' ELSE 'no hello' END;
--prints 'hello'
I hope the idea is clear with above examples.
Edit: to address OP's additional concerns, You can incorporate selects in case statements, but you should enclose in brackets. For eg.
SET #var:=2;
SELECT CASE WHEN #var:=1 THEN (select 'hello') ELSE (select 'no hello') END;
One thing to notice is that it should return back only one value (from one row and a column)
You can, but only inside of functions, procedures and triggers like so:
DELIMITER //
DROP PROCEDURE IF EXISTS anyname//
CREATE PROCEDURE anyname()
BEGIN
IF #var1 = 1 THEN
SELECT 'hello';
END IF;
END//
SET #var1 := 1;
CALL anyname()//

What is wrong with this trigger?

I want to set only one row actived.
If somebody try to set the estrutura_versao_status as 1, set every other estrutura_versao_status to 0 before and keep this new row as the only one estrutura_versao_status = 1.
CREATE TRIGGER tgr_classifica_ativa_revisao BEFORE INSERT ON `sys_estrutura`
FOR EACH ROW
BEGIN
IF (NEW.estrutura_versao_status = 1) THEN
UPDATE `sys_estrutura` SET estrutura_versao_status = 0;
END IF;
END;
This is the 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
Assuming you want to set the flag for the newly inserted row, just use assign that to the NEW variable. There is no need to do an UPDATE on the table.
CREATE TRIGGER tgr_classifica_ativa_revisao BEFORE INSERT ON sys_estrutura
FOR EACH ROW
BEGIN
IF (NEW.estrutura_versao_status = 1) THEN
SET NEW.estrutura_versao_status = 0;
END IF;
END;