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. 大叔也说Xamarin~Android篇~调用远程API接口,发POST请求

    回到目录 Xamarin我们在上节已经教大家如何去部署它的环境了,今天来说一个实际的例子,使用android客户调用.net web api的一个接口,并发送POST请求,当服务端回到请求后做出响应, ...

  2. 关于STM32的外部引脚中断的问题

    今天想用自己以前的比较干净的工程模板做一个东西,,,,,,,在添加上引脚中断的时候,,突然想知道自己配置的中断优先级是否正确执行,,,,, 以前刚学习32的时候测试过是可以的,,不过今天发现了一个大问 ...

  3. Android 控件架构及View、ViewGroup的测量

    附录:示例代码地址 控件在Android开发的过程中是必不可少的,无论是我们在使用系统控件还是自定义的控件.下面我们将讲解一下Android的控件架构,以及如何实现自定义控件. 1.Android控件 ...

  4. 阿里云ecs Linux下安装MySQL后设置root密码 【转】

    方法一:最简单的方法,也是安装完mysql后,系统提示的方法.使用mysqladmin来完成.shell> mysqladmin -u root password "newpwd&qu ...

  5. 快速入门系列--CLR--03泛型集合

    .NET中的泛型集合 在这里主要介绍常见的泛型集合,很多时候其并发时的线程安全性常常令我们担忧.因而简述下.NET并发时线程安全特性,其详情请见MSDN. 普通集合都不支持多重并发写操作 部分支持单线 ...

  6. 线程池ThreadPoolExecutor、Executors参数详解与源代码分析

    欢迎探讨,如有错误敬请指正 如需转载,请注明出处 http://www.cnblogs.com/nullzx/ 1. ThreadPoolExecutor数据成员 Private final Atom ...

  7. Hello Netgen

    Hello Netgen eryar@163.com 摘要Abstract:本文主要介绍如何对下载的Netgen源码进行编译生成Netgen程序和程序开发所需要的库nglib. 关键字Key Word ...

  8. [OpenCV] Samples 06: [ML] logistic regression

    logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...

  9. spring 源码分析之BeanPostProcessor

    1.官方解答: Factory hook that allows for custom modification of new bean instances, e.g. checking for ma ...

  10. 可变参数宏__VA_ARGS__

    在 GNU C 中,宏可以接受可变数目的参数,就象函数一样,例如:#define pr_debug(fmt,arg...) \printk(KERN_DEBUG fmt,##arg) 用可变参数宏(v ...