MySQL procedure with multi in parameters when you can pass only one - mysql

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.

Related

MYSQL - How to create a variable inside stored procedure?

I'm trying to make a stored procedure that conditionally if the document number exists looks for an invoice and updates its balance to a new value that is a subtraction of the current balance and an input value, but for some reason when the procedure is executed it doesn't the update is running.
DELIMITER $$
CREATE PROCEDURE insertar_egreso(IN no_registro VARCHAR(255), IN id_proveedor INT, IN descripcion VARCHAR(255), IN no_doc VARCHAR(255),IN total DECIMAL ,IN no_cuenta VARCHAR(100),IN aprobado VARCHAR(255),IN subclase_gasto VARCHAR(255) )
BEGIN
IF no_doc!='' AND no_doc IS NOT NULL THEN
UPDATE ct_facturas SET saldo_fact =((SELECT saldo_fact FROM ct_facturas WHERE no_registro = no_doc)-total) WHERE no_registro = no_doc;
END IF;
SET total = ABS(total);
INSERT INTO ct_regtransac(no_registro,no_cuenta,vr_transaccion,tipo_mov,saldoreg) VALUES (no_registro,no_cuenta,total,1,total*-1);
INSERT INTO ct_egresos(tipo_reg,no_registro,id_proveedor,descripcion,no_doc,total,no_cuenta,aprobado, tipo_movimiento_cta, subclase_gasto, estado) VALUES ('EGRESO',no_registro,id_proveedor,descripcion,no_doc,total,no_cuenta,aprobado,'Salida',subclase_gasto,NULL);
END$$
DELIMITER ;
the queries inside if are not executing but i executed that update into phpMyAdmin with the same parameters and did work ,could it be that the if is not correctly written or the update sentence is confusing parameters?

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;

Trouble with calling a stored procedure within an stored procedure and setting result as a variable

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

Incorrect number of arguments for PROCEDURE

Define procedure:
DELIMITER $$
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20))
proc_label:BEGIN
IF tablename IS NULL THEN
SELECT 'Null detect';
LEAVE proc_label;
END IF;
SELECT 'after';
END;
$$
DELIMITER ;
Call Procedure:
CALL SP_Reporting();
Error :
ERROR 1318 (42000): Incorrect number of arguments for PROCEDURE
cds.SP_Reporting ; expected 1, got 0
How pass var by default like SP_Reporting(IN tablename = 'default value' VARCHAR(20))
When you are making your stored procedure, you can assign a value to your input, so there is no need to pass parameters while you are calling the proc.
We usually assign NULL and for making parameters optional, we use this method.
tablename VARCHAR(20) = NULL
Your complete script:
DELIMITER $$
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20) = NULL)
proc_label:BEGIN
IF tablename IS NULL THEN
SELECT 'Null detect';
LEAVE proc_label;
END IF;
SELECT 'after';
END;
$$
DELIMITER ;
EDIT
MySQL is not accepting optional parameters. So one way is to pass NULL value in your stored procedure and check it with IF statement inside your proc.
DELIMITER $$
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20))
proc_label:BEGIN
IF tablename IS NULL THEN
-- Do something;
ELSE
-- Do something else;
END IF;
END;
$$
DELIMITER ;
You have to pass the table name in the procedure call statement like:
CALL SP_Reporting(table_name);
you can't pass default in call statement.
You can assign default value before calling the procedure.
or use OUT instead of IN as a parameter.
you miss the parameter :tablename
you should like this :
call SP_Reporting('any varchar')
or
call SP_Reporting(null)

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