I did go through the similar questions and their answers of SO but it didn't help.
Here is my procedure:
DELIMITER //
DROP PROCEDURE IF EXISTS test//
CREATE PROCEDURE test()
BEGIN
DECLARE intime TIME;
SET intime:=(SELECT intime FROM new_attendance WHERE empid='xxx' AND DATE(dt)='2013-08-02');
SELECT intime;
END //
DELIMITER ;
When I execute this line of code it works and returns proper value:
SELECT empid FROM new_attendance WHERE empid='xxx' AND DATE(dt)='2013-08-02'
but it's not working inside procedure. i appreciate your help. Thanks a lot in advance!
First of all, variable assignment in MySQL takes a = syntax, not a := one.
EDIT: Strike the above, it seems that both syntaxes are supported after all...
Second, wouldn't be simpler to eliminate the intime variable altogether and just do
DELIMITER //
DROP PROCEDURE IF EXISTS test//
CREATE PROCEDURE test()
BEGIN
SELECT intime FROM new_attendance WHERE empid='xxx' AND DATE(dt)='2013-08-02';
END //
DELIMITER ;
Related
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);
I write a simple mysql stored procedure in workbench, however, it complains syntax error. What is wrong with my syntax?
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
select * from indicators;
END
Check out this link for a simple example of a Stored Procedure.
To your question: When you define a SP, you always start with setting a new delimiter in order to use the normal delimiter (;) in your SP. If you don't do this, SQL thinks that you finished your query after the smicolon, which isn't at the end of your query so it throws an error. To set a new delimiter, do the following:
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
SELECT * FROM indicators;
...
END //
DELIMITER ;
So now, you first set the delimiter to // and sql just stores the simicolons in your SP. At the END, you say that your query is done and type // - the new delimiter. Then you set it back to the normal simicolon in order to continue as always.
You'd better post your error messages, even though I can guess the problem you are suffering.
The default delimiter of MySQL is ";". So MySQL Workbench treat the statement below as a complete statement:
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
select * from indicators;
It's definitely wrong!
So you should change the delimiter expilictly when you write procedures. Here is an example:
delimiter // -- change the delimiter temporarily
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
select * from indicators;
END //
delimiter ; -- restore the default delimiter
DELIMITER $$
CREATE DEFINER=`root`#`locahost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
SELECT * from indicators;
END $$
DELIMITER ;
Use this
DELIMITER $$
CREATE DEFINER=`root`#`locahost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
SELECT * from indicators;
END $$
DELIMITER ;
I have seen some answer about this. But those cant solve my problem
DROP PROCEDURE IF EXISTS test_while_loop;
Delimeter //
CREATE PROCEDURE test_while_loop()
BEGIN
SELECT COUNT(*) INTO count_ FROM job_applicant;
END
//
Delimeter;
But shows unexpected near ';'
in your procedure you forget to DECLARE count_ variable. add blow line in the procedure your problem will solve
DECLARE count_ INT DEFAULT 0;
Try this.
DROP PROCEDURE IF EXISTS test_while_loop;
DELIMITER //
CREATE PROCEDURE test_while_loop()
BEGIN
DECLARE count_ INT DEFAULT 0;
SELECT COUNT(*) INTO count_ FROM job_applicant;
END //
DELIMITER ;
Right keyword is DELIMITER, not Delimeter
The problem lies in your last line.
Delimiter;
should be
Delimiter ;
(Note the space. Also note the spelling: it's DELIMITER, not DELIMETER.)
Once that's fixed, you also need to declare the variable count_, as Vipin Jain points out. The complete code should be
DROP PROCEDURE IF EXISTS test_while_loop;
DELIMITER //
CREATE PROCEDURE test_while_loop()
BEGIN
DECLARE count_ INT DEFAULT 0;
SELECT COUNT(*)
INTO count_
FROM job_applicant;
END //
DELIMITER ;
(Breaking up the SELECT as I did is strictly a matter of personal preference.)
You need to make up your mind. You've specified DELIMITER // yet you're still using ;. Of course it gives a syntax error. It's wrong. Use // (or don't change the delimiter at all). If the delimiter has been changed to // you can't use ; in the body of the procedure.
NB the final line Delimeter;
is misspelt
is missing an internal space.
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).
I used MSSQL stored procedures and triggers for a while; MySQL is getting me absolutely crazy about how to write even the simpler procedure.
Why I get a syntax error in this so stuoid trigger?
CREATE TRIGGER set_prio_default BEFORE INSERT ON categories
FOR EACH ROW
BEGIN
set #my_prio := 1;
SET new.prio := #my_prio;
END
In facts this TRIGGER is an oversemplification of:
DELIMITER $$
DROP PROCEDURE IF EXISTS `slot08`.`test` $$
CREATE PROCEDURE `slot08`.`test` ()
BEGIN
select 1 + max(prio) from categories INTO #my_prio;
select #my_prio;
END $$
DELIMITER ;
Still i do not understand how to use variables in procedures. If I use a DECLARE statement and the variable name miss the # character I got an error from mysql telling me "unknown system variable" - but many examples I saw used this syntax
I mean:
this does not work
CREATE TRIGGER set_prio_default BEFORE INSERT ON categories
FOR EACH ROW
BEGIN
declare my_prio integer default 1;
set my_prio := 1;
SET new.prio := my_prio;
END
If I use # I get syntax error.
Have some hints?
Thanks!
I dont think you have to use the := operator. A simple equals will do. All variables declared inside the stored procedure must be prefixed with the #symbol.
Hey, found the answer. Hope that people with so little experience as me in MySQL procedures could avoid to spend the time I have spent on the same issue. This does work:
DELIMITER $$
CREATE TRIGGER blablabla
BEFORE INSERT ON myStupidTable
FOR EACH ROW
BEGIN
declare my_prio integer;
select 1 + max(prio) from myStupidTable INTO my_prio;
SET new.prio := my_prio;
END $$
DELIMITER ;
It seems that the MySQL syntax errors experienced so far were a delimiter issue.
Greetings
Daniel