Why won't simple If ELSE Statement work in mySql - 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.

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.

How to run simple stored procedure in mysequel pro or mysql workbench

I try to run this, but I get error.
set #var1 = 'AAA' ;
IF #var1 = 'AAA' THEN
Select * from List limit 2
END IF;
I am trying to run this in MAC's MySequel Pro, or Mysql Workbench.
I get syntax error. Can't say why.
I am not very familiar with stored procedure syntax.
Google search also not helped.
Do I use a ; End BEGIN, ??? tried all, but no luck.
How is the syntax, when I should use ;
When I should use Begin and END ??
Appreciate your help.
IF #var1 == 'AAA' THEN is wrong and so is the syntax error. It should just be below. Your's is not a compound statement and thus don't need a Begin .. end block. Refer MySQL Documentation On IF Syntax
IF #var1 = 'AAA' THEN
Select * from List limit 2
END IF;
Your SQL statement missing a semicolon at the end. It should be:
Select * from List limit 2;
And this just for a sample of stored procedure:
delimiter $$
use `your_database`$$
drop procedure if exists `SP_Name`$$
create definer=`root`#`localhost` procedure `SP_Name`()
begin
set #var1 = 'AAA';
IF #var1 = 'AAA' THEN
Select * from List limit 2;
END IF;
end$$
delimiter ;
For more info, check MySQL Documentation or MySQL Stored Procedure Tutorial example

mysql Stored function not working

I need to select the first flight of today. For that I'm using
delimiter//
CREATE function get_flightID(flightident())
returns int
begin
return (SELECT * FROM flightdep where depday = dayofweek(CURDATE()) ORDER BY depTime asc LIMIT 1;)
end//
delimiter;
But this isn't working. When I execute this I got an error about MySQL syntax.
17:50:50 ) end// delimiter 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 ') end// delimiter' at line 1 0.000 sec
I'm really guessing what to use in my first line, I tried everything I could think of.
CREATE function get_flightID(flightident())
First, you are returning select *, which would presumably have more than one column. Second, you have an unnecessary semicolon. Try something like this:
delimiter//
CREATE function get_flightID(flightident())
returns int
begin
return (SELECT deptime
FROM flightdep
where depday = dayofweek(CURDATE())
ORDER BY depTime asc
LIMIT 1
)
end//
delimiter;
You're attempting to return a resultset (that is, SELECT *...) from your function, but you declared it to return an int.
You want something like this:
DELIMITER $$
DROP FUNCTION IF EXISTS get_flightID $$
CREATE function get_flightID()
returns int
reads sql data
begin
declare result int;
SELECT flightID INTO result
FROM flightdep
where depday = dayofweek(CURDATE())
ORDER BY depTime asc LIMIT 1;
return result;
end$$
DELIMITER ;

MySql Stored procedure shows as error in syntax

I am new to creating procedures in mysql, i know how to create them in MSSQL, but i am not sure what is wrong with this, it says Syntax Error Near END
CREATE PROCEDURE GetNameByID(IN CustID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = CustID
END
The query in your procedure needs a semi colon after it:
CREATE PROCEDURE GetNameByID(IN CustID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = CustID;
END
You may also need to set the delimiter to something. The MySQL documentation does this:
DELIMITER //
CREATE PROCEDURE GetNameByID(IN CustID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = CustID;
END//
(but obviously not with your query)
You are missing the ; at the end of select statement

What is wrong with mysql query?

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.