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. 早安Visual Studio!一次重构之旅,夏洛特烦恼

    vs问题描述 我的IDE版本是vs2013,今天新开发了一个功能,是一个接口程序,当F5调试时,出现了莫名的错误,为什么呢?因为vs弹出了下面的一个框,只说是“未将对象引用设置到对象实例“. 点击”确 ...

  2. 将不确定变为确定~DateTime.MinValue和MaxValue引发的异常

    回到目录 问题描述: SqlDateTime 溢出.必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间 概念相关 .Net中的DateTime结 ...

  3. 深度解析SDN——利益、战略、技术、实践(实战派专家力作,业内众多专家推荐)

    深度解析SDN——利益.战略.技术.实践(实战派专家力作,业内众多专家推荐) 张卫峰 编   ISBN 978-7-121-21821-7 2013年11月出版 定价:59.00元 232页 16开 ...

  4. fir.im Weekly - 技术人也要苦练“七十二变”

    一年又一年,Code,Build,Run.多少技术人像"孙悟空"一样,日复一日苦练"七十二变",笑对"八十一难",最后能"取经成功 ...

  5. js判断函数是否存在、判断是否为函数

    代码: <script type="text/javascript"> //判断是否为函数 try { if(typeof FunName === "func ...

  6. SQL Server 2014云特性:无缝集成公有云

    本篇是我在IT168的约稿,原文地址:http://tech.it168.com/a2014/0620/1637/000001637358_all.shtml       IT行业已经进入了云时代,未 ...

  7. OpenCascade Primitives BRep - Box

    OpenCascade Primitives BRep - Box eryar@163.com Abstract. BRep is short for Boundary Representation. ...

  8. Uvaoj 11624 - Fire!

    /************************************************************************* > File Name: test.cpp ...

  9. Yii的学习(4)--Active Record

    摘自Yii官网:http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.ar 在官网原文的基础上添加了CDbCriteria的详细用法. 虽然 ...

  10. Unity3D 中的三个Update()方法

            MonoBehaviour.Update 更新 当MonoBehaviour启用时,其Update在每一帧被调用. MonoBehaviour.FixedUpdate 固定更新 当Mo ...