define variable in mysql code example

Example 1: how to create a variable in mysql

-- MySQL

SET @variable_name := value;

Example 2: mysql declare variable

SET start = 1;
SET finish = 10;

SELECT * FROM places WHERE place BETWEEN start AND finish;

Example 3: how to put value in variable mysql

SET variable = (SELECT SUM(tax) FROM taxable_transactions);

Example 4: mysql function variable

CREATE PROCEDURE p(increment INT)
BEGIN
  DECLARE counter INT DEFAULT 0;
  WHILE counter < 10 DO
    -- ... do work ...
    SET counter = counter + increment;
  END WHILE;
END;

Tags:

Sql Example