MySQL: @variable vs. variable. What's the difference?

前端 未结 4 1180
梦毁少年i
梦毁少年i 2020-11-22 01:28

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

@variable

and:

variable

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 01:56

    In MySQL, @variable indicates a user-defined variable. You can define your own.

    SET @a = 'test';
    SELECT @a;
    

    Outside of stored programs, a variable, without @, is a system variable, which you cannot define yourself.

    The scope of this variable is the entire session. That means that while your connection with the database exists, the variable can still be used.

    This is in contrast with MSSQL, where the variable will only be available in the current batch of queries (stored procedure, script, or otherwise). It will not be available in a different batch in the same session.

提交回复
热议问题