MySQL:@变量与变量。 有什么区别?

发布于 2024-07-25 01:07:39 字数 260 浏览 8 评论 0原文

之间有区别

@variable

:和:

variable

在我发布的另一个问题中,有人告诉我, MySQL 中的 。 他还提到 MSSQL 如何具有批处理范围,MySQL 如何具有会话范围。 有人可以为我详细解释一下吗?

In another question I posted someone told me that there is a difference between:

@variable

and:

variable

in MySQL. He also mentioned how MSSQL has batch scope and MySQL has session scope. Can someone elaborate on this for me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

街角迷惘 2024-08-01 01:07:39

MySQL 有一个用户定义变量

它们是松散类型的变量,可以在会话中的某个位置初始化并保留其值直到会话结束。

它们前面带有 @ 符号,如下所示: @var

您可以使用 SET 语句或在查询中初始化此变量

SET @var = 1

SELECT @var2 := 2

:在MySQL中开发存储过程,您可以传递输入参数并声明局部变量:

DELIMITER //

CREATE PROCEDURE prc_test (var INT)
BEGIN
    DECLARE  var2 INT;
    SET var2 = 1;
    SELECT  var2;
END;
//

DELIMITER ;

这些变量不带任何前缀。

过程变量和特定于会话的用户定义变量之间的区别在于,每次调用过程时过程变量都会重新初始化为 NULL,而特定于会话的变量则不会

CREATE PROCEDURE prc_test ()
BEGIN
    DECLARE var2 INT DEFAULT 1;
    SET var2 = var2 + 1;
    SET @var2 = @var2 + 1;
    SELECT  var2, @var2;
END;

SET @var2 = 1;

CALL prc_test();

var2  @var2
---   ---
2     2


CALL prc_test();

var2  @var2
---   ---
2     3


CALL prc_test();

var2  @var2
---   ---
2     4

:请注意,每次调用过程时,var2(过程变量)都会重新初始化,而@var2(会话特定变量)则不会。

(除了用户定义的变量之外,MySQL有一些预定义的“系统变量”,这些变量可能是“全局变量”,例如@@global.port或“session”变量”,例如@@session.sql_mode;这些“会话变量”与特定于会话的用户定义变量无关。)

MySQL has a concept of user-defined variables.

They are loosely typed variables that may be initialized somewhere in a session and keep their value until the session ends.

They are prepended with an @ sign, like this: @var

You can initialize this variable with a SET statement or inside a query:

SET @var = 1

SELECT @var2 := 2

When you develop a stored procedure in MySQL, you can pass the input parameters and declare the local variables:

DELIMITER //

CREATE PROCEDURE prc_test (var INT)
BEGIN
    DECLARE  var2 INT;
    SET var2 = 1;
    SELECT  var2;
END;
//

DELIMITER ;

These variables are not prepended with any prefixes.

The difference between a procedure variable and a session-specific user-defined variable is that a procedure variable is reinitialized to NULL each time the procedure is called, while the session-specific variable is not:

CREATE PROCEDURE prc_test ()
BEGIN
    DECLARE var2 INT DEFAULT 1;
    SET var2 = var2 + 1;
    SET @var2 = @var2 + 1;
    SELECT  var2, @var2;
END;

SET @var2 = 1;

CALL prc_test();

var2  @var2
---   ---
2     2


CALL prc_test();

var2  @var2
---   ---
2     3


CALL prc_test();

var2  @var2
---   ---
2     4

As you can see, var2 (procedure variable) is reinitialized each time the procedure is called, while @var2 (session-specific variable) is not.

(In addition to user-defined variables, MySQL also has some predefined "system variables", which may be "global variables" such as @@global.port or "session variables" such as @@session.sql_mode; these "session variables" are unrelated to session-specific user-defined variables.)

断舍离 2024-08-01 01:07:39

在 MySQL 中,@variable 表示用户-定义变量。 您可以定义自己的。

SET @a = 'test';
SELECT @a;

在存储程序之外,不带@的变量是一个系统变量,您无法自己定义。

该变量的范围是整个会话。 这意味着当您与数据库的连接存在时,仍然可以使用该变量。

这与 MSSQL 形成鲜明对比,MSSQL 中变量仅在当前批次的查询(存储过程、脚本或其他)中可用。 它不会在同一会话的不同批次中提供。

In MySQL, @variable indicates a user-defined variable. You can define your own.

SET @a = 'test';
SELECT @a;

Outside of stored programs, a variable, without @, is a system variable, which you cannot define yourself.

The scope of this variable is the entire session. That means that while your connection with the database exists, the variable can still be used.

This is in contrast with MSSQL, where the variable will only be available in the current batch of queries (stored procedure, script, or otherwise). It will not be available in a different batch in the same session.

旧城空念 2024-08-01 01:07:39

MSSQL 要求在过程中声明变量,并且人们使用 @Variable 语法 (DECLARE @TEXT VARCHAR(25) = 'text')。 此外,MS 允许在过程的任何块内进行声明,这与 MySQL 不同,MySQL 要求所有 DECLARE 都在顶部。

虽然在命令行上很好,但我觉得在 MySQL 的存储过程中使用 set = @variable 是有风险的。 没有范围和变量跨越范围边界。 这类似于 JavaScript 中不带 var 前缀声明的变量,这些变量是全局命名空间并会产生意外的冲突和覆盖。

我希望 MySQL 的好心人能够在存储过程中的各个块级别允许 DECLARE @Variable。 请注意 @(at 符号)。 @ 符号前缀有助于将变量名称与表列名称分开 - 因为它们通常是相同的。 当然,人们总是可以添加“v”或“l_”前缀,但是 @ 符号是一种方便而简洁的方法,可以使变量名称与您可能从中提取数据的列相匹配破坏它。

MySQL 是存储过程的新手,他们的第一个版本做得很好。 很高兴看到他们在这里将其形成并观察该语言的服务器端方面的成熟。

MSSQL requires that variables within procedures be DECLAREd and folks use the @Variable syntax (DECLARE @TEXT VARCHAR(25) = 'text'). Also, MS allows for declares within any block in the procedure, unlike MySQL which requires all the DECLAREs at the top.

While good on the command line, I feel using the set = @variable within stored procedures in MySQL is risky. There is no scope and variables live across scope boundaries. This is similar to variables in JavaScript being declared without the var prefix, which are then the global namespace and create unexpected collisions and overwrites.

I am hoping that the good folks at MySQL will allow DECLARE @Variable at various block levels within a stored procedure. Notice the @ (at sign). The @ sign prefix helps to separate variable names from table column names - as they are often the same. Of course, one can always add an "v" or "l_" prefix, but the @ sign is a handy and succinct way to have the variable name match the column you might be extracting the data from without clobbering it.

MySQL is new to stored procedures and they have done a good job for their first version. It will be a pleasure to see where they take it form here and to watch the server side aspects of the language mature.

单挑你×的.吻 2024-08-01 01:07:39

原则上,我在存储过程中使用 UserDefinedVariables(以 @ 开头)。 这使生活变得更轻松,特别是当我在两个或多个存储过程中需要这些变量时。 就在我只需要一个存储过程中的变量时,我使用系统变量(不带前缀@)。

@Xybo:
我不明白为什么在 StoredProcedures 中使用 @variables 应该有风险。 您能否更容易地解释一下“范围”和“边界”(对于我作为新手来说)?

In principle, I use UserDefinedVariables (prepended with @) within Stored Procedures. This makes life easier, especially when I need these variables in two or more Stored Procedures. Just when I need a variable only within ONE Stored Procedure, than I use a System Variable (without prepended @).

@Xybo:
I don't understand why using @variables in StoredProcedures should be risky. Could you please explain "scope" and "boundaries" a little bit easier (for me as a newbe)?

虐人心 2024-08-01 01:07:39

如果从用 Java、Python 等编写的应用程序调用存储过程,@variable 非常有用。
有时,变量值是在第一次调用中创建的,并且在后续调用的函数中需要。

关于 PL/SQL (Oracle) 的旁注

Oracle PL/SQL 中可以看到优势,其中这些变量有 3 个不同的作用域:

  • 函数变量,其作用域在以下情况结束:函数退出。
  • 包体变量定义在包的顶部以及范围为会话且可见性为包的所有函数之外。
  • 包变量,其变量是会话,可见性是全局的。

我在 PL/SQL 方面的经验

我开发了一种架构,其中完整的代码是用 PL/SQL 编写的。 这些是从用 Java 编写的中间件调用的。 中间件有两种类型。 一种用于满足客户端呼叫的需求,也是用 Java 编写的。 另一个是为了满足来自浏览器的调用。
客户端设施 100% 用 Ja​​vaScript 实现。 使用命令集代替 HTML 和 JavaScript 在 PL/SQL 中编写应用程序。

我一直在寻找相同的工具来将 PL/SQL 编写的代码移植到另一个数据库。 我发现的最近的一个是 Postgres。 但所有变量都有函数作用域。

对 MySQL 中 @ 的看法

我很高兴看到 MySQL 中至少有这个 @ 工具。 我不认为 Oracle 会为 MySQL 存储过程构建 PL/SQL 中可用的相同设施,因为这可能会影响 Oracle 数据库的销售。

@variable is very useful if calling stored procedures from an application written in Java , Python etc.
There are ocassions where variable values are created in the first call and needed in functions of subsequent calls.

Side-note on PL/SQL (Oracle)

The advantage can be seen in Oracle PL/SQL where these variables have 3 different scopes:

  • Function variable for which the scope ends when function exits.
  • Package body variables defined at the top of package and outside all functions whose scope is the session and visibility is package.
  • Package variable whose variable is session and visibility is global.

My Experience in PL/SQL

I have developed an architecture in which the complete code is written in PL/SQL. These are called from a middle-ware written in Java. There are two types of middle-ware. One to cater calls from a client which is also written in Java. The other other one to cater for calls from a browser.
The client facility is implemented 100 percent in JavaScript. A command set is used instead of HTML and JavaScript for writing application in PL/SQL.

I have been looking for the same facility to port the codes written in PL/SQL to another database. The nearest one I have found is Postgres. But all the variables have function scope.

Opinion towards @ in MySQL

I am happy to see that at least this @ facility is there in MySQL. I don't think Oracle will build same facility available in PL/SQL to MySQL stored procedures since it may affect the sales of Oracle database.

绿光 2024-08-01 01:07:39

用户定义变量(例如@ name, @age):

SET @name = 'John', @age := 27;
SELECT @name := 'John', @age := 27;
SELECT 'John', 27 INTO @name, @age;
  • 可以在会话范围内使用,因此注销后它们将被删除。

  • 不受事务影响,因此它们的更改值无法回滚。 *我的问题和答案详细解释它。

局部变量(例如v1, v2):

  • 有类型。

  • 可以使用DECLARE语句设置 如下图。 *必须指定类型,并且可以使用 DEFAULT 初始化局部变量,不带 DEFAULT 的局部变量初始值为 NULL :

DECLARE v1, v2 INT DEFAULT 2; -- v1 and v2 are 2.
DECLARE v1, v2 INT;           -- v1 and v2 are NULL.

User-defined variables (e.g. @name, @age):

SET @name = 'John', @age := 27;
SELECT @name := 'John', @age := 27;
SELECT 'John', 27 INTO @name, @age;
  • can be used in session scope so they are deleted after logout.

  • are not influenced by transaction so their changed values cannot be rollbacked. *My question and the answers explain it in detail.

Local variables (e.g. v1, v2):

  • have types.

  • can be set with DECLARE statement as shown below. *You must specify type and you can initialize local variables with DEFAULT and the initial values of the local variables without DEFAULT are NULL :

DECLARE v1, v2 INT DEFAULT 2; -- v1 and v2 are 2.
DECLARE v1, v2 INT;           -- v1 and v2 are NULL.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文