What is wrong with mysql query? - mysql

I use the following mysql query,
DELIMITER $$
DROP PROCEDURE IF EXISTS `allied`.`aboutus_delete`$$
CREATE DEFINER=`allied`#`%` PROCEDURE `aboutus_delete`(
IN p_Id int(11)
)
BEGIN
if exists( select aboutUsId
from aboutus
where aboutUsId=p_id
and isDeleted=0
)
update aboutus set isDeleted=1 where aboutUsId=p_id
else
select 'No record to delete'
END$$
DELIMITER ;
But i get this error when i execute it...
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
'update aboutus set isDeleted=1 where aboutUsId=p_id
else
select 'No record to' at line 6
EDIT:
using semicolon doesn't seem to work,
if exists(select aboutUsId from aboutus where aboutUsId=p_id and
isDeleted=0) then
update aboutus set isDeleted=1 where aboutUsId=p_id;
else
select 'No record to delete';

This is a different issue: you can optimize this procedure a bit. Why hit the datastore twice when one query will do? Just set the isDeleted attribute to 1 and check the row_count value afterwards.
BEGIN
UPDATE aboutus SET isDeleted = 1 WHERE aboutUsId = p_id AND isDeleted = 0;
IF (SELECT row_count()) <= 0 THEN
SELECT 'No record to delete';
END IF;
END

You missed the 'THEN' in 'IF'...

Along with the semicolons and THEN, you are missing END IF to terminate the IF statement.

Related

How to get value from select in trigger mysql and use it for condition of IF

DELIMITER $$
CREATE TRIGGER cek
AFTER INSERT ON lapor_karya
FOR EACH ROW
BEGIN
SET #var = (SELECT COUNT(lapor_karya.ID_Lapor) FROM lapor_karya GROUP BY NEW.ID_Karya);
IF(#var > 10)
THEN
DELETE * FROM karya_pelajar WHERE ID_Karya=NEW.ID_Karya;
END IF;
END $$
DELIMITER ;
error
#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 '* FROM karya_pelajar WHERE ID_Karya=NEW.ID_Karya;
END IF;
END' at line 9
I want to get value from lapor_karya table, and use this value for condition IF. If my value more than 10, then will execute query delete. But it's doesn't working
The error which you got about the DELETE statement. You have to leave * when you writing DELETE statement.
Also, just try without parentheses.
IF #var > 10 THEN
DELETE FROM karya_pelajar WHERE ID_Karya=NEW.ID_Karya;
END IF;
For more info, check this out MySQL documentation.
A slight simplification:
BEGIN
IF ( SELECT COUNT(*) FROM lapor_karya
WHERE ??? = NEW.ID_Karya
) > 10
THEN
DELETE * FROM karya_pelajar WHERE ID_Karya=NEW.ID_Karya;
END IF;
END $$
That is, a parenthesized SELECT that returns a single value can be used virtually any place where an expression can be used.

Synatx error near 'IF EXISTS'

I want to solve an error.
While i am exicuting this query i met with an error.
IF EXISTS (SELECT * FROM category_info WHERE category_name = 'Electronics')
BEGIN
select * from category_info;
END
ELSE
BEGIN
insert into category_info(category_name) values('Electronics');
END
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 'IF EXISTS (SELECT * FROM category_info WHERE category_name =
'Electronics') BEG' at line 1
I need help,Thanks in advance
It can be solved by using procedure.
delimiter $$
CREATE PROCEDURE Check_exists(IN categoryName varchar(20))
BEGIN
IF EXISTS(SELECT * FROM category_info WHERE category_name = categoryName)
THEN
SELECT 'Already Exists' as status;
ELSE
INSERT INTO category_info(category_name) VALUES(categoryName);
END IF;
END $$
and this procedure can be called by
call Check_Exists('Electronics');
It will display message "Already exists " if it is exists or else it will insert a row.

MySQL said: Error #1064 - while using IF condition in sql

I got this error msg in my sql command. i am unable to figure out the cause. please help me.
if ( SELECT * FROM `teams` WHERE `client_id`='3' and `member_id`='6' and `current`='1' ) then
begin
UPDATE `teams` SET `current`='0' WHERE `client_id`='3' and `member_id`='6' and `current`='1'
end ;
else
begin
INSERT INTO `teams`(`client_id`, `member_id`) VALUES ('3','33')
end;
end if;
here i'm trying to update column current if set to 1, else insert a new record.
I need to use this sql command in my php file when a form is submitted.
below is the error message:
#1064 - You have an error in your SQL syntax;
This is not a valid statement in mysql, the valid syntax would be as below. Also if-else is allowed in stored procedure, functions or triggers.
if ( SELECT * FROM `teams` WHERE `client_id`='3' and `member_id`='6' and `current`='1' ) then
begin
UPDATE `teams` SET `current`='0' WHERE `client_id`='3' and `member_id`='6' and `current`='1'
end ;
else
begin
INSERT INTO `teams`(`client_id`, `member_id`) VALUES ('3','33')
end;
end if;
The whole set of statements can be done with one INSERT...ON DUPLICATE KEY UPDATE... statement.

MySQL trigger: error when checking if variable exists

I have a trigger which references a global variable that's coming from a delete query which sets the trigger off:
DELIMITER ;;
CREATE TRIGGER test_trigger BEFORE DELETE ON test_table FOR EACH ROW
BEGIN
IF (SELECT #userID IS NULL)
THEN #userID := 0
END IF;
END
;;
DELIMITER ;
For some reason I'm getting SQL error:
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 '#userID := 0
I don't understand this, it's a very basic IF statement, why doesn't it work?
UPDATE: I'm totally confused, I've tried this
IF TRUE
THEN TRUE
END IF;
and it still throws an error... Seriously?
UPDATE 2: SOLVED This actually works, however it is very weird
IF (SELECT #userID IS NULL)
THEN SET userID = 0;
END IF;
Apparently you need a semicolon inside IF statement?
CREATE TRIGGER test_trigger BEFORE DELETE ON test_table FOR EACH ROW
BEGIN
IF (SELECT #userID IS NULL)
THEN
SET #userID = 0;
END IF;
END;

Why won't simple If ELSE Statement work in mySql

I'm trying to create a simple stored procedure with an if else statement in SQLYog against a mySql db. I'm not overly familiar with mySql syntax so i'm hoping it's something simple but I just can't see why this isn't working
CREATE PROCEDURE p(IN Number INT)
IF NUMBER = 1 THEN
SELECT * FROM tblProduct WHERE ProductID = Number
ELSE SELECT * FROM tblProduct WHERE ProductId = 2
END IF
I'd appreciate if anyone can help me with this and tell me where i'm going wrong.
Thanks for reading.
I get the following when I try to execute:
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 'ELSE SELECT * FROM tblProduct where intProductId = 2
END IF' at line 5
Statements in MySQL are delimited by semicolons. To create procedures with them, you do a little trick like so:
DELIMITER //
CREATE PROCEDURE p(IN Number INT)
BEGIN
IF NUMBER = 1 THEN
SELECT * FROM tblProduct WHERE ProductID = Number;
ELSE
SELECT * FROM tblProduct WHERE ProductId = 2;
END IF;
END //
DELIMITER ;
Check out the documentation for if/else for more info.
Remember the IF ELSE statement is always used in stored procedure, triggers not in simple select query. And ELSE OR IF keyword always write in new line not in front of query.Like below
Correct syntax:
DELIMITER //
CREATE PROCEDURE NAME(IN Number INT)
BEGIN
IF roll= 1 THEN
SELECT * FROM table1 WHERE id = roll;
ELSE
SELECT * FROM table2 WHERE id = 2;
END IF;
END //
DELIMITER ;
Wrong syntax:
DELIMITER //
CREATE PROCEDURE NAME(IN Number INT)
BEGIN
IF roll= 1 THEN SELECT * FROM table1 WHERE id = roll;
ELSE SELECT * FROM table2 WHERE id = 2;
END IF;
END //
DELIMITER ;
You need a ; after your select statements. You also need BEGIN and END around your procedure body. See the manual for lots of examples about the exact syntax for procedures.