How to convert Oracle stored procedure to MySQL - mysql

I did these SQL statements in oracle
create or replace procedure teacherlist(lists out SYS_REFCURSOR )
is
begin
open lists for
select teacherid,teachername,status,email,contact,address
from teacher;
end;
but now I have to do the same stored procedure in MySQL for PHPMyAdmin, which I did it like this:
DELIMITER $$
CREATE PROCEDURE `teacherlist`(OUT `v_stuid` VARCHAR(10), OUT `v_teachername` VARCHAR(100), OUT `v_status` VARCHAR(20), OUT `v_email` VARCHAR(40), OUT `v_contact` VARCHAR(20), OUT `v_address` VARCHAR(100))
begin
DECLARE v_teacherid varchar(10);
DECLARE v_teachername varchar(100);
DECLARE v_status varchar(20);
DECLARE v_email varchar(40);
DECLARE v_contact varchar(20);
DECLARE v_address varchar(100);
DECLARE My_Cursor CURSOR FOR
select teacherid,teachername,status,email,contact,address
from teacher;
OPEN My_Cursor;
FETCH My_Cursor INTO v_teacherid,v_teachername,v_status,v_email,v_contact,v_address;
CLOSE My_Cursor;
END$$
DELIMITER ;
which I'm not sure right or wrong because when I execute the procedure it did not give me any output.

There is no direct equivalent of Oracle Database REF CURSOR in MySQL.
The easiest solution is probably something like:
DELIMITER $$
CREATE PROCEDURE teacherlist()
BEGIN
select teacherid,teachername,status,email,contact,address
from teacher;
END$$
DELIMITER ;
And using call statement to use the result set.

Related

**When i execute this procedure than get error 1064**

CREATE DEFINER=`root`#`localhost` PROCEDURE `GetStateList`(IN _CountryName VARCHAR(255))
BEGIN
DECLARE #CCode VARCHAR(50)
SET #CCode = (SELECT CountryID from countrylist where CountryName = _CountryName);
SELECT #CCode;
END
You need to redefine the Delimiter to something else (eg: $$) other than ;. At the end, reset the limiter back to ;. Also, a semicolon was missing in the Declare statement:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `GetStateList`(IN _CountryName VARCHAR(255))
BEGIN
DECLARE #CCode VARCHAR(50); -- semicolon was missing here
SET #CCode = (SELECT CountryID
from countrylist
where CountryName = _CountryName);
SELECT #CCode;
END$$
DELIMITER ;

Issue when declaring a variable for parameter

in my SQL version community-8.0.11.0:
I'm getting an error when declaring the variable in the below code.
use `mydb`;
Delimiter //
declare V_id char(30);
set V_id = 'AM-439';
select * from tableA
where TableID= V_id;
Delimiter;
Could anyone help me, please?
You need to include the code in a stored procedure, you can't just declare a variable in a sql script in MySQL.
This seems to work:
USE `mydb1`;
DROP procedure IF EXISTS `new_procedure`;
DELIMITER $$
USE `mydb1`$$
CREATE PROCEDURE `new_procedure` ()
BEGIN
declare V_id char(30);
set V_id = 'AM-439';
select * from tableA
where TableID= V_id;
END$$
DELIMITER ;
call `new_procedure`;

Reading XML in a procedure for select query

I have created a procedure in which I am passing a xml type data. I am trying to read that data but it is always giving null.
delimiter //
create procedure SP_LogIn(xml text)
begin
declare AgentId varchar(30);
declare pass varchar(30);
set #AgentId=ExtractValue(#xml,'/operation/userName');
set #Pass=ExtractValue(#xml,'/operation/paasword');
select * from am_agentmasteraccount where am_AgentId=#AgentId and am_AgentPassword=#Pass;
end //
delimiter;
here I am calling the procedure
call SP_Login('<operation><userName>RAJ0560111</userName><password>rajpratha</password></operation>');
try this it would work.
delimiter //
create procedure SP_LogIn(xml text)
begin
declare AgentId varchar(30);
declare pass varchar(30);
set AgentId:=(ExtractValue(xml,'/operation/userName'));
set Pass:=(ExtractValue(xml,'/operation/password'));
select * from am_agentmasteraccount where am_AgentId=AgentId and am_AgentPassword=Pass;
select AgentId,pass;
end //
delimiter;

Error in if...end-if stored procedure in mysql

drop procedure if exists test;
delimiter $$
create procedure `test`()
begin
DECLARE done INT DEFAULT FALSE;
DECLARE i1 VARCHAR(20);
DECLARE i2 DATE;
DECLARE i3 VARCHAR(20);
DECLARE i4 INT;
DECLARE curs1 CURSOR FOR SELECT * FROM test11;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs1;
FETCH curs1 INTO i1,i2,i3,i4;
if i4=22 then
insert into test1(abc) values(i1);
end if ;
CLOSE curs1;
end ;
;;
delimiter ;
above throws an error : SQL Error [1064] [42000]: 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 14
create table test11(abc varchar(20), cde varchar(20), eg varchar(20), asda varchar(20));
insert into test11 values("Hello",'1989-08-09','sdads','22');
create table test1(abc varchar(20), cde int(20), eg date, asda varchar(20));
I don't what i am doing wrong. Syntax seems to be fine.
drop procedure if exists sp;
CREATE PROCEDURE sp()
BEGIN
DECLARE intoffer INT;
SELECT max(asda) INTO intoffer FROM test11;
IF (intoffer IS NULL) THEN
SET intoffer = 1;
ELSE
SET intoffer = intoffer + 1;
END IF;
INSERT INTO test1 (asda) VALUES (intoffer);
end;
This is another procedure throwing same error. After removing the if..end-if both the code seems to work fine.
You are Missing fetch from
drop procedure if exists test;
delimiter $$
create procedure `test`()
begin
DECLARE done INT DEFAULT FALSE;
DECLARE i1 VARCHAR(20);
DECLARE i2 DATE;
DECLARE i3 VARCHAR(20);
DECLARE i4 INT;
DECLARE curs1 CURSOR FOR SELECT * FROM test11;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs1;
insertLoop: LOOP
FETCH FROM curs1 INTO i1,i2,i3,i4;
if i4=22 then
insert into test1(abc) values(i1);
end if ;
END LOOP insertLoop;
CLOSE curs1;
end ;
;;
delimiter ;

Mysql stored procedure giving error at runtime

I have created a stored procedure in Mysql, as :
delimiter $$
drop procedure if exists test9$$
Create procedure test9(test_type varchar(20))
Reads sql data
begin
Declare 1_id int;
Declare 1_date varchar(20);
Declare done int default 0;
Declare cur1 cursor for
select id,name from buyers where ticket_type='test_type';
Declare Continue handler for not found set done=1;
Create temporary table if not exists ticketninja.history2(n_id int,n_date varchar(20));
Open cur1;
hist_loop:loop
fetch cur1 into 1_id,1_date;
if done=1 then
leave hist_loop;
end if;
insert into ticketninja.history2(n_id ,n_date) values(1_id,1_date);
End loop hist_loop;
close cur1;
select * from history2;
drop table history2;
End;
$$
delimiter ;
but when i call it using,
call test9('platinum');
it returns an error saying :
#1312 - PROCEDURE ticketninja.test1 can't return
a result set in the given context
what i am doing wrong here?
I think you need an OUT variable (see first code sample here)