MySQL: @variable vs. variable. Whats the difference?

Source: Internet
Author: User

MySQL: @variable vs. variable. Whats the difference?
Up vote351down votefavorite121

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

@variable

And:

variable

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

Add a Comment
Up vote445down voteaccepted

MySQLHas the concept of user-defined variables.

They is loosely typed variables that may is initialized somewhere in a session and keep their value until the session end S.

They @ is prepended with a sign, like this:@var

Can initialize this variable with a SET statement or inside in a query:

SET @var = 1SELECT @var2 := 2

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

 delimiter //create  procedure Prc_test  ( var int begin declare< Span class= "PLN" > var2 int;set var2 = 1  select Var2;end; //delimiter ;         

These variables is not prepended with any prefixes.

The difference between a procedure variable and a session-specific user-defined variable is this procedure variable is REI Nitialized to each time NULL the procedure are called, while the session-specific variable are not:

CREATE PROCEDUREPrc_test()BEGIN DECLAREVar2 INTDEFAULT 1; SETVar2:=Var2+ 1; SET @Var2:= @Var2+ 1; SELECTVar2, @Var2;END;SET @var2 = 1; Call Prc_test@var2------2< Span class= "PLN" > 2call prc_test (); @var2------2< Span class= "PLN" > 3call prc_test (); @var2------2< Span class= "PLN" > 4            

As can see, var2 (procedure variable) are reinitialized each time the procedure are called, while @var2 (SESSION-SPECIF IC variable) is not.

(in addition to user-defined variables, MySQL also have some predefined "system variables", which may "Global V Ariables "such as @@global.port or" session variables "such as @@session.sql_mode ; these" session variables "is unrelated to session-specific user-defined variables.)

MySQL: @variable vs. variable. Whats the difference?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.