Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Query Browser - using variables

I'm used to MSSQL's Query Analyzer. Needing to convert some SP's for a hobby project, I'm having trouble making the transition to the mysql query browser, particularly when it comes to using variables. I'm essentially trying to simulate a procedure before it's a procedure.

So in Query Analyzer i'd write something like this...

delcare @var1 int
declare @var2 varchar(30)

set @var1 = 17 --some thing i'd normally pass to the SP
set @var2 = 'something else id pass to the SP'

SELECT * 
FROM table 
WHERE id = @var1 
OR textcolumn = @var2

Then I'd play around with the query (cause it's way more complex that the example one) until I got it right, or I'd substitute the values for the variables cause they're used like 100 times in the body of the query and that's a lot of retyping.

So my question is how to get that to work in MySQL's query browser.

I understand that it's only executing whatever statement is highlighted (the cursor is on that line or block of text). And I think i understand that the "Begin Transaction" button should be used somehow, but I can't get it to go. Here's what I have so far

DELIMITER $$

begin
declare var1 varchar(1) default 'W';
  select count(*) from gamestatspitchers where wls = var1;
end$$

delimiter ;

Thanks for any help. I can't seem to sort this out.

like image 611
Jody Avatar asked Oct 19 '09 02:10

Jody


People also ask

What is @@ in MySQL?

@@ is used for system variables. Using different suffix with @@ , you can get either session or global value of the system variable.

Can you have variables in MySQL?

The MySQL server maintains system variables that configure its operation. A system variable can have a global value that affects server operation as a whole, a session value that affects the current session, or both.


1 Answers

Delimit the queries with semicolons like this:

SELECT  @var1 := 17, @var2 := 'somethingelse';
SELECT  @var1, @var2;

The query will be highlighted when you put a cursor over it.

Then just press Ctrl-Enter to execute this very query you want to execute.

Each tab keeps its own session, so the values of @var will remain defined after you execute the first query. You can rerun the second query as many times as you want.

like image 83
Quassnoi Avatar answered Oct 23 '22 21:10

Quassnoi