user3848891
user3848891

Reputation: 91

Difference between declare @variablename and declare variablename while working with stored procedures

I wanted to know what is the difference between @variablename and variablename. All the help would be appreciated.

Upvotes: 1

Views: 604

Answers (1)

catalinetu
catalinetu

Reputation: 660

variablename

Local variables

Local variables are declared within stored procedures and are only valid within the BEGIN…END block where they are declared. Local variables can have any SQL data type.

@variablename

User variables

In MySQL stored procedures, user variables are referenced with an ampersand (@) prefixed to the user variable name (for example, @x and @y). You can store a value in a user-defined variable in one statement and then refer to it later in another statement. This enables you to pass values from one statement to another. User-defined variables are session-specific. That is, a user variable defined by one client cannot be seen or used by other clients. All variables for a given client session are automatically freed when that client exits.

In MySQL the variable var1 and @var1 are two different things. @var1 is a user session variable which remains for the duration of the active session. The other is local to the stored procedure.

Upvotes: 2

Related Questions