SQL - DECLARE和SET之间的区别

时间:2022-05-08 13:28:02

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

使用SQL(或更具体的MySQL)DECLARE和SET之间有什么区别?

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

两者都可以设置变量。我已阅读MySQL文档,我不明白两者之间的区别。

2 个解决方案

#1


4  

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.

DECLARE不初始化变量。声明它时,您声明变量名称,类型和默认值,它可以是表达式。

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

SET用于初始化您之前声明的变量,并且在DECLARE它之前不能设置变量。

#2


3  

DECLARE defines the variable type and SET initializes the variable.

DECLARE定义变量类型,SET初始化变量。

DECLARE @Var1 INT;
DECLARE @Var2 INT;
SET @Var1 = 1;
SET @Var2 = 2;

#1


4  

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.

DECLARE不初始化变量。声明它时,您声明变量名称,类型和默认值,它可以是表达式。

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

SET用于初始化您之前声明的变量,并且在DECLARE它之前不能设置变量。

#2


3  

DECLARE defines the variable type and SET initializes the variable.

DECLARE定义变量类型,SET初始化变量。

DECLARE @Var1 INT;
DECLARE @Var2 INT;
SET @Var1 = 1;
SET @Var2 = 2;