Skip to content

SQL Server batch variables are lost between statements #3078

Description

@KIENKK-LP

What happened?

Query

DECLARE @sn NVARCHAR(50) = '2404GQV000066A00105';

SELECT *
FROM serialnew
WHERE [S/N] = @sn;

SELECT *
FROM [v_wms_joined]
WHERE [S/N] = @sn;

SELECT *
FROM drm_report_n
WHERE [Serial number] = @sn;

SELECT *
FROM serial_existed
WHERE sn_code = @sn;

Actual behavior
TablePro reports:
Statement 2/5 failed: Must declare the scalar variable "@sn".
Expected behavior
The complete SQL Server script should be sent as one T-SQL batch, so
@sn remains available to all following statements. Each SELECT result
should still be displayed separately if possible.

Investigation
The query editor currently appears to split the script at semicolons using
SQLStatementScanner, then executes each statement separately. SQL Server
local variables only live within a single batch, so @sn declared in the
first statement is unavailable to later statements.
Relevant code:

Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementScanner.swift
TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift
Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift
SQLScriptText already has SQL Server-specific GO batch handling for
scripts, but that path does not seem to be used by the query editor.

Could you please review whether SQL Server query execution should preserve
T-SQL batches containing DECLARE, SET, IF, BEGIN/END, and similar
batch-scoped constructs?

### Steps to reproduce

_No response_

### Expected behavior

_No response_

### Database type

SQL Server

### TablePro version

Version 0.75.0 (131)

### macOS version & chip

macOS 27.0

### Screenshots / Logs

_No response_

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions