Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring and using MySQL varchar variables

I'm trying to do some simple manipulations with variables in MySQL 5.0 but I can't quite get it to work. I've seen many (very!) different syntaxen for DECLARE/SET, I'm not sure why... in any case I'm presumably confusing them/picking the wrong one/mixing them.

Here's a minimal fragment that fails:

DECLARE FOO varchar(7); DECLARE oldFOO varchar(7); SET FOO = '138'; SET oldFOO = CONCAT('0', FOO);  update mypermits     set person = FOO   where person = oldFOO; 

I've also tried wrapping it with BEGIN... END; and as a PROCEDURE. In this case MySQL Workbench helpfully tells me: "SQL syntax error near ')'" on the first line and "SQL syntax error near 'DECLARE oldFOO varchar(7)'" on the second. Otherwise it gives both lines as errors in full, with "SQL syntax error near ..." on both.

Edit: I forgot to mention that I've tried it with and without @s on the variables. Some resources had it with, others without.

What dumb mistake am I making?

like image 977
Charles Avatar asked Oct 20 '10 19:10

Charles


People also ask

How do I declare a variable in MySQL?

Mysql also supports the concept of User-defined variables, which allows passing of a value from one statement to another. A user-defined variable in Mysql is written as @var_name where, var_name is the name of the variable and can consist of alphanumeric characters, ., _, and $.

What is varchar in MySQL?

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.

Can we declare variable in MySQL query?

You can declare a variable using @anyVariablename which is a session variable. To create a session variable, you need to use SET command.


2 Answers

This works fine for me using MySQL 5.1.35:

DELIMITER $$  DROP PROCEDURE IF EXISTS `example`.`test` $$ CREATE PROCEDURE `example`.`test` () BEGIN    DECLARE FOO varchar(7);   DECLARE oldFOO varchar(7);   SET FOO = '138';   SET oldFOO = CONCAT('0', FOO);    update mypermits      set person = FOO    where person = oldFOO;  END $$  DELIMITER ; 

Table:

DROP TABLE IF EXISTS `example`.`mypermits`; CREATE TABLE  `example`.`mypermits` (   `person` varchar(7) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;  INSERT INTO mypermits VALUES ('0138');  CALL test() 
like image 112
OMG Ponies Avatar answered Oct 17 '22 07:10

OMG Ponies


I ran into the same problem using MySQL Workbench. According to the MySQL documentation, the DECLARE "statement declares local variables within stored programs." That apparently means it is only guaranteed to work with stored procedures/functions.

The solution for me was to simply remove the DECLARE statement, and introduce the variable in the SET statement. For your code that would mean:

-- DECLARE FOO varchar(7);  -- DECLARE oldFOO varchar(7);  -- the @ symbol is required SET @FOO = '138';  SET @oldFOO = CONCAT('0', FOO);  UPDATE mypermits SET person = FOO WHERE person = oldFOO; 
like image 24
Amos Long Avatar answered Oct 17 '22 06:10

Amos Long