IF NOT EXISTS issue in stored procedure - mysql

I have to create a stored procedure to add info into the Comments table, then add the commenters name into the Commenters name if it does not exist. I've been tweaking the code for quiet awhile now, and keep getting a syntax error at line 11.
Not sure I understand how to use IF NOT EXISTS syntax.
DELIMITER //
CREATE PROCEDURE AddComment(Name VARCHAR(60), Title VARCHAR(60), Comments VARCHAR(60))
BEGIN
INSERT INTO Comments(Name, Title, Comments)
VALUES (Name, Title, Comments);
IF NOT EXISTS(SELECT Name
FROM Commenters
WHERE Name = Name)
UPDATE Commenters
SET Name = Name;
END IF;
END;
//

Related

stored procedure in mysql returning null

I have the following table created in mysql database.
create table stud_info(Student_ID int,Name varchar(10),Class varchar(10),Marks int)
I have also created a stored procedure to retrieve the name given the id like below:
DELIMITER //
create procedure selectEmp2(IN num1 INT,OUT name varchar(20))
BEGIN
select Name INTO name from myDB.stud_info where Student_ID=num1;
END//
When I am calling the stored procedure , I am getting null value. Please let me know where I am going wrong.
I think your stored procedure should work, but I would advise giving names to parameters that are likely to be unique. I also prefer explicit variable assignment, because select into can mean different things. Does this work?
DELIMITER //
create procedure selectEmp2(IN in_num1 INT, OUT out_name varchar(20))
BEGIN
select si.Name into out_name
from myDB.stud_info si
where si.Student_ID = in_num1;
END;//
Try this:
DELIMITER //
create procedure selectEmp2(IN _num1 INT,OUT _name varchar(20))
BEGIN
select Name INTO _name
from myDB.stud_info
where Student_ID=_num1;
END//

Nested Stored Procedure in MYSQL has error

So I have this stored procedure, that I have tried to write a few different way, all to no avail.
CREATE PROCEDURE `CreateHero`
(IN USER_EMAIL VARCHAR(40), IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
INSERT INTO HEROES (USER_ID, NAME, GENDER)
VALUES
((CALL GetUserId(USER_EMAIL)), NAME, GENDER);
END
I am getting this 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 'CALL GetUserId(USER_EMAIL)), NAME, GENDER)' at line 6
I have tried tinkering with for a while.
GetUserId works. I tried to store the result in a temporary variable and then insert it but that did not work.
Not to be shameless but If you can determine a solution, a solution where the CALL GetUserId is stored in a variable would be best.
You can't use it like this. Rewrite your GetUserId procedure with an OUT parameter.
Something like this:
DELIMITER $$
CREATE PROCEDURE GetUserId(IN p_email varchar(20), OUT p_id int)
BEGIN
SELECT id INTO p_id FROM users where email = p_email;
/*or whatever your procedure does*/
END$$
DELIMITER ;
Then your procedure CreateHero would look like this:
DELIMITER $$
CREATE PROCEDURE `CreateHero`
(IN USER_EMAIL VARCHAR(40), IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
DECLARE v_id int;
CALL GetUserId(USER_EMAIL, v_id);
INSERT INTO HEROES (USER_ID, NAME, GENDER)
VALUES
(v_id, NAME, GENDER);
END$$
DELIMITER ;
Old thread but could help some one looking for later...
Base on fancyPants answer but more beautiful like this:
DELIMITER $$
CREATE FUNCTION GetUserId(IN p_email varchar(20)) RETURNS int
BEGIN
RETURN(SELECT id FROM users where email = p_email);
/*or whatever your function does*/
END$$
DELIMITER ;
And then:
DELIMITER $$
CREATE PROCEDURE `CreateHero` (IN USER_EMAIL VARCHAR(40),
IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
INSERT INTO HEROES (USER_ID, NAME, GENDER)
VALUES (GetUserId(USER_EMAIL), NAME, GENDER);
END$$
DELIMITER ;

Trigger not working as expected

I trying execute an trigger on mysql database.
The command executes successfully, but the trigger not working.
DELIMITER #
CREATE TRIGGER generate_coupon AFTER INSERT ON order
FOR EACH ROW
BEGIN
DECLARE userid, couponvalue INT;
DECLARE couponcode VARCHAR;
SELECT idUser INTO userid FROM indication WHERE email = NEW.email;
SET couponvalue = 20;
SET couponcode = 'abc123';
INSERT INTO coupon(idUser,idOrder,couponvalue,couponcode) values(userid, NEW.id, couponvalue, couponcode);
END#
DELIMITER ;
I suspect your problem arises from the collisions between your variables couponvalue and couponcode with the same-named columns in your coupon table. As documented under Local Variable Scope and Resolution:
A local variable should not have the same name as a table column.
You could simplify your trigger to the following and, in so doing, avoid this problem entirely:
CREATE TRIGGER generate_coupon AFTER INSERT ON order FOR EACH ROW
INSERT INTO coupon
(idUser, idOrder, couponvalue, couponcode)
SELECT idUser, NEW.id, 20, 'abc123'
FROM indication
WHERE email = NEW.email
;

Error 1146 with stored procedure

hope everything is going great with you, I'm facing a problem with stored procedure, get to the chase, here's the code :
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `register`(in un varchar(45),in pw varchar(45),
in user_email varchar(45),
in permissionid int,in targeted_table varchar(15))
begin
declare id int;
declare target_table varchar(15);
set target_table = targeted_table;
insert into user_authentication(user_name,user_password,email,permission_id)
values(un,pw,user_email,permissionid);
select user_id into id
from user_authentication
where user_name = un;
insert into target_table(user_id)values(id) ;
end
whenever I call the SP through this statement :
call register('abeer','somePassword','someEmail',1,'job_seeker')
workbench shouts at me coming out with this exception :
Error Code: 1146
Table 'recruitment.target_table' doesn't exist
In fact It commits the first insertion statement in the SP, but when It reaches the select statement, I got the exception above, though I'm dead sure the table,job_seeker, is there, can't you just tell me what goes wrong with my SP, for this is the first time using multiple statements inside SP, variables too, thank you .
You have coded a literal for the table name - you want mysql to evaluate the variable, then execute it as dynamic sql. Here's how you do it:
PREPARE mycmd from CONCAT('insert into ', target_table, '(user_id) values(', id, ')');
EXECUTE mycmd;

MySQL Stored Procedures

I'm coming from a MS SQL Server background.
Working on a new project using MySQL with NaviCat 8 Admin tools.
Ok, here's the question.
Normally when working in MS land if I want to update some data I use a stored procedure to do this:
Drop Procedure spNew
Create Procedure spNew (#P_Param)
UPDATE Table
SET Field = 'some value'
WHERE ID = #P_Param
I am trying to do this same logic from within NaviCat.
I defined the Parameter, (IN '#P_Param' int)
In the Definition i placed:
BEGIN
UPDATE Table
SET Field = 'some value'
WHERE ID = #P_Param
END;
When I try and save the stored procedure, i'm getting this error:
"1064 - You have an error in your SQL syntax, blah, blah, blah"
Can anyone at least point me in the right direction?
Thanks.
CREATE PROCEDURE spNew(P_Param INT)
BEGIN
UPDATE Table
SET Field = 'some value'
WHERE ID = P_Param;
END;
Note that MySQL syntax and overall ideology are very different from those of SQL Server.
You may also need to set delimiter:
DELIMITER $$
CREATE PROCEDURE spNew(P_Param INT)
BEGIN
UPDATE Table
SET Field = 'some value'
WHERE ID = P_Param;
END;
$$
DELIMITER ;
BTW, I'm assuming you don't actually call your table "Table", since it's a reserved word.
If you do, you need to enclose it into backticks like this:
DELIMITER $$
CREATE PROCEDURE spNew(P_Param INT)
BEGIN
UPDATE `Table`
SET `Field` = 'some value'
WHERE `ID` = P_Param;
END;
$$
DELIMITER ;
Parameters to MySQL stored procedures aren't prefixed with # or quoted in either the declaration or when used. Local variables are prefixed with #, however.
Try:
DROP PROCEDURE IF EXISTS spNew;
CREATE PROCEDURE spNew(IN P_Param INT)
BEGIN
UPDATE Table
SET Field = 'some value'
WHERE ID = P_Param
END;