Skip to content

Conversation

@dlevy-msft-sql
Copy link
Contributor

@dlevy-msft-sql dlevy-msft-sql commented Jan 25, 2026

Summary

This PR allows the -q (initial query) flag to be used with -i (input files), enabling users to set session options before running scripts.

Changes

  • Modified validation to only block -i with -Q (not -q)
  • Updated run logic to execute -q first, then process -i files
  • Updated -i flag description to document the new behavior
  • Added tests for the combined usage

Usage

# Set session options before running a script
sqlcmd -S server -q "SET PARSEONLY ON" -i script.sql

# Set multiple session options
sqlcmd -S server -q "SET NOCOUNT ON; SET ANSI_NULLS ON" -i script.sql

The initial query runs first, then the input files are processed.

Notes

  • The -Q flag (query and exit) remains mutually exclusive with -i since -Q causes sqlcmd to exit immediately after the query
  • This aligns with ODBC sqlcmd behavior

Fixes #389

Previously -q and -i were mutually exclusive. Now the initial query (-q)
runs first, then the input files (-i) are processed. This is useful for
setting session options before running scripts.

Example:
  sqlcmd -S server -q 'SET PARSEONLY ON' -i script.sql

The -Q flag (query and exit) remains mutually exclusive with -i since
-Q causes sqlcmd to exit immediately after the query.

Fixes microsoft#389
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enables the -q (initial query) flag to work alongside -i (input files), allowing users to set session options before running scripts. Previously, -q was mutually exclusive with -i, but now the initial query runs first, followed by the input files. The -Q (query and exit) flag remains mutually exclusive with -i as expected.

Changes:

  • Modified validation to allow -i with -q while keeping -i and -Q mutually exclusive
  • Updated run logic to execute -q before processing -i files
  • Updated flag descriptions and documentation

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
cmd/sqlcmd/sqlcmd.go Modified validation logic to distinguish between -q and -Q, updated run logic to execute initial query before input files, and updated flag description
cmd/sqlcmd/sqlcmd_test.go Updated test expectations to reflect that only -Q (not -q) is mutually exclusive with -i, added test for -q with -i combination
README.md Added documentation for the new feature with example usage

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Fix typo: '-i' instead of 'i' in mutuallyExclusiveError call
- Add InitialQuery check to prevent double s.Run() when using -q alone
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@dlevy-msft-sql dlevy-msft-sql self-assigned this Jan 25, 2026
@dlevy-msft-sql dlevy-msft-sql added sqlcmd switch switch in existing sqlcmd Size: S Small issue (less than one week effort) labels Jan 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Size: S Small issue (less than one week effort) sqlcmd switch switch in existing sqlcmd

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make -q and -i work together

1 participant