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 ;
Related
I am trying to create this stored procedure in MySQL. However, MySQL does not accept this procedure and displays that there is a syntax error. Can someone explain what I am doing wrong? By the way, I am new to writing stored procedures.
DELIMITER $$
CREATE definer=`root`#`localhost` PROCEDURE 'login_insert'
( IN uname VARCHAR(20),
IN paword VARCHAR(20),
OUT result INT)
BEGIN
INSERT INTO db1.login_tab
( username,
pword,
)
VALUES
(
uname,
paword,
)
IF ##ERROR = 0
SET #result = 0
ELSE SET #result = 5
RETURN #result
END$$
DELIMITER ;
Changing it to the following as per below suggestion is not helping either. I have removed the return statement completely
DELIMITER $$
CREATE definer=`root`#`localhost` PROCEDURE 'login_insert'
( IN uname VARCHAR(20),
IN paword VARCHAR(20),
OUT result INT)
BEGIN
INSERT INTO db1.login_tab
( username,
pword
)
VALUES
(
uname,
paword
)
END$$
DELIMITER ;
I am creating a procedure called AccLikesVid inserting into a table named a_likes_v (table infomation):
DELIMITER $$
CREATE PROCEDURE AccLikesVid(username VARCHAR(30), vidid INT(11), type BOOL)
BEGIN
IF (type = 1) THEN
INSERT INTO a_likes_v VALUES (username, vidid, NOW(), 1);
ENDIF;
END$$
But when I execute the above code, MySQL Workbench generates an error: Error Code 1193. Unknown system variable 'now'
Could you tell me what's wrong with my code?
You should use:
DELIMITER $$
CREATE PROCEDURE AccLikesVid(username VARCHAR(30), vidid INT(11), v_type BOOL)
BEGIN
IF (v_type = 1) THEN -- TYPE is keyword, avoid such identifiers
INSERT INTO a_likes_v(account_name, video_id, dtime, liked)
VALUES (username, vidid, NOW(), 1); -- avoid blind insert
END IF; -- END IF not ENDIF
END$$
DBFiddle Demo
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//
Getting error during Creating Stored Procedure for Callable Statement:
I know some thing very simple going wrong, but i'm just unable to figure out!
My QUERY:
USE demo;
1. CREATE PROCEDURE
2. INSERT_emp_data (IN ID INT, IN NAME VARCHAR(2), IN AGE INT, IN IMAGE BLOB)
3. BEGIN
4. INSERT INTO emp_data VALUES(ID, NAME, AGE, IMAGE);
5. END;
/
SQL query:
CREATE PROCEDURE
INSERT_emp_data (IN ID INT, IN NAME VARCHAR(2), IN AGE INT, IN IMAGE BLOB)
BEGIN
INSERT INTO emp_data VALUES(ID, NAME, AGE, IMAGE);
MySQL said: Documentation
#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 4
Appreciate your help!
Thank for your time!
When you write a stored procedure in MySQL, you should use the DELIMITER statement. In addition, you should name your columns so they do not conflict with column names. And, when using INSERT always list the columns name. So:
DELIMITER $$
CREATE PROCEDURE INSERT_emp_data (
IN IN_ID INT,
IN IN_NAME VARCHAR(2),
IN IN_AGE INT,
IN IN_IMAGE BLOB
)
BEGIN
INSERT INTO emp_data(id, name, age, image)
VALUES(IN_ID, IN_NAME, IN_AGE, IN_IMAGE);
END;
$$
DELIMITER ;
if i use one table in two different Stored procedure name, (one for insert , one for update command) it showing syntax error.
first i created studentrc SP:
delimiter //
create procedure studentrc(in student_name varchar(20),in Reg_no int(6),in mark1 int(3), in mark2 int(3),in total int(10))
begin
insert into studentrecords values(student_name,Reg_no,mark1,mark2,total);
end; //
no errors
next i create studentrcs SP:
delimiter //
create procedure studentrcs(inout Reg_no int(6))
begin
UPDATE studentrecords
set student_name=?,mark1=?,mark2=?,total=?
where Reg_no=?;
end;//
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 studentrecords
set student_name=?,mark1=?,mark2=?,total=?
where Reg_no=' at line 3
how can rectify this error...
I'll be the first to admit I'm not a SQL guru by any means, but shouldn't you be taking in more variables and using them in place of the question marks?
You need to put the news values in the args list
create procedure studentrcs(sname varchar(100),m1 varchar(100),m2 varchar(100),total int,inout Reg_noarg int(6))
begin
UPDATE studentrecords
set student_name=sname ,mark1=m1,mark2=m2,total=totalarg
where Reg_no=Reg_noarg ;
for "INSERT"
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydatabase`.`myprocedurename` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myprocedurename`(IN field1 VARCHAR(50),
IN field2 INT(10),IN field3 INT(10), IN field4 VARCHAR(10))
BEGIN
INSERT INTO mytablename (FIELD_1,FIELD_2,FIELD_3,FIELD_4) VALUES
(field1,field2,field3,field4);
END $$
DELIMITER ;
size which i have given in IN parameter should be equal to field size in table
for "UPDATE"
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydatabase`.`myprocedurename` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myprocedurename`(IN field1 VARCHAR(50),
IN field2 INT(10) )
BEGIN
UPDATE mytablename SET FIELD_1=filed1 WHERE FIELD_2=field2;
END $$
size which i have given in IN parameter should be equal to field size in table
hope this may help you.