mysql stored procedure select and update selected - mysql

i am trying to write a stored procedure for mysql, and basicly, it will select one id, and update selected id's operationdate.
DELIMITER //
CREATE PROCEDURE getID(
IN proc_intervaldate datetime
)
BEGIN
DECLARE RUserID BIGINT;
SELECT
UserID
INTO RUserID
FROM Tasks
WHERE OperationDate < proc_intervaldate
LIMIT 1 ;
UPDATE Tasks SET OperationDate = now() WHERE UserID = RUserID;
SELECT RUserID ;
END //
DELIMITER ;
but when i use this, procedure, it will return UserID but not update, if i comment
SELECT RUserID
then, it updates, but no data returns.

when I use this, procedure, it will return UserID but not update, if I comment 'SELECT RUserID;' then, it updates, but no data returns.
You can change definition of stored procedure to use an OUT parameter rather than just IN. With that, you can just capture the OUT value on the same OUT parameter, in your scripting language. Meaning you need not execute:
SELECT RUserID ;
in your stored procedure.
Change your SP definition as below:
DELIMITER //
CREATE PROCEDURE getID( IN proc_intervaldate datetime, OUT RUserID bigint )
BEGIN
SELECT
UserID INTO RUserID
FROM Tasks
WHERE
OperationDate < proc_intervaldate
LIMIT 1 ;
UPDATE Tasks SET OperationDate = now() WHERE UserID = RUserID;
END;
//
DELIMITER ;
You can call this stored procedure like, for example, at MySQL console:
SET #RUserID := 0;
CALL getID( now(), #RUserID );
SELECT #RUserID;
Refer to:
MySQL: CALL procedure 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.

Related

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

SQL stored procedure- where am I going wrong?

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

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 - Call Multiple Procedures from Within a Procedure

I want to call multiple procedures from within a procedure. In the following SQL, I create three procedures. upd_r_money and upd_r_fuel both work as expected when called individually from the command line. When I call upd_all, only the first call within upd_all is run; the second call to upd_r_money doesn't run.
I can't figure out why this happens - maybe something in my upd_r_fuel procedure causes my upd_all procedure to end early? I am a newby to writing procedures, and SQL in general.
There was another question here about this problem, but the answer is exactly what I'm already doing, and the answer's link was down.
drop procedure upd_r_money;
delimiter //
CREATE procedure upd_r_money(row_id int)
BEGIN
DECLARE money_rate INT DEFAULT 1;
DECLARE period INT DEFAULT 0;
SET period = (select timestampdiff(second, (select lastaccessed from gamerows where id = row_id), now()));
update gamerows
set money = money + period * money_rate,
lastaccessed = now()
where id = row_id;
END;
//
delimiter ;
drop procedure upd_r_fuel;
delimiter //
CREATE procedure upd_r_fuel(row_id int)
fuel: BEGIN
DECLARE fuel_rate INT DEFAULT 1;
DECLARE period INT DEFAULT 0;
SET period = (select timestampdiff(second, (select lastaccessed from gamerows where id = row_id), now()));
update gamerows
set fuel = fuel + period * fuel_rate,
lastaccessed = now()
where id = row_id;
END fuel;
//
delimiter ;
drop procedure upd_all;
delimiter //
CREATE PROCEDURE upd_all(row_id int)
BEGIN
call upd_r_fuel(row_id);
call upd_r_money(row_id);
END;
//
delimiter ;
If I copy and paste the above SQL commands, my procedures are created successfully with no errors and I can call all three of them. However as I wrote earlier, upd_all seems to stop after calling its first procedure within. If I switch upd_r_money with upd_r_fuel, the same behavior occurs - the first procedure is called and not the second.
I suspect that it doesn't work as expected because you update lastaccessed time and calculate difference with NOW. First work because there is significant difference. But with second stored procedure you have timestammpdiff between NOW() and NOW() - miliseconds.
Check if removing in first stored procedure lastaccessed from update helps.
drop procedure upd_r_money;
delimiter //
CREATE procedure upd_r_money(row_id int)
BEGIN
DECLARE money_rate INT DEFAULT 1;
DECLARE period INT DEFAULT 0;
SET period = (select timestampdiff(second, (select lastaccessed from gamerows where id = row_id), now()));
update gamerows
set money = money + period * money_rate
where id = row_id;
END;
//
delimiter ;
Warning: Now the order of execution matters.
Also your stored procedures are so similiar that I would combine them in one UPDATE:
update gamerows
set fuel = fuel + period * fuel_rate,
money = money + period * money_rate,
lastaccessed = now()
where id = row_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 ;