Right way to create IF ELSE statement in Mysql - mysql

I'm trying to insert into a table if the value doesn't exist by using IF EXISTS THEN. Everytime I try to execute this query
DELIMITER //
CREATE PROCEDURE addnTag
BEGIN
IF (EXISTS (SELECT * FROM Tag WHERE Tag.Tag = 'tt'))
THEN SELECT -1;
ELSE
INSERT INTO Tag(Tag) VALUES('tt')
SELECT last_insert_id();
END IF;
END//
DELIMITER ;
I got the
error (1064) 'BEGIN IF (EXISTS (SELECT * FROM Tag WHERE Tag.Tag =
'tt')) THEN SELECT -1; ELSE' at line 2 '
I can't find where is the error in my query.
Please help me. Thanks.

I think you are just missing the parentheses after the procedure name, and the semi-colon after your INSERT statement.
This should work:
DELIMITER //
CREATE PROCEDURE addnTag()
BEGIN
IF (EXISTS (SELECT * FROM Tag WHERE Tag.Tag = 'tt'))
THEN
SELECT -1;
ELSE
INSERT INTO Tag(Tag) VALUES('tt');
SELECT last_insert_id();
END IF;
END//
DELIMITER ;

Hello_
I don't get errors with this syntax:
DELIMITER //
DROP PROCEDURE IF EXISTS addnTag //
CREATE PROCEDURE addnTag()
BEGIN
IF (EXISTS (SELECT * FROM Tag WHERE Tag.Tag = 'tt'))
THEN SELECT -1;
ELSE
INSERT INTO Tag(Tag) VALUES('tt');
SELECT last_insert_id();
END IF;
END //
DELIMITER ;
You were missing () on function declaration and the semicolon in the end of your INSERT statement.

Related

How to use select query in stored procedure from trigger in mysql

Here is my Trigger
DELIMITER $$
CREATE TRIGGER before_insert BEFORE INSERT ON SampleTable
FOR EACH ROW
BEGIN
IF NEW.flag =0 THEN
SET NEW.media = "../trigger/smile1.png";
ELSEIF NEW.flag = 1 THEN
SET NEW.media = "../trigger/smile2.png";
ELSEIF NEW.flag = 2 THEN
SET NEW.media = "../trigger/smile3.png";
END IF;
CALL Media (NEW.flag);
END$$
DELIMITER ;
The trigger was created successfully. And I called stored procedure from a trigger.
procedure
DELIMITER $$
CREATE PROCEDURE Media (a INT)
BEGIN
SELECT firstname, lastname, media FROM SampleTable WHERE flag = a;
END $$
DELIMITER ;
When I use insert query it shows the following error.
not allowed to return a result set from a trigger
I found select query is a reason for the error. But I don't know how to solve it.
Any Help...

MySQL IF statement procedure creation error

why won't this run? Complains of error when I try to create the procedure.
CREATE PROCEDURE `xxx_testIF`
(
IN _memberId int
)
BEGIN
IF _memberId = 0
select * from incident_report
ELSE
select * from incident_report where filed_by = _memberId
END IF;
END
Always use Delimiters while creating procedures and also you have missing THEN after the IF condition.
Try this:
DELIMITER $$
CREATE PROCEDURE `xxx_testIF`
(
IN _memberId int
)
BEGIN
IF _memberId = 0 THEN
select * from incident_report;
ELSE
select * from incident_report where filed_by = _memberId;
END IF;
END$$
DELIMITER ;

Unable to execute "IF EXISTS" query

I am trying to run a SQL query, but something is messed up and I can't get below query to check if the data exists or not in the specified table.
The code I have is:
IF EXISTS(SELECT brth_day FROM sms.birthdaycheck WHERE brth_day = '2015-02-10')
THEN
BEGIN
UPDATE sms.birthdaycheck SET cnt = 1 WHERE brth_day = '2015-02-10'
END
ELSE
BEGIN
INSERT INTO sms.birthdaycheck(cnt,brth_day) VALUES (1,'2015-02-10')
END
END IF;
Edited solution :
delimiter ;;
create procedure XYZ(...parameters...)
begin
if exists(
SELECT brth_day FROM sms.birthdaycheck WHERE brth_day = '2015-
02-10')
then
UPDATE sms.birthdaycheck SET cnt = 1 WHERE brth_day = '2015-02-10';
else
INSERT INTO sms.birthdaycheck(cnt,brth_day) VALUES (1,'2015-02-10');
end if;
end;;
In MySQL, if control block must used inside a function or stored procedure.
So you need to rewrite your query:

Checking if select statement in stored procedure returns row

How does one check if a select statement inside a stored procedure returns any rows.
select * from creditcards;
If sqlcod = 0 THEN
I'd like to do something like this for example but sqlcod doesn't seem to work in MySql.
try using COUNT,
DELIMITER $$
CREATE PROCEDURE procName()
BEGIN
SET #recCount = (select count(*) from creditcards);
If #recCount = 0 THEN
-- statement here ;
END IF;
END $$
DELIMITER ;
Try using like below also:
Use FOUND_ROWS() and SQL_CALC_FOUND_ROWS:
DELIMITER $$
create procedure myproc()
begin
SELECT SQL_CALC_FOUND_ROWS *
FROM tbl_name
WHERE id > 100
LIMIT 10;
if SELECT FOUND_ROWS() = 0 then
-- log away bro!
end if;
end $$
DELIMITER $$

Cant get Mysql trigger statement right

Can someone please tell me whats wrong with this trigger statement?
DELIMITER //
CREATE TRIGGER something AFTER INSERT ON sometable
FOR EACH ROW
BEGIN
DECLARE var INT DEFAULT 0;
SET var = SELECT COUNT(*) FROM anothertable;
IF var=0 THEN
INSERT INTO anothertable values(`x`,`y`,`z`);
END IF;
END//
I keep getting error saying i have syntax errors...
Try putting the SELECT statement within brackets:
DELIMITER //
CREATE TRIGGER something AFTER INSERT ON sometable
FOR EACH ROW
BEGIN
DECLARE var INT DEFAULT 0;
SET var = (SELECT COUNT(*) FROM anothertable);
IF var=0 THEN
INSERT INTO anothertable values('x','y','z');
END IF;
END//