"@"存储过程中的符号? [英] "@" symbol in stored procedure?

查看:658
本文介绍了"@"存储过程中的符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在线查找此问题的答案,但找不到任何明确的解释:

I tried finding an answer to this online, but could not find any clear explanation:

存储过程中的@是否用于某种特殊目的/特别表示某些东西?对于何时使用它,我有些困惑,因为示例在用法上似乎有所不同.

Does the @ in a stored procedure serve some sort of special purpose/signify something in particular? I am a little confused as to when we use it, since examples seem to vary on its usage.

例如,在下面的示例中,使用@:

For instance in the following example @ is used:

DELIMITER $

DROP PROCEDURE IF EXISTS emp_count_2;

CREATE PROCEDURE emp_count_2(OUT param1 INT)
BEGIN
 SELECT COUNT(*) INTO param1 FROM Employee;
END
$
DELIMITER ;


/* To invoke this procedure use the mysql command statement
   CALL emp_count_2(@empCount);
   SELECT @empCount; 
*/

再一次,此示例中的@是否有某种特殊用途,还是我们可以删除@并仅使用常规变量名?

Once again, does the @ in this example serve some sort of special purpose, or can we remove the @ and just use normal variable names?

**我正在使用MySql

** I am using MySql

推荐答案

MySQL中的@variable语法表示用户定义的会话变量.您可以在存储过程外部设置这些用户变量,但也可以在存储过程内部设置它们,其结果是,在过程调用返回后,变量将保留该值.

The @variable syntax in MySQL denotes a user-defined session variable. You can set these user variables outside a stored procedure, but you can also set them inside a stored procedure, and the effect is that the variable retains the value after your procedure call returns.

因此,在您的示例中,以下内容也将执行相同的操作:

So in your example, the following would also do the same thing:

CREATE PROCEDURE emp_count_2()
BEGIN
 SELECT COUNT(*) INTO @empCount FROM Employee;
END

CALL emp_count_2(); /* sets @empCount as a side-effect */
SELECT @empCount;

多个会话以这种方式并发设置用户变量是可以的,因为用户变量的作用域是单个会话,并且并发会话可能具有相同名称但具有不同值的变量.

It's okay for multiple sessions to set the user variable in this way concurrently, because user variables are scoped to a single session, and concurrent sessions may have variables of the same name, but with different values.

不带@前缀的变量语法用于过程本地变量,或者是过程参数,或者是用

The variable syntax with no @ prefix is for variables local to the procedure, either procedure parameters, or else local variables declared with DECLARE within the procedure body.

如果要多次调用一个过程并将结果存储在单独的用户变量中,则可以使用这种用法(将用户变量作为参数传递并在过程主体中进行分配).否则,每次调用该过程都会覆盖当前会话的@empCount用户变量中的先前值.

This usage you have, passing a user variable as a parameter and assigning it in the body of the procedure, is useful if you want to call a procedure several times and store the result in separate user variables. Otherwise each call to the procedure would overwrite the previous value in the @empCount user variable for the current session.

这篇关于"@"存储过程中的符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆