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
Related
I have this table
I want to create stored function that takes empno and returns its manager (boss) name. (one employee is boss of other employee)
this is the code i tried but it returned error
MYSQL said: The query was empty
CREATE FUNCTION MANAGER1(ENO int) RETURNS varchar DETERMINISTIC
BEGIN
DECLARE MANAGER_NAME varchar(MAX);
Select name into MANAGER_NAME where boss = ENO;
RETURN varchar MANAGER_NAME
DELIMITER $$ ;
Check once again with following query for creating stored function:
DELIMITER //
CREATE FUNCTION getManager1(eno int(11))
RETURNS varchar(255)
DETERMINISTIC
BEGIN
DECLARE manager_name varchar(250);
Select name into manager_name FROM employees where id = eno;
return manager_name;
END //
DELIMITER ;
In your stored procedure statement, END tag is missing and table name is also missing in SELECT statement. I have executed query mentioned here. Please match with your query and check what is missing in your query posted in question.
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//
I have table group with begindate lesson count and weekdays columns. I would like write MySQL procedure for adding data to another table named lessons according to group. But I couldn't handle with syntax of MySQL. Could you please help me resolve problems with that procedure:
CREATE PROCEDURE simpleproc (IN idGroup INT, IN groupName varchar(20),IN beginDate date, IN weekday1 INT, IN weekday2 INT, IN lessonCount INT)
BEGIN
DECLARE i;
SET i:=1;
WHILE i<=lessonCount DO
DATE_ADD(beginDate,INTERVAL 1 DAY)
IF (WEEKDAY(beginDate)=weekday1) OR (WEEKDAY(beginDate)=weekday2) THEN
SET name:=groupName+i;
SET price:=DIV(price,8)
insert into lessons (lessonName, idGroup, lessonPrice, datePassed)
values (name,idGroup,price,begindate);
SET i:=i+1
END IF;
END WHILE;
END
After solving problems I will add this code to prepared statement in Java
This will run. Make sure: 1. That you increment i in a proper place of your code inside the while loop. 2. Avoid usind reserved words (like name) for fields and variables. 3. Define price variable somewhere before making integer division of it. You know the code and your tables' structure. Nobody is capable to do it for you.
DROP PROCEDURE IF EXISTS `simpleproc`;
DELIMITER ;;
CREATE DEFINER=`root`#`localhost` PROCEDURE `simpleproc`(IN idGroup INT, IN groupName varchar(20),IN beginDate date, IN weekday1 INT, IN weekday2 INT, IN lessonCount INT)
BEGIN
DECLARE i int;
DECLARE name1 int;
DECLARE price int;
SET i:=1;
WHILE i<=lessonCount DO
SET beginDate:=DATE_ADD(beginDate,INTERVAL 1 DAY);
IF WEEKDAY(beginDate) in(weekday1,weekday2) THEN
SET name1:=groupName+i;
SET price:=price DIV 8;
insert into lessons (lessonName, idGroup, lessonPrice, datePassed)
values (name1,idGroup,price,begindate);
END IF;
SET i:=i+1;
END WHILE;
END
;;
DELIMITER ;
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
I am new to creating procedures in mysql, i know how to create them in MSSQL, but i am not sure what is wrong with this, it says Syntax Error Near END
CREATE PROCEDURE GetNameByID(IN CustID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = CustID
END
The query in your procedure needs a semi colon after it:
CREATE PROCEDURE GetNameByID(IN CustID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = CustID;
END
You may also need to set the delimiter to something. The MySQL documentation does this:
DELIMITER //
CREATE PROCEDURE GetNameByID(IN CustID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = CustID;
END//
(but obviously not with your query)
You are missing the ; at the end of select statement