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
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.
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 );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With