Synatx error near 'IF EXISTS' - mysql

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.

Related

What is wrong with my mysql stored procedure?

I have a stored procedure that checks whether or not a new entry is already existing in the table. if it exists, the insert will not happen. when I run it, there is an error
DROP PROCEDURE IF EXISTS AddPriority2;
DELIMITER $$
CREATE PROCEDURE AddPriority2
(
IN strName VARCHAR(100),
OUT itExists INT
)
BEGIN
DECLARE
SELECT COUNT(Id) INTO itExists
FROM priorities
WHERE Name = strName AND StatId = 1;
IF(itExists = 0) THEN
INSERT INTO priorities
(
NAME,
StatId
)
VALUES
(
strName,
1
);
END IF;
END
here is the error
Query: CREATE PROCEDURE AddPriority2 ( IN strName VARCHAR(100), OUT itExists INT ) BEGIN DECLARE SELECT COUNT(Id) INTO itExists FROM pr...
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 'SELECT COUNT(Id) INTO itExists
FROM priorities
WHERE Name = strName AND StatId =' at line 8
1) You cannot declare a select statement - a declare has to be for a variable..(and I would not use an output parameter for that) 2) or you can use exists instead
if not exists (select 1 from priorities WHERE Name = strName AND StatId = 1) then
insert...
end if;

MySQL IF statement in Trigger

I just want to create trigger in my sql but some error happened
this is the code
CREATE TRIGGER delete_santri_in_kamar
AFTER UPDATE ON
santri
FOR EACH ROW
BEGIN
DECLARE stat INT
SET stat = select status FROM santri WHERE id_santri=new.id_santri
IF (stat = 0) THEN
DELETE FROM santri_kamar_asrama WHERE id_santri=new.id_santri
END IF
END
and this is the error message
#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 'SET stat = select status FROM santri WHERE id_santri=new.id_santri
IF (stat =' at line 7
please help me
Add semicolons after the statements and change default delimiter(;) before the code of create trigger. Otherwise it will give
SQL Error(1064): You have an error in your SQL Syntax;
After the create trigger code make ; as default delimiter
DELIMITER $$
CREATE TRIGGER delete_santri_in_kamar
AFTER UPDATE ON
santri
FOR EACH ROW
BEGIN
DECLARE stat INT;
SET stat = (select status FROM santri WHERE id_santri=new.id_santri);
IF (stat = 0) THEN
DELETE FROM santri_kamar_asrama WHERE id_santri=new.id_santri;
END IF;
END
$$
DELIMITER ;

mysql syntax error near '', how can i correct it?

I'm using MySQL when I meet an error executing query below.
CREATE PROCEDURE insert_apply(
IN in_username VARCHAR(25),
IN in_machine_id INT
)
BEGIN
START TRANSACTION;
IF EXISTS (SELECT * FROM machine WHERE machine.id = in_machine_id AND machine.available = 1) THEN
INSERT INTO user_machine VALUES (in_username, in_machine_id);
UPDATE machine SET machine.available = 0 WHERE machine.id = in_machine_id;
END IF;
COMMIT;
END;
MySQL error is,
#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
What does " near '' " mean? How can I correct this error?
Thanks a lot.
I think these one useful to you. Actually your missed DELIMITER //
DELIMITER //
CREATE PROCEDURE insert_apply(
IN in_username VARCHAR(25),
IN in_machine_id INT
)
BEGIN
START TRANSACTION;
IF EXISTS (SELECT * FROM machine WHERE machine.id = in_machine_id AND machine.available = 1) THEN
INSERT INTO user_machine VALUES (in_username, in_machine_id);
UPDATE machine SET machine.available = 0 WHERE machine.id = in_machine_id;
END IF;
COMMIT;
END;
Thank you.

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.

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.