Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(api): preserve MSSQL batch semantics for declared variables (#18895)

rebelice1c0a237e32eb
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index e27764ef2a..d4f4a258e9 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -733,6 +733,14 @@ func queryRetryStopOnError(
optionalAccessCheck accessCheckFunc,
schemaSyncer *schemasync.Syncer,
) ([]*v1pb.QueryResult, []*parserbase.QuerySpan, time.Duration, error) {
+ // MSSQL requires statements to be executed as a batch to preserve variable scope.
+ // For example, "DECLARE @meta int = 1; SELECT @meta" must be sent as one batch,
+ // otherwise the second statement will fail with "variable not defined" error.
+ // The queryRetry function will handle splitting internally via GetQuerySpan and queryBatch.
+ if instance.Metadata.GetEngine() == storepb.Engine_MSSQL {
+ return queryRetry(ctx, stores, user, instance, database, driver, conn, statement, queryContext, licenseService, optionalAccessCheck, schemaSyncer)
+ }
+
// Split the statement into individual SQLs
statements, err := parserbase.SplitMultiSQL(instance.Metadata.GetEngine(), statement)
if err != nil {