Difference between DECLARE and SET in SQL

C H picture C H · Nov 19, 2013 · Viewed 8.5k times · Source

What's the difference between DECLARE and SET using SQL (or more specifically MySQL)?

Both can set variables it seems. I have read the MySQL docs and I did not understand the difference between the two.

Answer

Hituptony picture Hituptony · Nov 19, 2013

DECLARE does not initialize the variable. When you declare it you declare the variable name, the type, and a default value, which could be an expression.

SET is for initializing the variable you declared previously, and you cannot SET the variable until you DECLARE it.