Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
# SSMS Executor

SQL Server Management Studio (SSMS) extension for executing the current statement based on the cursor position.
SQL Server Management Studio (SSMS) extension for executing the current statement based on the cursor position — no need to highlight text first.

If your script contains multiple statements, position the cursor within the desired statement and press `ctrl+shift+e` to execute it.
## Features

### Execute Current Statement

If your script contains multiple SQL statements, place your cursor anywhere within the statement you want to run and press `Ctrl+Shift+E`. The extension automatically detects the statement boundaries and executes only that statement, leaving the rest of your script untouched. Your original cursor position and any text selection are restored after execution.

This is available from the **Tools** menu (**Execute Statement**) or from the toolbar.

### Execute Inner Statement

When working with compound or nested SQL blocks (such as `BEGIN...END`), the **Execute Inner Statement** command lets you run just the innermost statement at the cursor position rather than the entire block. This is useful for debugging or testing individual statements inside stored procedures, `IF` blocks, or transaction wrappers.

This is available from the **Tools** menu (**Execute Inner Statement**).

### Options

You can configure the extension under **Tools > Options > SSMS Executor**:

| Option | Description |
|---|---|
| **Execute inner statements** | When enabled, the default `Ctrl+Shift+E` shortcut will execute the inner statement instead of the full block. |

# Documentation

Expand All @@ -17,5 +37,5 @@ If your script contains multiple statements, position the cursor within the desi
You can download the extension from the [Releases section](https://github.com/tkwj/ssms-executor/releases)
or build it yourself using the provided source code.

This version only supports SSMS 21 & 22
For prior SSMS versions, please consult the original version here https://github.com/devvcat/ssms-executor/
This version only supports SSMS 21 & 22.
For prior SSMS versions, please consult the original version here: https://github.com/devvcat/ssms-executor/
8 changes: 5 additions & 3 deletions SSMSExecutor/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public Executor(DTE2 dte)

_document = dte.GetDocument();

if (_document == null) throw new InvalidOperationException("No active document.");

SaveActiveAndAnchorPoints();
}

Expand Down Expand Up @@ -124,7 +126,7 @@ private bool IsCaretInsideStatement(TSqlStatement statement, VirtualPoint caret)

if (caret.Line >= ft.Line && caret.Line <= lt.Line)
{
var isBeforeFirstToken = caret.Line == ft.Line && caret.LineCharOffset < ft.Column;
var isBeforeFirstToken = caret.Line == ft.Line && caret.LineCharOffset < ft.Column + 1;
var isAfterLastToken = caret.Line == lt.Line && caret.LineCharOffset > lt.Column + lt.Text.Length;

if (!(isBeforeFirstToken || isAfterLastToken))
Expand All @@ -146,13 +148,13 @@ private TextBlock GetTextBlockFromStatement(TSqlStatement statement)
StartPoint = new VirtualPoint
{
Line = ft.Line,
LineCharOffset = ft.Column
LineCharOffset = ft.Column + 1
},

EndPoint = new VirtualPoint
{
Line = lt.Line,
LineCharOffset = lt.Column + lt.Text.Length
LineCharOffset = lt.Column + lt.Text.Length + 1
}
};
}
Expand Down
3 changes: 2 additions & 1 deletion SSMSExecutor/ExecutorPackage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio;
using Devvcat.SSMS.Options;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.Win32;
using System;
Expand Down
11 changes: 8 additions & 3 deletions SSMSExecutor/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ static class Helpers
{
public static bool HasActiveDocument(this DTE2 dte)
{
if (dte != null && dte.ActiveDocument != null)
try
{
var doc = (dte.ActiveDocument.DTE)?.ActiveDocument;
return doc != null;
if (dte != null && dte.ActiveDocument != null)
{
var doc = (dte.ActiveDocument.DTE)?.ActiveDocument;
return doc != null;
}
}
catch
{ }

return false;
}
Expand Down
9 changes: 6 additions & 3 deletions SSMSExecutor/SSMSExecutor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CopyVsixExtensionFiles>True</CopyVsixExtensionFiles>
<CopyVsixExtensionLocation>C:\Program Files\Microsoft SQL Server Management Studio 21\Release\Common7\IDE\Extensions\SSMSExecutor</CopyVsixExtensionLocation>
<CopyVsixExtensionLocation>C:\temp\SSMSExecutor</CopyVsixExtensionLocation>
<DeployExtension>False</DeployExtension>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
Expand Down Expand Up @@ -89,12 +89,15 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SqlServer.TransactSql.ScriptDom">
<Version>170.128.0</Version>
<Version>180.6.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.SDK" Version="17.14.40265" ExcludeAssets="runtime" NoWarn="NU1604">
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.14.2094" NoWarn="NU1604" />
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="18.5.40034" NoWarn="NU1604">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\ExecutorCommand.png" />
Expand Down
Loading