MySQL: @variable vs. variable. Whats the difference?
2016-09-09 08:05
579 查看
MySQL: @variable vs. variable. Whats the difference?
up vote351down votefavorite 121 | 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? |
add a comment |
up vote445down voteaccepted | MySQLhas 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 SETstatement 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 NULLeach 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.portor "session variables" such as @@session.sql_mode; these "session variables" are unrelated to session-specific user-defined variables.) |
相关文章推荐
- Http vs. Https: What’s the difference?
- setEnabled() vs setClickable(), what is the difference?
- Armoured Cable vs. Unarmoured Cable: What's The Difference?
- Mutex vs. Semaphore, what is the difference?
- swapCursor vs changeCursor, what’s the difference?
- Network Switch vs. Network Hub: What’s The Difference?
- Stack vs. Heap: What's the difference?
- OLAP vs OLTP: what makes the difference
- What's the difference of name scope and a variable scope in tensorflow?
- What are the differences between a pointer variable and a reference variable in C++?
- What is the difference between BIT and TINYINT in MySQL
- OLAP vs OLTP: what makes the difference
- What is the difference between a function expression vs declaration in JavaScript?
- SODIMM Vs. UDIMM: What Is the Difference?
- What's the difference between tf.placeholder and tf.Variable
- What are the differences between a pointer variable and a reference variable in C++?
- What's the difference of name scope and a variable scope in tensorflow?
- Mutex vs. Semaphore, what is the difference?
- FITTING A MODEL VIA CLOSED-FORM EQUATIONS VS. GRADIENT DESCENT VS STOCHASTIC GRADIENT DESCENT VS MINI-BATCH LEARNING. WHAT IS THE DIFFERENCE?
- FDMA vs TDMA vs CDMA: What's the difference?