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).
Related
I'm trying to create a stored procedure for deleting a record in my table.
I have tried the following but it doesn't seem to work:
Delimiter //
CREATE PROCEDURE BIRD_STRIKE_INCIDENT_DELETE #row text
AS
BEGIN
DELETE FORM bird_strike.incidents WHERE row_names = #row
END
delimiter ;
Call BIRD_STRIKE_INCIDENT_DELETE('11')
Can someone provide pointers on what I might be doing wrong here?
Thanks!
Your code looks more like SQL Server than MySql
This should be so
Delimiter //
CREATE PROCEDURE BIRD_STRIKE_INCIDENT_DELETE (_row text)
BEGIN
DELETE FROM bird_strike.incidents WHERE row_names = _row;
END//
delimiter ;
You can consider this approach.
IF EXISTS(SELECT 1 FROM sys.procedures
WHERE Name = 'BIRD_STRIKE_INCIDENT_DELETE')
BEGIN
DROP PROCEDURE dbo.BIRD_STRIKE_INCIDENT_DELETE;
END;
else
BEGIN
CREATE PROCEDURE BIRD_STRIKE_INCIDENT_DELETE (_row text)
BEGIN
DELETE FROM bird_strike.incidents WHERE row_names = _row;
END
END;
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'm trying to call a stored procedure from another stored procedure and store the value in a variable. The inner stored procedure basically checks if something exists and uses a select statement to return a zero or one. I keep getting an error. In this situation, MySQL is saying "=" is not valid at this position, expecting ";"
CREATE PROCEDURE `CardNames_Add` (searchedCard VARCHAR(50))
BEGIN
DECLARE exist TINYINT;
EXECUTE exist = CardNames_CheckExist searchedCard
IF (exist = 0)
INSERT INTO card_names (name)
VALUE(searchedCard)
END
You have to rewrite you other stored procedure, that you don't need btw, to give back a result
CREATE PROCEDURE CardNames_CheckExist (IN searchedCard VARCHAR(50), OUT result TINYINT )
BEGIN
--do some stuzff
result = 1
END
CREATE PROCEDURE `CardNames_Add` (searchedCard VARCHAR(50))
BEGIN
CALL CardNames_CheckExist(searchedCard,#result);
IF (#result = 0) THEN
INSERT INTO card_names (name)
VALUES (searchedCard);
END IF;
END
I want to be able to pass arguments to stored procedure, so I searched the net and encountered something like this:
DELIMITER $$
CREATE PROCEDURE addTmpUser
#id varchar(10)
AS
BEGIN
//some sql code
END$$
DELIMITER ;
The problem is that I am getting a syntax error for the # character.
Note: I am using MySQL db.
You are mixing variable types.
#variable is a user variable with a scope for the entire connection.
The variables in stored procedures look different, they don't have the # before them.
Also, you need to declare them. Here is an example
DELIMITER $$
CREATE PROCEDURE addTmpUser(p_id varchar(10))
-- the variable is named p_id as a nameing convention.
-- It is easy for variables to be mixed up with column names otherwise.
BEGIN
DECLARE innerVariable int;
insert into user (id) values (p_id);
-- return all users
select * from user;
END$$
DELIMITER ;
-- and now call it
call addTmpUser(10);
You need to use IN,OUT,INOUT to specify the parameter. So you can try this
DELIMITER $$
CREATE PROCEDURE addTmpUser (IN id VARCHAR(10))
BEGIN
//some sql code
END$$
DELIMITER ;
Look at the documentation
I am getting an error in MySQL that is driving me crazy and I just can't figure out what's wrong. I makes the following call:
CALL ProfileUpdateProgress(107)
MySQL returns the error: "Incorrect number of arguments for FUNCTION ccms.fnGetProfileAlbumsPhotoCount; expected 2, got 1"
Now, as you can see in the code below, the call being made to that function is: fnGetProfileAlbumsPhotoCount(_profileId, profileUserId)
That's two arguments isn't it?? Why is it erroring??
I'm going mad!!
Database procs:
DELIMITER $$
DROP PROCEDURE IF EXISTS `ProfileUpdateProgress` $$
CREATE DEFINER=`root`#`%` PROCEDURE `ProfileUpdateProgress`(
IN _profileId integer
)
BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
CALL ProfileUpdateProfileProgress(_profileId);
END $$
DELIMITER ;
which in turn calls:
DELIMITER $$
DROP PROCEDURE IF EXISTS `ProfileUpdateProfileProgress` $$
CREATE DEFINER=`root`#`%` PROCEDURE `ProfileUpdateProfileProgress`(IN _profileId int)
BEGIN
-- Declarations here
SELECT profileEyes, profileSex, profileHair, profileBustBand, profileBustCup, profileBirthCountry, profileProfession , profileAbout,
profileBiography, fnGetProfilePhoto(_profileId, null) AS profilePhoto, fnGetProfileAlbumsPhotoCount(_profileId, profileUserId) AS albumPhotoCount,
userAllowMultipleProfiles, profileIsPrimary, fnUserGetChildrenProfileCount(userId) AS ownerProfileCount
INTO _profileEyes, _profileSex, _profileHair, _profileBustBand, _profileBustCup, _profileBirthCountry, _profileProfession,
_profileAbout, _profileBiography, _profilePhoto, _albumPhotoCount, _userAllowMultipleProfiles, _profileIsPrimary,
_ownerProfileCount
FROM profile
INNER JOIN user
ON profileUserId = userId
WHERE profileId = _profileId;
-- Other irrelevant code here
END $$
DELIMITER ;
and the function being called that errors looks like:
DELIMITER $$
DROP FUNCTION IF EXISTS `fnGetProfileAlbumsPhotoCount` $$
CREATE DEFINER=`root`#`%` FUNCTION `fnGetProfileAlbumsPhotoCount`(
_profileId int,
_userId int
) RETURNS int(11)
BEGIN
DECLARE outProfileAlbumsPhotoCount int DEFAULT 0;
-- Irrelvant Code
RETURN outProfileAlbumsPhotoCount;
END $$
DELIMITER ;
Ah finally solved it. Another function called fnUserGetChildrenProfileCount in the select columns was the culprit as it too had a call to the fnGetProfileAlbumsPhotoCount() function and THAT call only had one argument, i.e. missing the second one.