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;
Related
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`;
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;
Why the stored procedure can't be created?
delimiter //
CREATE PROCEDURE p()
BEGIN
DECLARE j INT;
SET j = 1;
SELECT j:=j+1, request.* FROM request;
END//
The problem is in line:
SET j:=j+1, ...
You need to add a # to the variable name:
delimiter //
CREATE PROCEDURE p()
BEGIN
SET #j = 1;
SELECT #j:=#j+1, request.* FROM request;
END//
Here's an explanation: MySQL: #variable vs. variable. Whats the difference?
I am trying to create and set a variable:
DECLARE myId INT;
SET myId = 5;
However, I am getting invalid syntax complaint in MySQL Workbench:
SQL syntax error near 'DECLARE myId INT;'
I have tried the following variants:
DECLARE myId INT(4);
SET myId = 5;
DECLARE #myId INT;
SET #myId = 5;
DECLARE #myId INT(4);
SET #myId = 5;
What is wrong?
Try
SET #myId := 100;
Then if you do
select #myId;
You will get
100
As in the comment says Declare is only valid into stored programs like procedures, functions.
here you have an example of a store procedure and its call.
DELIMITER $$
CREATE PROCEDURE sp1 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE myId INT;
SET myId = 5;
SELECT CONCAT(xname,' -- ',myId);
END;
$$
DELIMITER ;
call sp1('MY NAME');
I experienced the same problem. The variables must be declared at the beginning of the script.
DELIMITER &&
DROP PROCEDURE IF EXISTS PS_HANDLERS;
CREATE PROCEDURE PS_HANDLERS(IN ID_USER INT, OUT isError INT)
BEGIN
DECLARE USER_EMAIL VARCHAR(50);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
SET IsError = 1;
END;
SET USER_EMAIL = CONCAT(RAND(),'#',RAND(),'.com');
SET isError = 0;
INSERT INTO tbl_user VALUES(ID_USER, 'ipsum','lorem','ipsum#lorem.com','password','ROLE_USER');
SELECT
u.*
FROM
tbl_user u;
END &&
DELIMITER ;
I need to create Cursors dynamically in stored procedure based on what situation i'm in, the ideal is below:
IF (varOrgGroupCode = '') THEN
BEGIN
DECLARE groupCode CURSOR FOR
SELECT `groupcode` FROM `LICENSEKEYS`;
END;
ELSE
BEGIN
DECLARE groupCode CURSOR FOR
SELECT `groupcode` FROM `LICENSEKEYS` WHERE `groupcode` = varOrgGroupCode;
END;
END IF;
The code above will cause an syntax error. I've googled & someone did it by using temp table. Can anyone tell me how please?
Probably the thing you found on Google was to create a temp table, populate that temp table based on your predicate and then reference the temp table in your cursor declaration.
Something like this:
DELIMITER $$
CREATE PROCEDURE `some_procedure`(IN varOrgGroupCode varchar(100))
BEGIN
DECLARE bNoMoreRows BOOLEAN DEFAULT FALSE;
DECLARE vGroupCode varchar(100);
DECLARE groupCode CURSOR FOR
SELECT `groupcode` FROM `LICENSEKEYS_TEMP`;
declare continue handler for not found set bNoMoreRows := true;
BEGIN
drop table if exists LICENSEKEYS_TEMP;
create temporary table `LICENSEKEYS_TEMP` (groupCode varchar(100));
IF (varOrgGroupCode = '') THEN
insert into `LICENSEKEYS_TEMP` (groupCode) SELECT `groupcode` FROM `LICENSEKEYS`;
ELSE
insert into `LICENSEKEYS_TEMP` (groupCode) SELECT `groupcode` FROM `LICENSEKEYS` WHERE `groupcode` = varOrgGroupCode;
END IF;
open groupCode;
GROUPCODE_LOOP: loop
fetch groupCode into vGroupCode;
-- Do some stuff
if bNoMoreRows then
close groupCode;
leave GROUPCODE_LOOP;
end if;
END LOOP GROUPCODE_LOOP;
END;
END$$
DELIMITER ;