MySQL Workbench stored procedure END with delimiter not working - mysql

I'm using MariaDB to create a stored procedure using this code:
DROP PROCEDURE IF EXISTS P1;
DELIMITER // /* Set delimiter */
CREATE PROCEDURE P1(IN `date` DATE, IN `customer` INT, IN `bookingLocation` INT)
BEGIN /* Begin procedure */
/* Declare variables */
DECLARE currDate DATE;
DECLARE customerPlan INT;
DECLARE locCapacity INT;
DECLARE existingBookings INT;
SET currDate=CURRENT_DATE();
IF `date` < currDate THEN /* If date is in past */
SELECT "DATE CANNOT BE IN PAST" AS error;
ELSE
SELECT plan INTO customerPlan FROM `customers` WHERE customer_ID = `customer`; /* Get the customer's plan */
SELECT capacity INTO locCapacity FROM `locations`;
SELECT COUNT(*) FROM `desk_bookings` WHERE location=bookingLocation;
END IF;
END// /* End procedure */
DELIMITER ;
The code runs correctly using the command line, but when I run the code in MySQL Workbench, I receive an error:
"/" is not valid at this position, expecting EOF, '/'

I was able to resolve this by removing the comment on the DELIMITER // /* Set delimiter */ line.

Related

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;

Can we use local variable which is declared data type as TIMESTAMP inside a cursor SQL function?

I have scenario to declare some local variable(CURRENT_SYSTEM_TIME) of data type as "TIMESTAMP" and local variable(CURRENT_SYSTEM_TIME) I want use it inside a cursor to check some condition in WHERE clause.
Here is the example :
DROP FUNCTION IF EXISTS LOCAL_VARIABLE_NOT_ABLE_TO_ACCESS;
DELIMITER $$
CREATE FUNCTION LOCAL_VARIABLE_NOT_ABLE_TO_ACCESS(USERINFORMATION VARCHAR(50))
RETURNS TIMESTAMP
NOT DETERMINISTIC
BEGIN
DECLARE CURRENT_SYSTEM_TIME TIMESTAMP;
SELECT CURRENT_TIMESTAMP INTO CURRENT_SYSTEM_TIME FROM DUAL ;
DECLARE DATA_LIST CURSOR FOR (SELECT COLUMN_1,COLUMN_2,COLUMN_3 FROM TABLE_1 WHERE COLUMN_10 > CURRENT_SYSTEM_TIME);
/*
some logic
*/
RETURN 1;
END$$
DELIMITER ;
COMMIT;
After executing this query am getting some error as
Error CODE: 1064
MySQL SERVER VERSION FOR the RIGHT syntax TO USE near 'DECLARE DATA_LIST CURSOR FOR (SELECT COLUMN_1,COLUMN_2,COLUMN_3 FROM TABLE_1 WH' AT line 10
Can someone explain me how to use local variable inside a cursor? Will it possible to use local variable inside a CURSOR?
Yes. We can use local variable inside function or Cursor for MYSQL.
Check below code.
at time of declaring CURRENT_SYSTEM_TIME variable we can set default time for it.
DROP FUNCTION IF EXISTS LOCAL_VARIABLE_NOT_ABLE_TO_ACCESS;
DELIMITER $$
CREATE FUNCTION LOCAL_VARIABLE_NOT_ABLE_TO_ACCESS(USERINFORMATION VARCHAR(50))
RETURNS TIMESTAMP
NOT DETERMINISTIC
BEGIN
DECLARE CURRENT_SYSTEM_TIME TIMESTAMP default CURRENT_TIMESTAMP;
DECLARE DATA_LIST CURSOR FOR (SELECT COLUMN_1,COLUMN_2,COLUMN_3 FROM TABLE_1 WHERE COLUMN_3 >
CURRENT_SYSTEM_TIME);
/* some logic */
RETURN 1;
END$$
DELIMITER ;
COMMIT;

Mysql loop and insert

I have the following MySql script:
SET #skip = 0;
SET #max = (SELECT COUNT(*) FROM table1);
CREATE TEMPORARY TABLE TempTable(
id INT NOT NULL,
name VARCHAR(32) NOT NULL
);
loop1: LOOP
INSERT INTO TempTable (id, name) SELECT id, name FROM table1 LIMIT #skip, 1;
IF #skip < #max THEN
SET #skip = #skip + 1;
ITERATE loop1;
END IF;
LEAVE loop1;
END LOOP loop1;
SELECT * FROM TempTable;
This script is not working but it should select all the id and names in table1. I am using a loop because I am also going to do other stuff in those loops but that is for later.
I am not looking for a solution like SELECT id, name FROM table1 but I want my error fixed. So I can continue with my loop.
The error I get is:
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 'loop1: LOOP INSERT INTO TempTable (id, name) SELECT id, name
FROM table1' at line 1
/* set delimiter */
DELIMITER $$
/* remove procedure if exists... */
DROP PROCEDURE IF EXISTS insert_it $$
/* create procedure */
CREATE PROCEDURE insert_it ()
BEGIN
DECLARE varcount INT DEFAULT 1;
DECLARE varmax INT DEFAULT 15;
WHILE varcount <= varmax DO
INSERT INTO yourtable(fixed_val, count_val) VALUES(3493, varcount);
SET varcount = varcount + 1;
END WHILE;
END $$
/* reset delimiter back to normal */
DELIMITER ;
/* call procedure */
CALL insert_it();
try something like this for the syntax of your loop:
DECLARE #count INT;
DECLARE #max INT;
SET #count=1;
SET #max= (SELECT COUNT(*) FROM table1);
WHILE(#count < #max)
BEGIN
/*your database query logic*/
END
use "SET #count=(#count+1)" to increment your counter within the loop
There is a syntax error in your code (the LIMIT #skip which is dynamic SQL and requires some tricks to make it work) but it is not at loop1: LOOP.
My guess is you are trying to use LOOP outside a compound statement (BEGIN ... END) like a stored procedure, which is not possible. You have to create a stored procedure to do that.

MYSQL function, is it Workbench or just noobish me?

I have been sitting with a stored procedure for MySQL for days now, it just won't work, so I thought I'd go back to basic and do a very simple function that checks if an item exists or not.
The problem I had on the first one was that it said END IF is invalid syntax on one of my IF clauses, but not the other two. The second one won't even recognize BEGIN as valid syntax...
Is it I that got everything wrong, or have I stumbled upon a MYSQL Workbench bug? I have Workbench 5.2 (latest version when I'm writing this) and this is the code:
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
to fix the "begin" syntax error, you have to declare a return value, like this:
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT(11)
after doing that, Workbench won't return an error anymore ;o)
You have to specify the return value in signature as well delimiter at the end is missing. So, your function should look like
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER ;
Add this last thing it works :
$$
DELIMITER ;
it means you are using ( ; ) this in function so for that reason we use it..see
and see also
MySQL - Trouble with creating user defined function (UDF)

MySQl storage procedures

I have a little problem. Looks like the procedure does not exist. Somehow it's dropped after the creation. I get different error each time i change something. I'm not really sure what's causing the error, maybe I'm not allowed to drop procedures and creating them in the same query.
I hope you guys can help me out.
drop procedure if exists refIntChk;
DELIMITER //
CREATE PROCEDURE refIntChk(IN district INT(11), OUT b INT(1))
BEGIN
DECLARE b INT(1);
IF district IN (select dist FROM t13)
THEN
SET b = 1;
ELSE
SET b = 0;
END IF;
END; //
DELIMITER ;
drop procedure gen if exists ;
DELIMITER //
CREATE PROCEDURE gen()
BEGIN
DECLARE rows INT(11) DEFAULT (SELECT COUNT(dist) FROM t13);
DECLARE district INT(11);
DECLARE custname VARCHAR(16);
DECLARE revenue FLOAT;
DECLARE x INT DEFAULT 10000;
DECLARE outvar INT(11);
WHILE x > 0
DO
SET district = FLOOR(RAND()*rows)+1;
CALL refIntChk(district, outvar);
IF outvar = 1
THEN
SET custname = substring(MD5(RAND()), -16);
SET revenue = (RAND() * 10);
INSERT INTO t14 VALUES(NULL, custname, district, revenue);
SET x = x - 1;
END IF;
END WHILE;
END;//
DELIMITER ;
CALL gen();
When you get errors, it's usually good to run each statement, one by one, and see which one is producing the error.
The second DROP procedure statement should be:
drop procedure if exists gen;