Mysql error nested arithmetic in select - mysql

When i am trying to execute this procedure it is showing an error.
USE `metro`;
DROP procedure IF EXISTS `transaction`;
DELIMITER $$
USE `metro`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `transaction`(
IN amount_p INT,
IN id_in INT,
IN id_out INT,
IN uidd INT
)
BEGIN
DECLARE uide INT;
DECLARE amt INT;
DECLARE NOW_AMT INT DEFAULT 0;
SELECT `amount` INTO amt FROM metro.wallet WHERE `uid`=uide;
INSERT INTO metro.transactions(`amount_deducted`,`time`,`station_id_in`,`station_id_out`,`uid`)
VALUES (amount_p,now(),id_in,id_out,uid);
SELECT uidd INTO uide;
SELECT uide;
SET NOW_AMT:=#amt+amount_p;
SELECT amt;
UPDATE metro.wallet SET `uid`=uide,`amount`=NOW_AMT;
END$$
DELIMITER ;
wallet is a table with uid and amount column.
Error code 1048, SQL state 23000: Column 'amount' cannot be null
Line 1, column 1
Execution finished after 0 s, 1 error(s) occurred.
Execution :
call metro.transaction(112,1,1,1);

Perhaps wrap your AMOUNT in a case statement where you can return a default if it is NULL.
SELECT case when `amount` is NULL then '' else `amount` END as `amount` INTO amt FROM metro.wallet WHERE `uid`=uide;

Related

Declaring INT variable to return in MYSQL Function problem

I'm trying to build a mysql function which inserts values into a table and returns me the last_id inserted, which should be the same after executing the insertion. But Xampp gives me an error in the line where im declaring the "last_bill_id" variable. Can someone please help me to understand what am I doing wrong?
Here is the code for the function:
CREATE FUNCTION insert_bill(
client_id varchar (12),
bill_date date
) RETURNS INT
BEGIN
DECLARE last_bill INT;
INSERT INTO bill
(
client_id, bill_date
)
VALUES
(
client_id, bill_date
);
SET last_bill = LAST_INSERT_ID();
RETURN last_bill;
END $$
DELIMITER ;
Error: #1064 - Something is wrong about 'INT
you need another DELIMITER at the start
DELiMiTER $$
CREATE FUNCTION insert_bill(
client_id varchar (12),
bill_date date
) RETURNS INT
DETERMINISTiC
BEGIN
DECLARE last_bill INT;
INSERT INTO bill
(
client_id, bill_date
)
VALUES
(
client_id, bill_date
);
SET last_bill := LAST_INSERT_ID();
RETURN last_bill;
END $$
DELIMITER ;

mysql function parameter not work in where condition of select statement

DELIMITER $$
CREATE DEFINER=`axistms`#`localhost` FUNCTION `CheckDoc`(`orderId` INT) RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE lvl int;
SELECT count(`id`) INTO lvl FROM `com_carrier_portal_upload_documents` WHERE `orderID`= orderId;
RETURN lvl;
END$$
DELIMITER ;
any order id pass through orderId parameter its doesn't effect on where condition. Always return count of all records.How to fix this?
I'd suggest you to rename stored procedure argument, do something like this -
CREATE DEFINER = `axistms`#`localhost` FUNCTION `CheckDoc` (orderIdParam int)
RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE lvl int;
SELECT
COUNT(`id`) INTO lvl
FROM `com_carrier_portal_upload_documents`
WHERE `orderID` = orderIdParam;
RETURN lvl;
END
...because WHERE orderID= orderId can be equal to WHERE true.

mysql procedure succeed delete some date but return null as row_count()

It always return null when I executed {select #leftnum,#deletenum ;},but it deleted the date as I excepted.sorry for pool English.
delimiter //
CREATE PROCEDURE REMOVEandreturn3( IN NUM INT ,
out leftnum int ,
out deletenum int)
BEGIN
DELETE FROM ORDERS2
WHERE ORDER_NUM = NUM ;
select count(order_num)
from orders2 into leftnum;
select row_count()
from orders2 into deletenum;
END//
delimiter ;
call removeandreturn2 (20007,#leftnum,#deletenum);
select *from orders2;
select #leftnum,#deletenum ;

variable in while loop retains only last value and inserts last value in temporary table

If I test my following procedure, then for all rows with value of status variable that we got in last select exists statement. Status variable retain only its last values not intermediate values. what is wrong with my procedure. I am not able to figure out.
DROP PROCEDURE IF EXISTS check_userfollowed_by_fbuser;
DELIMITER $
create procedure check_userfollowed_by_fbuser (
IN myArrayOfValue TEXT,
IN leaderID INT(11)
)
BEGIN
DECLARE status TINYINT(1);
DECLARE value VARCHAR(255);
DECLARE pos INT(11);
CREATE TEMPORARY TABLE fbid_exists_result (fbid varchar(255), status tinyint(1))
ENGINE=MEMORY;
WHILE (CHAR_LENGTH(myArrayOfValue) > 0)
DO
SET pos=LOCATE( ',', myArrayOfValue);
IF pos>0 THEN
SET value = LEFT( myArrayOfValue,pos-1 );
SET myArrayOfValue= SUBSTRING( myArrayOfValue,pos+1);
ELSE
SET value = myArrayOfValue;
SET myArrayOfValue='';
END IF;
SELECT EXISTS (
SELECT 1
FROM users_followings
LEFT JOIN users
ON users_followings.userID=users.userID
WHERE FacebookID=value
AND LeaderUserID=leaderID
LIMIT 1
)
INTO status;
INSERT INTO fbid_exists_result VALUES(value,status);
END WHILE;
SELECT fbid,
status
FROM fbid_exists_result ;
DROP TEMPORARY TABLE IF EXISTS fbid_exists_result ;
END$

MySql stored functions: Not works

I have the following function:
DROP FUNCTION IF EXISTS saveTableRow;
DELIMITER $$
CREATE FUNCTION saveTableRow(adapter_id int(10), view_id int(10),name varchar(255)) RETURNS TINYINT(1)
BEGIN
DECLARE retOK TINYINT DEFAULT 0;
IF (SELECT COUNT(*) FROM `tables` WHERE `adapter_id`=adapter_id AND `view_id`=view_id AND `name`=name ) = 0 THEN
INSERT INTO `tables` (`adapter_id`,`view_id`,`name`) VALUES (adapter_id, view_id, name);
SET retOK = 1;
END IF;
RETURN retOK;
END;
$$
DELIMITER ;
When i call the function to insert a new row with
SELECT saveTableRow(3,1,'Text');
I get the result '0' and there is no new row saved.
It might be a name collision problem. The name of the column is the same with the name of you parameter. You need to change the name of your parameter that is different from the name of your column. eg,
DROP FUNCTION IF EXISTS saveTableRow;
DELIMITER $$
CREATE FUNCTION saveTableRow(
_adapter_id int(10),
_view_id int(10),
_name varchar(255))
RETURNS TINYINT(1)
BEGIN
DECLARE retOK TINYINT DEFAULT 0;
IF (SELECT COUNT(*)
FROM `tables`
WHERE `adapter_id`=_adapter_id AND
`view_id`=_view_id AND
`name`=_name ) = 0 THEN
INSERT INTO `tables` (`adapter_id`,`view_id`,`name`)
VALUES (_adapter_id, _view_id, _name);
SET retOK = 1;
END IF;
RETURN retOK;
END;
$$
DELIMITER ;
change the if as follows as try please:
IF ((SELECT COUNT(*) FROM `tables` WHERE `adapter_id`=adapter_id AND `view_id`=view_id AND `name`=name ) < 1)