3

I've got a following, simple table:

mysql> select * from version;
+----+---------+
| id | version |
+----+---------+
|  1 | 1       |
+----+---------+
1 row in set (0.00 sec)

I need to create a stored procedure that will do something (or nothing) depending on the value of this table (precisely, the only row of this table).

DELIMITER $$
DROP PROCEDURE IF EXISTS upgrade_version $$
CREATE PROCEDURE upgrade_version(current_version INTEGER, script TEXT)
BEGIN
  DECLARE version INT(11);
  SET version = (SELECT `version` FROM `version` WHERE `id` = 1 LIMIT 1);
  SELECT version;
  IF version = current_version + 1 THEN
    PREPARE upgrade_stmt FROM script;
    EXECUTE upgrade_stmt;
    UPDATE `version` SET `version` = `version` + 1 WHERE `id` = 1;
  ENDIF;
END $$
DELIMITER ;

To be called within:

CALL upgrade_version(1,'ALTER TABLE ...');

It is syntactically incorrect, mysql returns:

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 'script;
    EXECUTE upgrade_stmt;
    UPDATE `version` SET `version` = `version`' at line 7 

What is wrong?

2
  • Do you get this message at procedure definition or when you try to call it ? Commented Dec 18, 2012 at 11:27
  • I get this error when defining the procedure. Commented Dec 18, 2012 at 11:57

3 Answers 3

5

I dont understand why you are selecting that "SELECT version", If you want to select the data you have to retrieve it into a varialbe, that statement works fine when you use it directly at the terminal. And are you sure that you have prepared_stmt in your scripts..!? possible fix is :

  1. change the varible name version to version_v
  2. instead of this:

    SET version = (SELECT version FROM version WHERE id = 1 LIMIT 1);
    SELECT version;

try this:

SELECT `version` INTO Version_v FROM `version` WHERE `id` = 1 LIMIT 1);
Sign up to request clarification or add additional context in comments.

Comments

4

Try this:

DELIMITER $$
DROP PROCEDURE IF EXISTS upgrade_version $$
CREATE PROCEDURE upgrade_version(current_version INT, script TEXT)
BEGIN
  SELECT `version` into @ver FROM `version` WHERE `id` = 1 LIMIT 1;  
  SET @s = script;
  IF @ver = current_version + 1 THEN
    PREPARE upgrade_stmt FROM @s;
    EXECUTE upgrade_stmt;
    DEALLOCATE PREPARE upgrade_stmt;
    UPDATE `version` SET `version` = `version` + 1 WHERE `id` = 1;
  END IF;
END $$
DELIMITER ;

Comments

0

Set Value To Declare variable

       DELIMITER $$
       create procedure Testing()
           begin
            declare Regione int;   
            set Regione=(select  id from users
            where id=1) ;
            select Regione ;
           end $$
         DELIMITER;

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.