Got error when creating procedure - mysql

I want to get the info from table s via inputting specific student name.
Code goes like this:
delimiter //
`CREATE PROCEDURE get_info_hujiamin (IN sn_input VARCHAR)
BEGIN
SELECT *
FROM s
WHERE s.sn=sn_input;
END`
//

Do query like below: here you haven't added length of VARCHAR
DELIMITER //
CREATE PROCEDURE GetOfficeByCountry(IN countryName VARCHAR(255))
BEGIN
SELECT *
FROM offices
WHERE country = countryName;
END //
DELIMITER ;
You can also refer My SQL Procedure

Related

Not receiving input value in stored procedure

I m passing parameters in stored procedures but unable to fetch records when filtering with inputs.But when I store that Input into variables and pass it to fetch record I get the records.why?
Here is stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS Demo_akg$$
CREATE PROCEDURE Demo_akg (IN ip_Id VARCHAR (100))
BEGIN
SELECT * FROM table_namel IAS WHERE Id = ip_Id;
END$$
DELIMITER;
I tried to take input in variable and pass it to where clause ti fetch records .
Here is the method that I have tried. And it works why?
DELIMITER $$
DROP PROCEDURE IF EXISTS Demo_akg$$
CREATE PROCEDURE Demo_akg (IN ip_Id VARCHAR (100))
BEGIN
Select ip_Id into #ip_Id;
SELECT * FROM table_namel IAS WHERE Id = #ip_Id;
END$$
DELIMITER;
We didn't know the structure of table_namel, but it seems the table have a field with same name as input parameter.
To prevent that use the parameters with some prefix/surfix:
DELIMITER $$
DROP PROCEDURE IF EXISTS Demo_akg$$
CREATE PROCEDURE Demo_akg (IN p_ip_Id VARCHAR (100))
BEGIN
SELECT * FROM table_namel IAS WHERE Id = p_ip_Id;
END$$
DELIMITER;

SQL stored procedure- where am I going wrong?

I'm trying to create a stored procedure that calculates total revenue from a customer by if it's occupied and the standard rate. I am getting an error message and when I try to call from it I get NULL. Can anyone help? Thanks.
//Delimiter
CREATE PROCEDURE calculateRevenue (in customerIDs int, OUT totalRevenue dec(15,2))
BEGIN
SELECT SUM(Occupied*StandardRate) into totalRevenue FROM climatesouth
WHERE customerIDs = customerID;
END //
delimiter//
call calculateTotal(10, #totalRevenue);
SELECT #totalRevenue;
First you need to give your input parameters different names from the columns. Then you need to use them. Also, DELIMITER goes before the stored procedure definition:
DELIMITER //
CREATE PROCEDURE calculateRevenue (
in in_customerIDs int,
out out_totalRevenue dec(15,2))
BEGIN
SELECT SUM(cs.Occupied cs.* cs.StandardRate) into out_totalRevenue
FROM climatesouth cs
WHERE cs.customerID = in_customerID;
END //
The delimiter assignment is off: your are setting it to // after the create procedure statement.
Also, the parameter name needs to be fixed: your are not using the correct name in the query (your parameter has a trailing 's'), because of which the procedure will not produce the result you expect.
So:
delimiter // -- change the default delimiter here
create procedure calculaterevenue (in p_customerid int, out p_totalrevenue dec(15,2))
begin
select sum(occupied * standardrate) into p_totalrevenue
from climatesouth
where customerid = p_customerid; -- "p_customerid" is the parameter name
end //
delimiter ; -- reset the delimiter, now we can call the procedure
call calculaterevenue(10, #totalrevenue);
select #totalrevenue;
It is easier just to return the result as a result set rather than to use the OUT-parameter. The OUT-parameters are usually used only when calling procedure from another procedure. If you call the procedure from your application, use the result set.
delimiter //
CREATE PROCEDURE calculateRevenue (in_customerID int)
BEGIN
SELECT SUM(Occupied*StandardRate) as totalrevenue
FROM climatesouth
WHERE customerID = in_customerID;
END
//
delimiter ;
call calculateRevenue(10);

MySQL Stored function to return name on id input

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.

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//

Mysql procedure giving error

CREATE PROCEDURE
myProcedure( id INT )
BEGIN
SELECT * FROM `board`;
END
//check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
There are two ways:
Way 1:
When there is only one executable statement in the procedure body, using BEGIN - END is optional. And when used, you have to use custom DELIMITER.
CREATE PROCEDURE myProcedure( id INT )
SELECT * FROM `board`;
Way 2:
Define custom DELIMITER, define procedure, and then reset the delimiter.
DELIMITER //
DROP PROCEDURE IF EXISTS myProcedure //
CREATE PROCEDURE myProcedure( id INT )
BEGIN
SELECT * FROM `board`;
END;
//
DELIMITER ;
use delimeter
delimiter //
CREATE PROCEDURE
myProcedure( id INT )
BEGIN
SELECT * FROM `board`;
END //
Check if this solves the error
Delimiter //
CREATE PROCEDURE
myProcedure( id INT )
BEGIN
SELECT * FROM board;
END
//