MYSQL - How to create a variable inside stored procedure? - mysql

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?

Related

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

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

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.

stored procedure in mysql returning null

I have the following table created in mysql database.
create table stud_info(Student_ID int,Name varchar(10),Class varchar(10),Marks int)
I have also created a stored procedure to retrieve the name given the id like below:
DELIMITER //
create procedure selectEmp2(IN num1 INT,OUT name varchar(20))
BEGIN
select Name INTO name from myDB.stud_info where Student_ID=num1;
END//
When I am calling the stored procedure , I am getting null value. Please let me know where I am going wrong.
I think your stored procedure should work, but I would advise giving names to parameters that are likely to be unique. I also prefer explicit variable assignment, because select into can mean different things. Does this work?
DELIMITER //
create procedure selectEmp2(IN in_num1 INT, OUT out_name varchar(20))
BEGIN
select si.Name into out_name
from myDB.stud_info si
where si.Student_ID = in_num1;
END;//
Try this:
DELIMITER //
create procedure selectEmp2(IN _num1 INT,OUT _name varchar(20))
BEGIN
select Name INTO _name
from myDB.stud_info
where Student_ID=_num1;
END//

MySql stored procedure returns value error

I am using the following stored procedure:
DELIMITER $$
USE `customer`$$
DROP PROCEDURE IF EXISTS `InsertCustomerEmail`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `InsertCustomerEmail`(IN p_Customer_ID INT(11),
IN p_from_who VARCHAR(50),
IN p_to_whom VARCHAR(50),
IN p_CC VARCHAR(50),
IN p_BCC VARCHAR(50),
IN p_Subject VARCHAR(500),
IN p_Massage VARCHAR(4000),
IN p_Is_Sent BIT(1),
IN p_Sent_When DATE,
IN p_Is_Active BIT(1),
OUT new_ID INT)
BEGIN
INSERT INTO customer_emails (
`Customer_ID`,
`from_who`,
`to_whom`,
`CC`,
`BCC`,
`Subject`,
`Massage`,
`Is_Sent`,
`Sent_When`,
`Is_Active`
) VALUES (p_Customer_ID,
p_from_who,
p_to_whom,
p_CC,
p_BCC,
p_Subject,
p_Massage,
p_Is_Sent,
p_Sent_When,
p_Is_Active);
SET #new_ID=SCOPE_IDENTITY();
END$$
DELIMITER ;
My database has an auto increment ID column, I would like to return this ID (the last one added) in the New_ID variable but when I run the CALL for the procedure it returns NULL for the New_ID.
Any suggestions?
Thanks!
The reason being the #new_ID assignment is not considered part of the transaction; the INSERT statement is. Move the following line outside the BEGIN ... END
SET #new_ID=SCOPE_IDENTITY();
Since you have only one operation in the batch, you can loose the BEGIN...END
EDIT
As Thorsten Dittmar suggested I need to explain a bit further.
The #new_ID assignment is executed right away, since it is not considered DML action and is not part of the batch. Thus SCOPE_IDENTITTY() is executed, but there is no identity generated yet, because the INSERT statement IS considered part of the batch and it is executed at its end.
EDIT 2
Since the OP added MySQL tag, above statement won't work, since there is no SCOPE_IDENTITY() in MySQL. Instead the correct function is LAST_INSERT_ID().
SET #new_ID=LAST_INSERT_ID();

Stored Procedure in mysql Not working

I am trying to insert a value in case not inserted else trying to update some of its field.There is only one variable used.
Value is not inserting although ion calling the store procedure it shows one row inserted.
Kindly help me , trying SP first time.
This is mine stored procedure
CREATE DEFINER=`root`#`localhost` PROCEDURE `InsertLocation`(in IpAddress varchar(45))
BEGIN
if (SELECT count(*) as count
FROM mbuzzz.location_byhits
where IpAddress = IpAddress
having count>0)
then
UPDATE location_byhits SET Hits=Hits+1 where IpAddress=IpAddress;
else
insert into location_byhits(IpAddress,Date_current)
values (IpAddress,CURTIME());
End if ;
end
Rename your input parameter to make it clear to the DB engine when you mean the parameter and when the column name.
where IpAddress = IpAddress
is always true since the engine thinks you compare a column to itself.
Try
delimiter |
CREATE DEFINER=`root`#`localhost` PROCEDURE `InsertLocation`(in IpAddrParam varchar(45))
BEGIN
if ((SELECT count(*) FROM mbuzzz.location_byhits where IpAddress = IpAddrParam)>0)
then
UPDATE location_byhits SET Hits=Hits+1 where IpAddress=IpAddrParam;
else
insert into location_byhits(IpAddress,Date_current)
values (IpAddrParam, CURTIME());
End if ;
end
|
delimiter ;