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?

 
 
 
up vote445down voteaccepted

MySQL has the 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 in 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 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.)

MySQL: @variable vs. variable. Whats the difference?的更多相关文章

  1. mysql: [ERROR] unknown variable 'datadir=/var/lib/mysql'问题

    环境: Centos7,mysql 5.7 问题: 在使用命令“mysql -u root -p”连接mysql时,报:“mysql: [ERROR] unknown variable 'datadi ...

  2. Java: Class Variable/Static Variable

    1. Class Variable/Static Variable: Class variable is also known as static variable with "static ...

  3. mysql碰到unknown variable 'xxxx' 的解决方法

    在使用mysqlbinlog查看日志的时候碰到了一个问题, 错误提示如下: /usr/local/mysql/bin/mysqlbinlog: unknown variable 'default-ch ...

  4. MySQL:unknown variable 'master-host=masterIP' [ERROR] Aborting

    <span style="font-size:18px;">120401 15:45:44 [ERROR] C:\Program Files\MySQL\MySQL S ...

  5. mysql:unknown variable 'default-character-set=utf8'

    1.修改my.cnf后,执行 service mysql restart 重启数据库失败 service mysql restart Shutting down MySQL.. SUCCESS! St ...

  6. 07:mysql的unknown variable ‘xxxxx’

    简单说明一下: 可能有的找不到配置文件的,不要慌,这个时候 你可能以前安装了多个版本的mysql 就是说你以前是mysql5,现在换成了mysql8, 矮!! 你可能发现你的mysql8里面没有配置文 ...

  7. MySQL: Set user variable from result of query

    set @user = 123456;set @group = (select GROUP from USER where User = @user);select * from USER where ...

  8. Go语言规格说明书 之 变量声明(Variable/Short variable declarations)

    go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...

  9. Variable|quantitative variables|continuous variable|discrete variable|qualitative variables| observation|data set

    2.1Variables and Data Variable:某物或某人的某一特征和其他个体不同. quantitative variables:定量变量either discrete (可以被数)o ...

随机推荐

  1. OpenJDK将对Android开发产生怎样的影响?

    转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0228/182.html?1456926201 Google已决定将从下一版本的Android开始采 ...

  2. EF架构~数据分批批量提交

    回到目录 对于大数据量提交,包括插入,更新和删除,我始终不建议用EF自带的方法,因为它会增加与数据库的交互次数,一般地,EF的一个上下文在提交时会打开一个数据连接,然后把转换成的SQL语句一条一条的发 ...

  3. EF架构~扩展一个分页处理大数据的方法

    回到目录 最近总遇到大数据的问题,一次性处理几千万数据不实际,所以,我们需要对大数据进行分块处理,或者叫分页处理,我在EF架构里曾经写过类似的,那是在进行BulkInsert时,对大数据批量插入时候用 ...

  4. Docker容器入门

    为什么要看docker 从去年起就或多或少的接受了docker的熏陶,主要还是Infoq在去年有很多关于docker的实践视频讲座,记得有一篇是<Docker在雪球的技术实践>,当时听的也 ...

  5. 锋利的JQuery —— JQuery性能优化

    大图猛戳

  6. fir.im Weekly - 你与优秀源码之间只差一个 Star

    说起开源社区,Github 是一个不可缺少的存在.作为全球最大的同性交友网站,上面有太多优秀的开源代码库和编程大神,让无数开发者心生向往.那么如何正确的使用 Github,也许是编程学习之必要.来看下 ...

  7. salesforce 零基础学习(三十一)关于LookUp字段点击Save时的Validation

    今天在群里大概遇到了这样一个问题,明明这个User存在,但是save的时候提示信息说Value不存在,大概的意思如下图所示,目前认为这种问题常见的可能情况有两种. 一.此字段设置Validation ...

  8. vuejs与服务器通信

    vuejs与服务器通信 与服务器通信 Vue 实例的原始数据 $data 能直接用 JSON.stringify() 序列化.社区贡献了一个插件 vue-resource,提供一种容易的方式与 RES ...

  9. HTML5语义标签的实践

    <article> 定义一篇文章 论坛发帖 博客文章 一篇文章 <article>  <h1>标题</h1>  <p>内容</p> ...

  10. 用javascript动态创建并提交表单form,表格table

    <script> //helper function to create the formfunction getNewSubmitForm(){ var submitForm = doc ...