Reputation: 3957
I am new in learning stored procedure.Now I am trying to make a procedure which will generate a random number and store it in a variable and then insert the value in another table.
create procedure getRand()
begin
delcare rc varchar(255);
SELECT CHAR(ROUND(65 + FLOOR( RAND() * 57 ))) into rc;
insert into Rand_loader (snum,rnum) values (rc,rc);
end//
But when I tried to run this on mysql then it gives this error
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 'rc varchar(255);
SELECT CHAR(ROUND(65 + FLOOR( RAND() * 57 ))) into rc;
insert' at line 3
I am unable to understand this error.Ill appreciate your help
Please help me to solve this
Upvotes: 0
Views: 253
Reputation: 6873
You just need to replace declare
with DECLARE
and always try to use capital letters while writing SQL commands.
Upvotes: 0
Reputation: 1609
Try below code. It works for me
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
PROCEDURE `yourdatabasename`.`getRand`()
/*LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'*/
BEGIN
DECLARE rc VARCHAR(255);
SELECT CHAR(ROUND(65 + FLOOR( RAND() * 57 ))) INTO rc;
INSERT INTO Rand_loader (snum,rnum) VALUES (rc,rc);
END$$
DELIMITER ;
You can download SQLyog
from here
https://code.google.com/p/sqlyog/downloads/list
to create tables and stored procedure with visual tools
Upvotes: 2
Reputation: 440
You have written "declare" wrong.
CREATE PROCEDURE `getRand`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
declare rc varchar(255);
SELECT CHAR(ROUND(65 + FLOOR( RAND() * 57 ))) into rc;
insert into Rand_loader (snum,rnum) values (rc,rc);
END
Upvotes: 1