Error Code: 1305 MySQL, Function does not Exist - mysql

I have a problem. I created a function in MySQL which returns a String (varchar data type).
Here's the syntax:
DELIMITER $$
USE `inv_sbmanis`$$
DROP FUNCTION IF EXISTS `SafetyStockChecker`$$
CREATE DEFINER=`root`#`localhost` FUNCTION `SafetyStockChecker`
(jumlah INT, safetystock INT)
RETURNS VARCHAR(10) CHARSET latin1
BEGIN
DECLARE statbarang VARCHAR(10);
IF jumlah > safetystock THEN SET statbarang = "Stabil";
ELSEIF jumlah = safetystock THEN SET statbarang = "Perhatian";
ELSE SET statbarang = "Kritis";
END IF;
RETURN (statbarang);
END$$
DELIMITER ;
When I call the function like call SafetyStockChecker(16,16), I get this error:
Query : call SafetyStockChecker(16,16)
Error Code : 1305
PROCEDURE inv_sbmanis.SafetyStockChecker does not exist
Execution Time : 00:00:00:000
Transfer Time : 00:00:00:000
Total Time : 00:00:00:000
What's wrong with the function?

That is not the correct way to call a function. Here's an example to call a function:
SELECT SafetyStockChecker(16,16) FROM TableName
The way you are doing now is for calling a STORED PROCEDURE. That is why the error says:
PROCEDURE inv_sbmanis.SafetyStockChecker does not exist
because it is searching for a Stored procedure and not a function.

You should use
SELECT SafetyStockChecker(16,16)

Related

How to call a stored procedure with parameters in powerbuilder12

I'm using sybase powerbilder12 IDE and mySQL.
I have a stored procedure like this:
DELIMITER //
CREATE PROCEDURE CRTempTable(IN loc_code CHAR(6))
BEGIN
create temporary table mstparameter (select * from mstparameter_consolidate where location_code = 'loc_code');
END//
DELIMITER ;
I'm calling it in the powerbuilder12 like this:
DECLARE TempTBCRCall PROCEDURE FOR TempTableCR
location_code = :gs_location_code_mstparameter ;
execute TempTBCRCall;
It gives me the error :
Stored procedure execution failure1054 SQLSTATE = S0022
[MySQL][ODBC 5.2(a) Driver][mysqld-5.5.25a]Unknown column
'location_code' in 'field list'... Error Code 0
but location_code is there in my mstparameter_consolidate table.
If I set to enter the location_code manually it works fine.
This is an example that works, I hope it helps you.
DECLARE pb_acceso_usuario PROCEDURE FOR SP_ACCESO_VALIDA_DATOS_USUARIO (:gs_cod_usuario,:ls_password);
execute pb_acceso_usuario;
if SQLCA.sqlcode = 0 then
FETCH pb_acceso_usuario INTO :ln_count,:gs_des_usuario,:ls_estado;
CLOSE pb_acceso_usuario;
end if
try putting "table-name." in front of the column-name.

MYSQL Error while creating a function

I have a function
DELIMITER $$
CREATE FUNCTION `GetSuppliedMedicineQuantity` (FromDate varchar(50),Phid int,Todate varchar(50), MId INT)
RETURNS INTEGER
BEGIN
DECLARE totalmedicinecount INT;
IF(Todate is null)
then
select totalmedicinecount=sum(Totalquantity+FreeMedQuantity) from suppliedmedicine where
DATE(DateOfSupply)>=DATE(FromDate)
and Pharmacyid=phid and Medicineid=MId
group by Medicineid;
else
select totalmedicinecount=sum(Totalquantity+FreeMedQuantity) from suppliedmedicine where
DATE(DateOfSupply)<DATE(FromDate) and DATE(dateofsupply)<DATE(Todate)
and Pharmacyid=phid and Medicineid=MId
group by Medicineid;
end if;
SET totalmedicinecount=IF(ISNULL(totalmedicinecount),0,totalmedicinecount);
RETURN totalmedicinecount;
END$$
While creating the function I am getting an error :
ERROR 1415: Not allowed to return a result set from a function
I am very new to MYSQL and don't know how to solve this, kindly help. Thanks in advance.

mysql stored function - in varchar and return varchar - Error : 1172

I could not figure out what went wrong with my stored function.
select batch as bach from test where mfg_code = 'BC-7A1-5' group by batch;
When I run the script above and I am able to get the number of rows as expected.
However I am not able with the similar script below:-
DELIMITER $$
DROP FUNCTION IF EXISTS `testdata1970_05`.`listbatch` $$
CREATE FUNCTION `listbatch`(mfgnum VARCHAR(24)) RETURNS VARCHAR(10)
BEGIN
DECLARE bach VARCHAR(10);
SELECT batch into bach FROM test WHERE mfg_code = mfgnum group by batch;
RETURN bach;
END $$
DELIMITER ;
And it returns error:
"ERROR 1172 (42000): Result consisted of more than one row"
And here is my query below:
select listbatch("BC-7A1-5");
Your error message is telling you that more than one row is being returned by your query, and the resultset cannot be stored in a simple variable, as the variable you defined can hold only one value!

MySQL: call procedure from function

Is it possible to call a procedure from a function in MySQL? I get the error "not allowed to return a result set from a function." I want the results of the procedure call to be inserted into the function variables the same as if I had done a SELECT INTO directly in the function.
The function is (simplified) defined as
create function my_function()
returns int deterministic
begin
declare some_parameter int;
declare the_result int;
call my_procedure(some_parameter, the_result)
return the_result;
end;
The procedure is (simplified) defined as:
create procedure my_procedure(in my_parameter int, out my_result int)
begin
select 1
from dual;
end;
In essence, no. Functions are looking for a datatype, not a record (which is what is returned from the procedure).

Using a variable as a column name in a stored procedure?

Here I wrote a mysql procedure which select all text type field of specified database 'wp'
and I want to update every text type field row appending extra string via CONCAT function.
after I call test2 , error shows:
Query : call test2()
Error Code : 1146
Table 'wp._tbl' doesn't exist
Execution Time : 00:00:00:000
Transfer Time : 00:00:00:000
Total Time : 00:00:00:000
---------------------------------------------------
mysql regards _tbl as a string but not a variable.
So can I correct this?
code:
DELIMITER $$
USE `wp`$$
DROP PROCEDURE IF EXISTS `test2`$$
CREATE
PROCEDURE `test2`()
BEGIN
DECLARE _tbl VARCHAR(100);
DECLARE _cl VARCHAR(100);
DECLARE notFound INT DEFAULT 0;
DECLARE cur CURSOR FOR SELECT table_name,column_name FROM information_schema.columns WHERE table_schema = 'wp' AND data_type ='text';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET notFound =1;
OPEN cur;
WHILE notFound = 0 DO
FETCH cur INTO _tbl,_cl;
UPDATE _tbl SET _cl = CONCAT(_cl,'extra string goes here.....');
IF NOT noFound THEN SET notFound = 0;
END IF;
END WHILE;
CLOSE cur;
END$$
DELIMITER ;
You'll need to use prepared statements.
Also see How To have Dynamic SQL in MySQL Stored Procedure