Stored Procedure in mysql Not working - mysql

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 ;

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?

MySql stored procedure - add SELECT and UPDATE in 1 stored procedure

I try to call 1 stored procedure and do 2 things:
CALL PING(1)
update timestamp to row
and also
get back value from column
DELIMITER //
CREATE PROCEDURE ping (IN `P_in` INT)
BEGIN
UPDATE V_column SET my_timestamp=NOW() WHERE id=P_in;
SELECT updateFiles FROM V_column WHERE id=P_in;
END

Why won't MYSQL procedure properly iterate new ROWS in TABLE using WHILE statement?

I am creating this basic procedure using MySQL Workbench to accept a single input parameter.
The table "unique_days" has a single PRIMARY KEY column called "dayid" which currently has a single ROW with a value of 1.
DROP PROCEDURE IF EXISTS dayid_iteration;
DELIMITER $$
CREATE PROCEDURE dayid_iteration(maxdate_final INT)
BEGIN
DECLARE maxdate_current INT;
SET #maxdate_current = (SELECT (MAX(dayid) + 1) FROM unique_days);
DELETE FROM unique_days WHERE dayid > 1;
WHILE (maxdate_current > maxdate_final) DO
INSERT INTO unique_days (dayid) VALUES (maxdate_current);
SET maxdate_current = (maxdate_current+1);
END WHILE;
END$$
DELIMITER ;
The procedure is then called with an integer parameter.
CALL dayid_iteration(11);
The variables are setting properly because I can run a select statement with the variable and it shows the correct new value. The deletion of dayid > 1 also works (Tested by manually adding additional rows, and then running procedure). However, I can't seem to get the WHILE statement to insert new rows with the value provided.
Any help is much appreciated. I searched multiple other questions, and countless forums, but everything looks like it should be working.
I am expecting the code to CREATE 9 ROWS for a total of 10 ROWS.
The following is included just so you can see the starting values of the table.
SELECT * FROM unique_days;
For anyone who finds this question, the following code functions correctly. The input variable on the parameter was not setting properly. Once the parameter had "IN" placed in front of the variable name, it correctly received the parameter.
CREATE PROCEDURE dayid_iteration(IN maxdate_final INT)
DROP PROCEDURE IF EXISTS dayid_iteration;
DELIMITER $$
CREATE PROCEDURE dayid_iteration(IN maxdate_final INT)
BEGIN
DECLARE maxdate_current INT;
SET maxdate_current = (SELECT (MAX(dayid) + 1) FROM unique_days);
WHILE (maxdate_current <= maxdate_final) DO
INSERT INTO unique_days (dayid) VALUES (maxdate_current);
SET maxdate_current = (maxdate_current+1);
END WHILE;
END$$
DELIMITER ;
CALL dayid_iteration(1000);
This call procedure now properly works.
CALL dayid_iteration(1000);

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

Insert into table (select from another table) and some parameters

I want to insert into one table selecting values from another table and some with paramters.
CREATE PROCEDURE `insertuser`(
IN `param1` VARCHAR(50)
)
BEGIN
insert into users(firstname,lastname,email)
select name,nickname,#param1 from members
where id=3;
END
I fill param1 but the query insert null to email
what I need to do?
# symbol is used to define Session variables (accessible to the Session everywhere). You don't need to use # here, as you are using the input parameter value from the Stored procedure.
Try the following instead:
DELIMITER $$
CREATE PROCEDURE `insertuser`(IN `param1` VARCHAR(50))
BEGIN
INSERT INTO users(firstname,lastname,email)
SELECT name,nickname,param1 -- remove # from #param1 here
FROM members
WHERE id=3;
END$$
DELIMITER ;