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?
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 don't get what is wrong with this script
BEGIN
DECLARE crs INT DEFAULT 0;
WHILE crs < 10 DO
INSERT INTO `continent`(`name`) VALUES ('cont'+crs)
SET crs = crs + 1;
END WHILE;
END;
I want it to insert 10 values into the table continent but there is an error at the second line.
declare variable in MySQL with # and assign with :=
SET #crs = 0; // declaration
--here your query
#crs := #crs+1 // assignment
References
user defined variables
assignment
MySQL does not support the execution of anonymous blocks of stored procedure code.
You need to create a stored procedure including that code and then invoke it.
Also, you were missing the semi-colon at the end of your insert statements. I fixed that. You also probably want to use concat() instead of + to generate the names, but I'll leave that change to you.
Create the procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_ten_rows $$
CREATE PROCEDURE insert_ten_rows ()
BEGIN
DECLARE crs INT DEFAULT 0;
WHILE crs < 10 DO
INSERT INTO `continent`(`name`) VALUES ('cont'+crs);
SET crs = crs + 1;
END WHILE;
END $$
DELIMITER ;
Invoke the procedure:
CALL insert_ten_rows();
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;
I am writing a stored procedure using MySql which returns multiple rows using select statement.
My code is as below
drop procedure if exists GetAccounts;
DELIMITER //
CREATE PROCEDURE GetAccounts()
BEGIN
DECLARE rowcount int;
SET #resultset = (SELECT * from requests where STATUS = "FAILURE" ;
END //
DELIMITER
Any examples of how to return a resultSet in storedProcedure?
Thanks
Gendaful
DROP PROCEDURE IF EXISTS GetAccounts;
DELIMITER //
CREATE PROCEDURE GetAccounts()
BEGIN
DECLARE rowcount int;
SELECT * from requests where STATUS = "FAILURE" ;
END //
DELIMITER ;
Having trouble getting this to apply in MySQL Workbench 5.2.15
DELIMITER //
CREATE
DEFINER=`potts`#`%`
FUNCTION
`potts`.`fn_create_category_test` (test_arg VARCHAR(50))
RETURNS int
BEGIN
DECLARE new_id int;
SET new_id = 8;
RETURN new_id;
END//
The actual function will have a lot more between BEGIN and END but as it stands, even this 3 liner won't work.
Thanks!
DELIMITER $$
CREATE FUNCTION `fn_create_category_test` (test_arg varchar(50))
RETURNS INT
BEGIN
DECLARE new_id int;
set new_id=8;
return new_id;
END $$
DELIMITER ;
Works fine for me, try getting rid of DEFINER?