Reading XML in a procedure for select query - mysql

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;

Related

How to convert Oracle stored procedure to 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.

MySQL8 Procedure - Use Parameters in Cursor

I'm trying to use input parameters of a stored procedure within a cursor of the procedure.
Calling the procedure as follows results in an Error
-- -role- -table- -cond-
CALL grantRoleToUsersFromWhere('Student', 'studenten', true);
Error Code: 1146. Table 'uni4.utable' doesn't exist
This tells me that the parameter 'userTable' was not written to the variable 'uTable' OR 'uTable' is not recognized as a variable at all by the cursor Statement.
I tried different approaches storing / using the parameters. e.g. use them directly or store them in a Variable with a SET statement. However, if I try to use SET uTable=userTable; before the cursor declaration, MySQL WorkBench won't accept the Procedure declaration.
I spent quite some time on this but I think I'm missing an important yet simple part :-)
DROP PROCEDURE IF EXISTS grantRoleToUsersFromWhere;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE grantRoleToUsersFromWhere(IN grantRole VARCHAR(30), IN userTable VARCHAR(30), IN addCondition VARCHAR(50))
BEGIN
DECLARE workUser VARCHAR(30) default '';
DECLARE gRole VARCHAR(30) default grantRole;
DECLARE uTable VARCHAR(30) default userTable;
DECLARE aCond VARCHAR(50) default addCondition;
DECLARE cur1 CURSOR FOR SELECT Name FROM uTable WHERE aCond;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO workUser;
GRANT gRole TO workUser;
END LOOP;
CLOSE cur1;
END $$
DELIMITER ;
Create dynamic cursors directly is not possible. You can however use VIEW's to achieve the same thing. See sample.
CREATE PROCEDURE p1 (select_statement VARCHAR(255))
BEGIN
DECLARE v1,v2 VARCHAR(255);
DECLARE c CURSOR FOR SELECT * FROM t;
SET #v = CONCAT('create temporary table t as ',select_statement);
PREPARE stmt1 FROM #v;
EXECUTE stmt1;
OPEN c;
FETCH c INTO v1,v2;
SELECT v1,v2;
END//
' DECLARE cur1 CURSOR FOR SELECT Name FROM uTable WHERE aCond;' is not possible mysql does not do variable substitution (for table name). Your read loop is infinite because you don't declare a continuation handler and test for not found in the read loop.

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

How to check output of function or variable for NULL

I am bit confused with Mysql syntax. I want to check for NULL the value of ExtractValue(xml, '//order[1]/quantity[$#i]') function. It can be assign to variable or this action can be skipped. I tried this and there is syntax error:
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test_for_null`$$
CREATE PROCEDURE `sp_test_for_null`()
BEGIN
DECLARE xml VARCHAR(1000);
SET xml = '';
DECLARE test VARCHAR(1000);
SET test = (SELECT ExtractValue(xml, '//order[1]/quantity[$#i]');
IF (test IS NULL) THEN SELECT 1; END IF;
END$$
DELIMITER ;
CALL sp_test_for_null;
I'd try this (note that all DECLAREs are at the beginning:
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test_for_null`$$
CREATE PROCEDURE `sp_test_for_null`()
BEGIN
DECLARE xml VARCHAR(1000);
DECLARE test VARCHAR(1000);
SET xml = '';
SET test = (SELECT ExtractValue(xml, '//order[1]/quantity[$#i]');
SELECT ISNULL(test, 1, 0);
END$$
DELIMITER ;
CALL sp_test_for_null;

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)