views:

425

answers:

7

As a long time Toad for Oracle user, I have gotten used to hitting Ctrl+Enter and having just the statement under the cursor be executed.

In SQL Server Management Studio, hitting F5 runs the entire script. To run just the current statement, I have to manually highlight the statement I want, and then hit F5.

That is really annoying to me. Does anyone know of a tool with a keyboard shortcut to run just the current statement on a SQL Server? I would change tools just for this one feature.

Note: Oddly enough, even the free Toad for SQL Server does not let you run just the statement under the cursor.

+1  A: 

Hit Ctrl-E while text is highlighted.

Chris McCall
Ok, but I still have to manually highlight what I want. My real goal is to be able to type a statement and then hit some key that will execute just what is under the cursor.
JosephStyons
for me, F5 is easier to hit while the text is highlighted then "CTRL-E".
KM
A: 

If it's the having to move your hand to the mouse part that bothers you, you can hold the Ctrl down while hitting an up or down arrow key to select a line at once.

Joel Coehoorn
You mean Shift, yes?And that is tedious for larger statements.Thanks for the idea though.
JosephStyons
+1  A: 

I use a workaround: I comment queries I'm not using. You can use CTRL-K, CTRL-C to comment the SQL you've highlighted. Use CTRL-K, CTRL-U to uncomment. That way, you can comment all other queries, and execute the one you're interested in with F5.

Andomar
This is a total pain in the butt. Why should a SQL Editor make me comment out everything I don't want? Can I not just positively state "run this one command" without grabbing the mouse or pushing "Shift+Up" 35 times?
JosephStyons
Just to clarify, thanks for the answer; I appreciate it. I'm really just griping at the SQL Server Mgmt Studio interface.
JosephStyons
If you find a better way, be sure to post it here :)
Andomar
A: 

you could always use the command line tools sqlcmd and osql. I did a lot of sybase all at the unix command line using a wrapper function that passed my command string into the equivalent (which i think was isql?). I used vi to, so maybe I was just crazy then ;-)

KM
Actually that won't help. SQLCMD will not send the command to SQL Server until the batch is ended. (Normally indicated with GO.)
Shannon Severance
+2  A: 

Ok, so what I'm getting from all these answers is "No, that is not possible."

Edit:

Here is how I was able to do this:

1 - Download SQL Developer

2 - Download the jTDS driver

3 - Follow these instructions to add that driver to SQL Developer

4 - Connect to SQL Server using SQL Developer (cool!)

5 - Run it and life is good

JosephStyons
+2  A: 

I don't think this is possible to do using just the management studio. BUT you can use keyboard shortcut software (e.g. http://www.autohotkey.com/) to have a special sequence recorded and assigned to a hotkey. In your case you need:

<home><shift-end><F5>

This will select the current line and execute it.

DmitryK
A: 

Note: Oddly enough, even the free Toad for SQL Server does not let you run just the statement under the cursor.

It is really annoying that TOAD does not hold to what it promises:

From TOAD help: [F9 to execute] a portion of a statement, which can contain one or more statements. You can select the portion of the statement by placing the cursor within or adjacent to the statement, or by selecting the statement. Note: Toad considers "adjacent" all statements (including comments) separated from the cursor or from each other by fewer than two blank lines. If an error occurs during statement execution, an error message displays, allowing you to either ignore the error and continue or to abort execution.

I tried it million times but it simply executes the whole script. I wanted to search for it in the user support group (toadss(at)yahoogroups.com) but yahoo has the stupidest search facility ever! It can't even find the keyword "toad" in the toad mail group, DOH!

I think TOAD is the best querying tool ever, but the lack of this feature really annoys me too.

UPDATE: SOLUTION FOUND! I asked this issue in toadss mail group and got the answer. Unlike Oracle, you have to separate statements in SQL Server with the keyword GO after each statement. Only if you do that the F9 button works as expected, executing the current statement.

ercan