I'm trying to create a stored procedure in MySQL with an if-elseif statement but it doesn't seem to work. I can create a procedure that will run either the if or the elseif but not with the actual switch.
CREATE DEFINER=`root`#`%` PROCEDURE `sp_do_something`(
IN setting VARCHAR(30),
IN setting2 VARCHAR(30),
IN if_else_switch VARCHAR(20))
DETERMINISTIC
IF #if_else_switch = 'foo' then
BEGIN
...
END;
elseif #if_else_switch = 'bar' then
BEGIN
...
END;
END IF;
Then I call it with
CALL `database`.`sp_do_something`('setting', 'setting2', 'foo');
It completes without any errors but just returns "Affected rows: 0" and doesn't appear to be doing any of the actual SQL-code.
I'm guessing that it has something to do with the last value of the call-query and it somehow not being caught by either if - but why?
Try This
CREATE DEFINER=`root`#`%` PROCEDURE `sp_do_something`(
IN setting VARCHAR(30),
IN setting2 VARCHAR(30),
IN if_else_switch VARCHAR(20))
DETERMINISTIC
IF if_else_switch = 'foo' then
......
elseif if_else_switch = 'bar' then
.......
END IF;
when you use # with variable so it is global variable and it value persist till end of the session.
and in your case if_else_switch is come parameter so it is local variable. so use the local variable instead global variable
for more know http://www.java2s.com/Tutorial/MySQL/0201__Procedure-Function/LOCALSESSIONANDGLOBALVARIABLESINMYSQL.htm
Related
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 have create MySQL procedure having multiple IN parameters. I want to call procedure with few parameters but when I leave other fields blank it shows this error:
DELIMITER $$
CREATE DEFINER=itzakeed_akeed#localhost PROCEDURE ApiKez(
IN Choice VARCHAR(100),
IN ValidKey VARCHAR(100),
IN azid INT(5),
IN amts FLOAT(50)
)
BEGIN
DECLARE GetKey VARCHAR(100);
DECLARE Balance FLOAT;
CASE WHEN Choice='KeyCheck' THEN
SELECT COUNT(id) INTO GetKey
FROM users
WHERE api_key=ValidKey;
if key is valid
IF GetKey=1 THEN
SELECT *
FROM users
WHERE key=ValidKey;
ELSE
SELECT 0;
END IF;
ELSE
SELECT "INVALID INPUT CHOICE";
END CASE;
END
$$
DELIMITER ;
No you cannot call a procedure with less parameters than what is required. However, you can pass null or empty strings and check if a parameter has a value from withing the stored procedure.
I have a mySQL procedure that is calling another procedure to build a concatenated TEXT field. My insert statement is giving an error of "script can not be null" when the outer procedure is called. When I call the inner procedure on it's own, it is returning the proper value. What gives? it is probably something simple, but I'm not seeing it.
OUTER PROCEDURE
DELIMITER $$
USE `automation`$$
DROP PROCEDURE IF EXISTS `pd_build_script`$$
CREATE DEFINER=`user`#`%` PROCEDURE `pd_build_script`(
IN autom_type INT(3),
IN auto_font VARCHAR(30),
IN auto_design VARCHAR(10),
IN auto_d_only TINYINT(1),
IN auto_job VARCHAR(255),
IN auto_template VARCHAR(75)
)
BEGIN
DECLARE font VARCHAR(30);
DECLARE design_script TEXT;
SELECT indesign_name INTO font FROM automation.`indesign_font` WHERE auto_type = autom_type AND web_name = auto_font;
IF (auto_design = "PD00") THEN
CALL pd00(font, #out_script);
ELSEIF (auto_design = "PD1" OR auto_design = "PD73" OR auto_design = "PD88"
OR auto_design = "PD97") THEN
CALL single_letter_art(font, auto_design, auto_d_only, #out_script);
ELSEIF (auto_design = "PD2") THEN
CALL pd2(font, auto_d_only, #out_script);
ELSEIF (auto_design = "PD3") THEN
CALL pd3(font, auto_d_only, #out_script);
ELSEIF (auto_design = "PD28") THEN
CALL pd28(font, auto_d_only, #out_script);
ELSE
CALL pd_general(font, auto_design, auto_d_only, #out_script);
END IF;
SET design_script = CONCAT("some text", #out_script, "more text");
INSERT INTO automation.`job_script` (job_id, script, batch)
VALUE (auto_job, #out_script, CONCAT('batch text', auto_job));
END$$
DELIMITER ;
INNER PROCEDURE
DELIMITER $$
USE `automation`$$
DROP PROCEDURE IF EXISTS `pd2`$$
CREATE DEFINER=`user`#`%` PROCEDURE `pd2`(
IN font VARCHAR(30), design_only TINYINT(1), OUT layers TEXT
)
BEGIN
DECLARE layer TEXT;
DECLARE links TEXT;
IF (design_only = 1) THEN
SET layer = "layer text";
ELSE
SET layer = CONCAT("layer text",font,"more layer text");
END IF;
#Set the links for the design.
SET links = "links";
SELECT CONCAT(layer,links) INTO layers;
END$$
DELIMITER ;
Found out what the problem was. The select for the font variable was returning null, as the option being passed was not in the DB. This caused a cascade of null data. Issue wasn't actually in the code.
I am using a stored procedure call as follows:
DELIMITER //
CREATE procedure getCustomer(NID varchar(200),Name varchar(200), OUT Flag INTEGER, OUT CID VARCHAR(200))
BEGIN
DECLARE id varchar(200);
SET Flag = 0;
SET id = CONCAT(NID, '_' , Name);
SELECT 1 INTO Flag FROM Customer WHERE customerID = id;
IF Flag = 1 THEN
SET CID = id;
ELSE
INSERT INTO Customer(NID, Name, customerID) VALUES(NID, Name, id);
SET CID = id;
END IF;
END//
can you please tell me how to call IN, OUT variables in testing this procedure call?
or simply how to test this procedure call using exec proceudre_name(parameter) format?
As documented under CALL Syntax:
To get back a value from a procedure using an OUT or INOUT parameter, pass the parameter by means of a user variable, and then check the value of the variable after the procedure returns. (If you are calling the procedure from within another stored procedure or function, you can also pass a routine parameter or local routine variable as an IN or INOUT parameter.) For an INOUT parameter, initialize its value before passing it to the procedure. The following procedure has an OUT parameter that the procedure sets to the current server version, and an INOUT value that the procedure increments by one from its current value:
CREATE PROCEDURE p (OUT ver_param VARCHAR(25), INOUT incr_param INT)
BEGIN
# Set value of OUT parameter
SELECT VERSION() INTO ver_param;
# Increment value of INOUT parameter
SET incr_param = incr_param + 1;
END;
Before calling the procedure, initialize the variable to be passed as the INOUT parameter. After calling the procedure, the values of the two variables will have been set or modified:
mysql> SET #increment = 10;
mysql> CALL p(#version, #increment);
mysql> SELECT #version, #increment;
+--------------+------------+
| #version | #increment |
+--------------+------------+
| 5.5.3-m3-log | 11 |
+--------------+------------+
However, from your procedure it looks as though what you really want is to define a uniqueness constraint over the customerID column in your Customer table and then use INSERT ... ON DUPLICATE KEY UPDATE:
ALTER TABLE Customer ADD UNIQUE (customerID);
INSERT INTO Customer
(NID, Name, customerID)
VALUES
(123, 'foobar', CONCAT(123, '_', 'foobar'))
ON DUPLICATE KEY UPDATE
NID = NID
;
Example using IN:
DELIMITER //
CREATE PROCEDURE `procWithIN` (IN var1 INT)
BEGIN
UPDATE someTable set fldname = fldname + var1;
END//
CALL procWithIN(10);
Example using OUT:
DELIMITER //
CREATE PROCEDURE `procWithOUT` (OUT var1 VARCHAR(100))
BEGIN
SET var1 = 'This is a test';
END //
SET #someText = NULL;
CALL procWithOut(#someText);
-- do something with #someText (containing 'This is a test')
SELECT * FROM tbl WHERE fld=#someText
Example with INOUT
DELIMITER //
CREATE PROCEDURE `procWithINOUT` (INOUT var1 INT)
BEGIN
SET var1 = var1 + 100;
END //
SET #someInt = 10;
CALL procWithINOUT(#someInt);
-- #someInt now contains 110
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).