IF Else in stored procedures not working - mysql

CREATE PROCEDURE p1
(
IN name_val VARCHAR(255),
OUT iJobID INT
)
BEGIN
IF NOT EXISTS (SELECT id FROM test WHERE id='11')
BEGIN
INSERT INTO test(name) VALUES(name_val);
SET iJobID := LAST_INSERT_ID();
END
ELSE
BEGIN
UPDATE test SET name=name_val WHERE id = 11;
END
INSERT INTO vasu2(vname) VALUES(name_val);
SET #ivD := LAST_INSERT_ID();
INSERT INTO vasu(id, id2) VALUES(iJobID, #ivD);
END;

the IF syntax you are using is most likely for T-SQL. Also, you need to change the delimiter.
DELIMITER $$
CREATE PROCEDURE p1
(
IN name_val VARCHAR(255),
OUT iJobID INT
)
BEGIN
SET #recCount := (SELECT COUNT(*) FROM test WHERE id = 11);
IF #recCount > 0 THEN
INSERT INTO test(name) VALUES(name_val);
SET iJobID := LAST_INSERT_ID();
ELSE
UPDATE test SET name=name_val WHERE id = 11;
END IF;
INSERT INTO vasu2(vname) VALUES(name_val);
SET #ivD := LAST_INSERT_ID();
INSERT INTO vasu(id, id2) VALUES(iJobID, #ivD);
END $$
DELIMITER ;
Using Stored Procedures

You forgot about: delimiter, then, end if
Hope it helps
delimiter $$
CREATE PROCEDURE test.p1
(
IN name_val VARCHAR(255),
OUT iJobID INT
)
BEGIN
IF NOT EXISTS (SELECT id FROM test WHERE id='11') THEN
INSERT INTO test(name) VALUES(name_val);
SET iJobID := LAST_INSERT_ID();
ELSE
UPDATE test SET name=name_val WHERE id = 11;
END IF;
INSERT INTO vasu2(vname) VALUES(name_val);
SET #ivD := LAST_INSERT_ID();
INSERT INTO vasu(id, id2) VALUES(iJobID, #ivD);
END
$$
DELIMITER ;

i think IF not exists does not exist in mysql then

Related

Trigger+procedure issue

As the title says, I have an issue with the trigger + procedure.
What I'm trying to do it's a procedure + trigger that creates an email with the name and surnames only if when I'm trying to insert a student data w/o an email.
Basically where am stuck is, the code makes the fusion of name and surname and creates the email, but how do I place it before it inserts inside the table?
Best regards,
Engineer
Here's the code I have made:
use alumnesTriggers;
drop procedure if exists ex2;
delimiter //
create procedure ex2 (in nom varchar(50), in cognom1 varchar(50),
in cognom2 varchar(50), in domini varchar(50), out email varchar(50))
begin
set email := concat(nom,cognom1,cognom2,'#',domini,'.com');
#set #email := email;
#UPDATE alumnesEmail as ae SET ae.email = #email WHERE nom = #nom;
end //
delimiter ;
drop trigger if exists ex2_2_trigger;
DELIMITER $$
CREATE TRIGGER ex2_2_trigger before insert on alumnesEmail for each row begin
set #mail := new.email;
#if #mail := null or #mail='' then
set #id := new.id;
set #nom := new.nom;
set #cognom1 := new.cognom1;
set #cognom2 := new.cognom2;
call ex2(#nom,#cognom1,#cognom2,'gmail',#email);
UPDATE alumnesEmail SET new.email = #email;
#end if;
END $$
DELIMITER ;
INSERT INTO `alumnesEmail` (`id`, `nom`, `cognom1`, `cognom2`, `email`)
VALUES (80, 'a', 'a', 'a',null);
select * from alumnesEmail where id = 80;
select #email;
select * from alumnesEmail ;
but how do I place it before it inserts inside the table?
In a before trigger, you just set new.email to the target value. This modifies the value that is about to be written to the database.
Also, the procedure seems superfluous here.
The following code should do just what you want:
delimiter $$
create trigger ex2_2_trigger
before insert on alumnesemail
for each row
begin
if new.email is null or new.email = '' then
set new.email = concat(new.nom, new.cognom1, new.cognom2, '#gmail.com');
end if;
end $$
delimiter ;

Stored Procedure with syntax error

i have the following code that is giving me a headache, i need to create this stored procedure for a database, however, i can save it nor implement it due to a Syntax error at 'AS'. I need to know what am i doing wrong?
Create PROCEDURE `insert_delete_update` (OUT
id int,
level varchar(225),
action varchar(20)
)
AS
BEGIN
SET NOCOUNT ON;
IF #Action = 'Insert'
BEGIN
insert into levels (id, level) values(#id, #level)
END
IF #Action = 'Select'
BEGIN
select * from levels
END
IF #Action = 'Update'
BEGIN
UPDATE levels SET
level = #level
WHERE id = #id
END
else IF #Action = 'Delete'
BEGIN
DELETE FROM levels WHERE id = #id
END
END
Right so i finally got my answer.
Here is the final code:
CREATE DEFINER=`root`#`localhost` PROCEDURE `insert_delete_update`()
BEGIN
Declare id int;
Declare level varchar(225);
Declare action varchar(20);
Set #Action = 'Insert';
BEGIN
insert into levels (id, level) values(#id, #level);
END;
SET #Action = 'Select';
BEGIN
select * from levels
END ;
SET #Action = 'Update';
BEGIN
UPDATE levels SET
level = #level
WHERE id = #id;
END;
SET #Action = 'Delete';
BEGIN
DELETE FROM levels WHERE id = #id;
END;
END;
END
Thx for all your help!
I have written stored procedure for you. You can use it and I hope it will resolve your problem. I have created from workbench only.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `insert_delete_update`(OUT
id int,
level varchar(225),
action varchar(20))
BEGIN
if #action = 'Insert' THEN
insert into levels (id, level) values(#id, #level) ;
elseif #action = 'Select' THEN
select * from levels ;
elseif #action = 'Update' THEN
UPDATE levels SET
level = #level
WHERE id = #id;
elseif #action = 'Delete' THEN
DELETE FROM levels WHERE id = #id;
end if;
END$$
DELIMITER ;

Executing query in MySQL Trigger not working

I have two table 'testing1' with fields 'firstname' and 'lastname'. Another table 'testing2' with field 'firstname'.
So the trigger checks first whether the 'NEW.firstname' exists in 'testing2' table or not. If it does then it doesn't execute the INSERT query but if it doesn't exist then the INSERT query is executed and 'NEW.firstname' is added in 'testing2' table.
Here's the trigger that i created ... but I'm getting error in the IF loop ...
DELIMITER $$;
CREATE TRIGGER testRef AFTER INSERT ON testing1
FOR EACH ROW
BEGIN
DECLARE rowCount INTEGER;
SET #rowCount := ( SELECT COUNT(firstname) FROM testing2 WHERE testing2.firstname = NEW.firstname );
IF (rowCount)
INSERT INTO testing2 (firstname) VALUES (NEW.firstname);
END $$
I'm unable to figure out where did I made the mistake... Any help ??
Try
DELIMITER $$;
CREATE TRIGGER testRef AFTER INSERT ON testing1
FOR EACH ROW
BEGIN
DECLARE rowCount INTEGER;
SET #rowCount := ( SELECT COUNT(firstname) FROM testing2 WHERE testing2.firstname = NEW.firstname );
IF (rowCount) THEN
INSERT INTO testing2 (firstname) VALUES (NEW.firstname);
END IF;
END$$
DELIMITER ;
But maybe you shoud use IF (rowCount)>0
This worked for me :
DELIMITER //
CREATE TRIGGER testRef AFTER INSERT ON testing1
FOR EACH ROW
BEGIN
DECLARE rowCount INTEGER;
SELECT COUNT(firstname) FROM testing2 WHERE testing2.firstname = NEW.firstname INTO rowCount;
IF rowCount = 0 THEN
INSERT INTO testing2 (firstname) VALUES (NEW.firstname);
END IF;
END //
DELIMITER ;

Syntax error in creating a trigger in MYSQL

I wrote a trigger which get trigered after update statement of table1. If there is no row in table1 based on the conditions, then update table2.
CREATE TRIGGER trigger1
AFTER UPDATE ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF new.column1 = 1 THEN
SELECT COUNT(1) INTO #cnt FROM my_database.table1
WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2
SET isDeleted = 1
WHERE userSeqID = new.userID;
END IF
It gives me error i nline number 4, I couldn't understand the problem
Every IF statement has to ended with END IF clause. Try this statement -
DELIMITER $$
CREATE TRIGGER trigger1
AFTER UPDATE
ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF NEW.column1 = 1 THEN
SELECT COUNT(1) INTO cnt FROM my_database.table1 WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2 SET isDeleted = 1 WHERE userSeqID = NEW.userID;
END IF;
END IF;
END$$
DELIMITER ;
The error on line 4 is because you're not using a delimiter.
Try this:
DELIMITER //
create trigger trigger1 after update on my_database.table1
for each row
begin
declare cnt int;
...
end if;
END;//
DELIMITER ;
You'll also need an END IF on the line after the select statement, and a terminal END; to match your begin, as I have in my example.

Call procedure inside trigger in mysql

I want to update counter table when user table is modofied.
I have created a trigger where i will cal this procedure.
But this procudeure is not creating.
Please tell me what error in this procedure.
CREATE PROCEDURE barproc(IN id int,IN val int)
BEGIN
DECLARE #total_products int;
set #total_products=(SELECT COUNT(*)
FROM user where a=id and status='active')
if(#total_products>0)
update counter
set b=val
WHERE a = id and status='active';
END if;
Try
DELIMITER $$
CREATE PROCEDURE barproc(IN id INT, IN val INT)
BEGIN
IF (SELECT COUNT(*)
FROM `user`
WHERE a = id AND `status` = 'active') THEN
UPDATE counter
SET b = val
WHERE a = id AND `status` = 'active';
END IF;
END$$
DELIMITER ;