Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql DECLARE WHILE outside stored procedure how?

Tags:

I fairly new to mysql but have MS SQL experience.

Is it possible to declare variables and use while statement outside stored procedure?

I only found examples where guys doing like this

1. procedure created
2. execute proc
3. drop proc

Suggest me the right way

like image 369
Vladimir Avatar asked Oct 18 '12 11:10

Vladimir


People also ask

How do you DECLARE a variable in SQL stored procedure?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.

How do I DECLARE multiple variables in MySQL?

DECLARE var1 int; DECLARE var2 int; DECLARE var3 int; SELECT var1:=id, var2:=foo, var3:=bar from page WHERE name="bob"; CALL someAwesomeSP (var1 , var2 , var3 );


1 Answers

No, you cannot do it. You can use these statements only inside BEGIN...END clause.

So, it is possible in stored procedures/functions, triggers and events.

More information here - MySQL Compound-Statement Syntax.

like image 79
Devart Avatar answered Oct 25 '22 14:10

Devart