Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL local variables

I am trying to define and initialize a MySQL variable for a query.

I have the following:

declare @countTotal int; SET @countTotal = select COUNT(*)  from nGrams; 

I am using MySQL in Netbeans and it tells me I have an error. What/where is my error?

How can I fix this?

like image 536
CodeKingPlusPlus Avatar asked Dec 02 '12 15:12

CodeKingPlusPlus


People also ask

Can you declare variables in MySQL?

MySQL allows you to declare two or more variables that share the same data type using a single DECLARE statement. The following example declares two integer variables x and y , and set their default values to zero.

How do I assign a variable in MySQL?

MySQL variable assignment There are two ways to assign a value to a user-defined variable. You can use either := or = as the assignment operator in the SET statement. For example, the statement assigns number 100 to the variable @counter. The second way to assign a value to a variable is to use the SELECT statement.

Where are variables stored in MySQL?

You can store a value in a user-defined variable in one statement and refer to it later in another statement. This enables you to pass values from one statement to another. User variables are written as @ var_name , where the variable name var_name consists of alphanumeric characters, . , _ , and $ .

What are local variables?

A local variable is a type of variable that can be used where the scope and extent of the variable is within the method or statement block in which it is declared. It is used as an iteration variable in the foreach statement, exception variable in the specific-catch clause and resource variable in the using statement.


2 Answers

MySQL has two different types of variable:

  • local variables (which are not prefixed by @) are strongly typed and scoped to the stored program block in which they are declared. Note that, as documented under DECLARE Syntax:

    DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

  • user variables (which are prefixed by @) are loosely typed and scoped to the session. Note that they neither need nor can be declared—just use them directly.

Therefore, if you are defining a stored program and actually do want a "local variable", per the wording in your question, you will need to drop the @ character and ensure that your DECLARE statement is at the start of your program block. Otherwise, to use a "user variable", drop the DECLARE statement.

Furthermore, you will either need to surround your query in parentheses in order to execute it as a subquery:

SET @countTotal = (SELECT COUNT(*) FROM nGrams); 

Or else, you could use SELECT ... INTO:

SELECT COUNT(*) INTO @countTotal FROM nGrams; 
like image 78
eggyal Avatar answered Sep 20 '22 14:09

eggyal


Try this:-

 select @countTotal := COUNT(*) from nGrams; 
like image 27
Rahul Tripathi Avatar answered Sep 19 '22 14:09

Rahul Tripathi