How to call a stored procedure with parameters in powerbuilder12 - mysql

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.

Related

MySQL stored procedure # syntax error

I want to be able to pass arguments to stored procedure, so I searched the net and encountered something like this:
DELIMITER $$
CREATE PROCEDURE addTmpUser
#id varchar(10)
AS
BEGIN
//some sql code
END$$
DELIMITER ;
The problem is that I am getting a syntax error for the # character.
Note: I am using MySQL db.
You are mixing variable types.
#variable is a user variable with a scope for the entire connection.
The variables in stored procedures look different, they don't have the # before them.
Also, you need to declare them. Here is an example
DELIMITER $$
CREATE PROCEDURE addTmpUser(p_id varchar(10))
-- the variable is named p_id as a nameing convention.
-- It is easy for variables to be mixed up with column names otherwise.
BEGIN
DECLARE innerVariable int;
insert into user (id) values (p_id);
-- return all users
select * from user;
END$$
DELIMITER ;
-- and now call it
call addTmpUser(10);
You need to use IN,OUT,INOUT to specify the parameter. So you can try this
DELIMITER $$
CREATE PROCEDURE addTmpUser (IN id VARCHAR(10))
BEGIN
//some sql code
END$$
DELIMITER ;
Look at the documentation

error while creating mysql procedure having SYS_REFCURSOR as out param

I'm creating procedure which is having two parameters , one is p_cursor of type SYS_REFCURSOR (OUT param) and the other one is p_rank of type INT(IN param). But it showing an error.
DELIMITER $$
CREATE PROCEDURE sp_student(p_cursor OUT SYS_REFCURSOR,p_rank IN INT)
BEGIN
OPEN p_cursor FOR SELECT * FROM student WHERE rank = p_rank;
END$$
DELIMITER ;
the error what I'm getting is,
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OUT SYS_REFCURSOR,p_rank IN INT)
BEGIN
OPEN p_cursor FOR SELECT * FROM st' at line 1
I think I'm syntactically wrong for SYS_REFCURSOR.. please check my code and let me realise my mistake.
thanks in advance
mysql doesnt have refcursor like oracle, if u r planning to write a stored procedure that returns multiple rows/result set in mysql just do
DROP procedure IF EXISTS `sample`;
DELIMITER $$
CREATE PROCEDURE `sample`(p_rank IN INT)
BEGIN
select * from MyTable where id=p_rank;
END$$
DELIMITER ;
call sample();
this will return a result set. which u can use.

Error Code: 1305 MySQL, Function does not Exist

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)

mysql error, stored procedure creation

I am new to mysql and I cant see why I have an error when I create my stored procedure.
DELIMITER |
CREATE PROCEDURE lastscan(IN task_id_var INT)
BEGIN
SELECT COUNT(*) FROM debugger WHERE task_id=task_id_var INTO #total|
SET #total=#total+1|
INSERT INTO debugger SET scan_num=#total, task_id=task_id_var|
END|
DELIMITER;
I get :
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
I also dont get, why do I need to use that delimiter syntax.. ? DELIMITER | and then again DELIMITER;...what its function
DELIMITER |
CREATE PROCEDURE lastscan(IN task_id_var INT, IN file_name_var VARCHAR(110))
BEGIN
SELECT COUNT(*) FROM debugger WHERE task_id=task_id_var INTO #total;
SET #total=#total+1;
INSERT INTO debugger SET scan_num=#total, task_id=task_id_var, file_name=file_name_var;
END|
DELIMITER;
This works for me. no need to put | delimiter in sored procedure. I think it is meant to be for the stored procedure and not for what is inside the body
You can't simply assign variables like that, you need the SET keyword first.
http://dev.mysql.com/doc/refman/5.1/en/set-statement.html
So you code should be something like this (tested with phpMyAdmin):
DELIMITER //
CREATE PROCEDURE lastscan(IN task_id_var INT)
BEGIN
SELECT COUNT(*) FROM debugger WHERE task_id=task_id_var INTO #total;
SET #total=#total+1;
INSERT INTO debugger SET scan_num=#total, task_id=task_id_var;
END;//
DELIMITER ;
The DELIMITER keyword is used to stop additional semicolons in your procedure to be the end of the current statement, so by redefining the delimiter to // MySql will process the whole CREATE PROCEDURE-block as one single statement and not stop at the first semicolon but instead wait for the first occurrence of //.
http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html

Error 1146 with stored procedure

hope everything is going great with you, I'm facing a problem with stored procedure, get to the chase, here's the code :
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `register`(in un varchar(45),in pw varchar(45),
in user_email varchar(45),
in permissionid int,in targeted_table varchar(15))
begin
declare id int;
declare target_table varchar(15);
set target_table = targeted_table;
insert into user_authentication(user_name,user_password,email,permission_id)
values(un,pw,user_email,permissionid);
select user_id into id
from user_authentication
where user_name = un;
insert into target_table(user_id)values(id) ;
end
whenever I call the SP through this statement :
call register('abeer','somePassword','someEmail',1,'job_seeker')
workbench shouts at me coming out with this exception :
Error Code: 1146
Table 'recruitment.target_table' doesn't exist
In fact It commits the first insertion statement in the SP, but when It reaches the select statement, I got the exception above, though I'm dead sure the table,job_seeker, is there, can't you just tell me what goes wrong with my SP, for this is the first time using multiple statements inside SP, variables too, thank you .
You have coded a literal for the table name - you want mysql to evaluate the variable, then execute it as dynamic sql. Here's how you do it:
PREPARE mycmd from CONCAT('insert into ', target_table, '(user_id) values(', id, ')');
EXECUTE mycmd;