error in stored procedure syntax - mysql

CREATE PROCEDURE getNumbers
(
#dName VARCHAR(20),
#iNum INT OUTPUT,
#sNum INT OUTPUT
)
AS
BEGIN
#iNum=SELECT count(i.ID) FROM instructor WHERE dept_name=#dName
#sNum=SELECT count(s.ID) FROM student AS s WHERE dept_name=#dName
END
Here is my first attempt at making a stored procedure. I keep getting an error in syntax at the line: #dName VARCHAR(20),
I have looked at other problems and tried their solutions but nothing works!

Is it MSSQL ? If so you don't need to use brackets and you should use following syntax to assign variables:
CREATE PROCEDURE getNumbers
#dName VARCHAR(20),
#iNum INT OUTPUT,
#sNum INT OUTPUT
AS
BEGIN
SELECT #iNum=count(i.ID) FROM instructor WHERE dept_name=#dName;
SELECT #sNum=count(s.ID) FROM student AS s WHERE dept_name=#dName;
END
If you do it in MYSql
DELIMITER $$
CREATE PROCEDURE getNumbers(
IN dName VARCHAR(20),
OUT iNum INT,
OUT sNum INT)
BEGIN
SELECT count(i.ID) INTO iNum FROM instructor WHERE dept_name=dName;
SELECT count(s.ID) INTO sNum FROM student AS s WHERE dept_name=dName;
END$$
DELIMITER ;

Try this form:
CREATE PROCEDURE getNumbers
#dName VARCHAR(20),
#iNum INT OUTPUT,
#sNum INT OUTPUT
AS
BEGIN

Related

MySQL syntax error in stored procedure inserting records

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 ;

Stored Procedure in mysql showing error in Workbench

I am creating stored procedure in MySQL workbench.
Although code looks fine but there shows some error which I couldnot figure out.Pl help.
create procedure UpdateAge(in employeeId int, in age int)
begin
update abc.employeedata set EmployeeAge= age where EmployeeId= employeeId ;
end
Error is: extraneous input found. expected end of input
Since there's just one statement, you probably don't need begin and end. Try
create procedure UpdateAge(in employeeId int, in age int)
update abc.employeedata set EmployeeAge= age where EmployeeId= employeeId ;
Try this
create procedure UpdateAge(in employeeId int, in age int)
begin
update abc.employeedata set EmployeeAge= age where EmployeeId= employeeId ;
end ;
Add semicolon to the end statement also

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 ;

mysql concat and insert into not working

i have a code in stored procedure which adds "PID-" to the id number, so if id number is 1, the result shoulb be PID-1. but it's not working.
Here's the code:
DROP PROCEDURE `inserproducts`//
CREATE DEFINER=`root`#`localhost` PROCEDURE `inserproducts`(pid int,pname varchar(50),pdesc varchar(50),psupp varchar(50),pdate date,pquant int)
begin
insert into products(productid,productname,proddescription,supplier,lastpurchasedate,quantityleft)
values(select concat('PID',pid,pname),pdesc,psupp,pdate,pquant));
select pid=last_insert_id();
end
how can i join insert into and concat together? Please help me with this one.
Use INSERT INTO...SELECT
insert into products(productname,proddescription,supplier,lastpurchasedate,quantityleft)
select concat('PID',pid,pname),pdesc,psupp,pdate,pquant
You can omit the column productid if it is an AUTO_INCREMENT column.
I was wondering why you need to execute select pid=last_insert_id(); when pid is an IN parameter.
UPDATE 1
DROP PROCEDURE `inserproducts`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `inserproducts`
(
pid int,
pname varchar(50),
pdesc varchar(50),
psupp varchar(50),
pdate date,
pquant int
)
begin
insert into products
(productname,
proddescription,
supplier,
lastpurchasedate,
quantityleft)
select concat('PID',pid,pname), pdesc, psupp, pdate, pquant;
select last_insert_id();
end$$
DELIMITER ;
just following up... if you specify values you don't need the select - there's probably a slight performance gain by just using values:
DROP PROCEDURE `inserproducts`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `inserproducts`
(
in pid int,
in pname varchar(50),
in pdesc varchar(50),
in psupp varchar(50),
in pdate date,
in pquant int
)
begin
insert into products
(productname, proddescription, supplier, lastpurchasedate, quantityleft)
values
(concat('PID',pid,pname), pdesc, psupp, pdate, pquant );
select last_insert_id();
end$$
DELIMITER ;
I'd leave select alone unless you actually need data from another table...