0

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

2
  • 1
    DECLARE you have a typo Commented Dec 23, 2013 at 15:30
  • problem solved by the suggestion given in comment.Yes it is the DECLARE issue Commented Dec 23, 2013 at 15:38

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

You just need to replace declare with DECLARE and always try to use capital letters while writing SQL commands.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.