views:

320

answers:

3

I mainly use the MySQL GUI tools. This allows me to easily see the results in a table as well as to quick edits and bookmark frequently run queries. This suits my needs far better than the command line.

I remember when I used to do this on Oracle DBs years ago I could put variables in the query itself, so that when running the query I got prompted for the variable.

e.g.

select email from users where login = [VAR]

And when you run the query the system prompts you for VAR and you can type in john_smith14 and it executes the query. This is really useful for adhoc queries which you run a lot.

Yes I know using shell scripts and the command line this could be done more easily, but for several reasons aside from this, shell scripts are not a good solution for me.

A: 

I'm not sure if there's a way to get the GUI tools to prompt you for a value, but you can certainly use variables in MySQL.

SET @myVar='john_smith14';
SELECT email FROM users WHERE login = @myVar;

That might even suit you better, since you don't have to keep typing in the variable value each time..?

nickf
IIRC, this is not a good solution for MySQL GUI tools, because it resets the connection after each transaction and user variables are lost.
Bill Karwin
really? nuts. there must be a way to do it though, right?
nickf
Okay never mind I just tried it and it works fine. I must be remembering behavior from an old version of MySQL Query Browser.
Bill Karwin
lol well i can't get it to work on my version!
nickf
Hi thanks for that. I've used that in some queries where there are a few different variables. But the particular nature of the queries is that they are very adhoc, so myVar changes ever time, which is why the user prompting is really what I need. This way I can give the query to non techs too.
sevitzdotcom
Hmm just tried that and got this errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';SELECT email FROM users WHERE id = @myVar' at line 1
sevitzdotcom
+1  A: 

Ok, a different solution, since it appears Bill is right (read the comments on my other answer).

In the Params tab in the bottom right, you can right click the "Local Params" folder and add a new parameter. Give it a name, eg: "myTest". Initially it is given a value of NULL. Double click on NULL and type in a new value.

Now you can access it in your query like this:

SELECT email FROM users WHERE login = :myTest;

To make this persist between sessions (opening and closing the query browser), just make it a global parameter instead of a local parameter. This works even if you restart the MySQL server.

nickf
That's really good and a step in the right direction, but is there anyway I can get it to store the Local Params so I don't jave to create them every time I close and open MySQL gui tools?
sevitzdotcom
yep - make it a Global parameter instead of a local one. *edits the answer*
nickf
Cool but I notice you can even put params inside string literals: e.g. SELECT :id, ':id'; Which means they're not true SQL query parameters, it's just QB doing string substitution before executing the query. Makes sense because MySQL does not support named query parameters.
Bill Karwin
A: 

Using prepared statements might be useful for you in this case.

PREPARE query1 FROM select email from users where login = ?

then execute it with your variable:

SET @a = 'john';
EXECUTE query1 USING @a;

This statement will be there during your whole session, and dropped when you disconnect.

This might seem like much overhead, but is useful when using the same query over and over again, with slightly different values.

http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html

jishi
Thanks, that's useful for some other things we do, even if in itself it doesn't solve the problem.Need to check how I can do this through the GUI though.
sevitzdotcom