Mysql procedure giving error - mysql

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

Related

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);

Unknown column error in mysql stored function

Following is my code of MySQL Stored Function.
DELIMITER $$
USE `mac_db`$$
DROP FUNCTION IF EXISTS `moving_average`$$
CREATE DEFINER=`root`#`localhost` FUNCTION `moving_average`(`table_name` VARCHAR(255),`column_name` VARCHAR(255),`order_column` VARCHAR(255),`row_cnt` INT) RETURNS DOUBLE
DETERMINISTIC
BEGIN DECLARE result_avg DOUBLE DEFAULT 0;
SELECT AVG(`column_name`) INTO result_avg FROM table_name ORDER BY order_column DESC LIMIT row_cnt;
RETURN result_avg;
END$$
DELIMITER ;
I am calling the function as follows:
SELECT moving_average('my_table','my_col','id',4);
I am getting the following error.
Table 'mac_db.table_name' doesn't exist
Please help to resolve the error.
table_name is a parameter not the name of table.
replace you proc parameter as below:
DELIMITER $$
USE mac_db$$
DROP FUNCTION IF EXISTS moving_average$$
CREATE DEFINER=root#localhost FUNCTION moving_average(#table_name VARCHAR(255),#column_name VARCHAR(255),#order_column VARCHAR(255),#row_cnt INT) RETURNS DOUBLE
DETERMINISTIC
BEGIN DECLARE result_avg DOUBLE DEFAULT 0;
SELECT AVG(#column_name) INTO result_avg FROM #table_name ORDER BY #order_column DESC LIMIT #row_cnt;
RETURN result_avg;
END$$
DELIMITER ;

Got error when creating procedure

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

You have an error in your SQL syntax when creating a procedure. MYSQL

I get that error when I try to create a procedure. I dont know what's failing and I searched a lot if someone has the same error than me, but usually they mistake at delimiters, and I think i have them right.
"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 ')
SELECT SUM(robatori.quantitat_robada) FROM robatori WHERE param1=jugador_lla' at line 1 "
My query is the next:
DELIMITER //
CREATE PROCEDURE robatoris (IN param1 VARCHAR)
SELECT SUM(robatori.quantitat_robada) FROM robatori WHERE param1=jugador_lladre;
//
DELIMITER ;
Thanks you all, that's my first question here. :)
You are missing the length for input param on type VARCHAR.
Change it like the following:
DELIMITER //
CREATE PROCEDURE robatoris ( IN param1 VARCHAR(255) )
SELECT SUM(robatori.quantitat_robada)
FROM robatori
WHERE param1=jugador_lladre;
//
DELIMITER ;
As you have only statement to execute, the BEGIN - END block was optional.
But it is advised to practice it in all cases.
DELIMITER //
CREATE PROCEDURE robatoris ( IN param1 VARCHAR(255) )
BEGIN
SELECT SUM(robatori.quantitat_robada)
FROM robatori
WHERE param1=jugador_lladre;
END;
//
DELIMITER ;
Add BEGIN and END. Use TEXT type:
DELIMITER //
CREATE PROCEDURE robatoris (IN param1 TEXT)
BEGIN
SELECT SUM(robatori.quantitat_robada)
FROM robatori
WHERE param1=jugador_lladre;
END//
DELIMITER ;

Getting errors when trying to create a PROCEDURE in mysql

I am trying to create a mysql stored procedure, but I get this error:
Script line: 2 Failed to CREATE PROCEDURE proc_test_bideep
The stored procedure code is:
DELIMITER $$
DROP PROCEDURE IF EXISTS `commun`.`insert_categorie` $$
CREATE PROCEDURE `commun`.`insert_categorie` (id_mere INT,
lib_categ VARCHAR(50),
id_categ_sup INT ,
categ_authInstantBuy INT)
BEGIN
SET #bg_mere := (SELECT categ_bg FROM categ_basic WHERE categ_id = id_mere);
#bg_mere+2,categ_level_bideep,categ_statut,categ_adult,categ_authSmallBid,categ_authBid,categ_authInstantBuy);
SELECT '1' AS code_retour; END IF;
ecetera.........
END $$
DELIMITER ;
a) You need to DECLARE any variables on the first lines of the procedure, including their datatype:
DECLARE bg_mere INT;
b) To fetch a value from the database into a variable, you use SELECT ... INTO syntax:
SELECT categ_bg INTO bg_mere FROM categ_basic WHERE categ_basic.categ_id = id_mere;
c) You have an END IF without the corresponding IF.
d) The closing END needs a semicolon (not BEGIN though), only then do you need a delimiter to finish the entire statement, and finally you should reset the delimiter back to normal:
BEGIN
# body of the stored procedure goes here
END;
$$
DELIMITER ;
Your parameters are missing the keyword IN such as: ...(IN id_mere INT, IN lib_categ ...). Also, you need to configure your OUT variable for #bg_mere in the initial parameter list such as (IN xxx, ..., OUT bg_mere VARCHAR/INT/WHATEVER).