@@ -211,12 +211,35 @@ func (c Commands) SetBatchTerminator(terminator string) error {
211211// isExitParenBalanced checks if the parentheses in an EXIT command argument are balanced.
212212// It tracks quotes to avoid counting parens inside string literals.
213213// It handles SQL Server's quote escaping: ” inside single-quoted strings, "" inside double-quoted strings, and ]] inside bracket identifiers.
214+ // It also ignores parentheses inside SQL comments (-- single-line and /* multi-line */).
214215func isExitParenBalanced (s string ) bool {
215216 depth := 0
216217 var quote rune
218+ inLineComment := false
219+ inBlockComment := false
217220 runes := []rune (s )
218221 for i := 0 ; i < len (runes ); i ++ {
219222 c := runes [i ]
223+
224+ // Handle line comment state
225+ if inLineComment {
226+ // Line comment ends at newline
227+ if c == '\n' {
228+ inLineComment = false
229+ }
230+ continue
231+ }
232+
233+ // Handle block comment state
234+ if inBlockComment {
235+ // Check for end of block comment
236+ if c == '*' && i + 1 < len (runes ) && runes [i + 1 ] == '/' {
237+ inBlockComment = false
238+ i ++ // skip the '/'
239+ }
240+ continue
241+ }
242+
220243 switch {
221244 case quote != 0 :
222245 // Inside a quoted string
@@ -228,6 +251,14 @@ func isExitParenBalanced(s string) bool {
228251 quote = 0
229252 }
230253 }
254+ case c == '-' && i + 1 < len (runes ) && runes [i + 1 ] == '-' :
255+ // Start of single-line comment
256+ inLineComment = true
257+ i ++ // skip the second '-'
258+ case c == '/' && i + 1 < len (runes ) && runes [i + 1 ] == '*' :
259+ // Start of block comment
260+ inBlockComment = true
261+ i ++ // skip the '*'
231262 case c == '\'' || c == '"' :
232263 quote = c
233264 case c == '[' :
@@ -247,6 +278,12 @@ func readExitContinuation(s *Sqlcmd, params string) (string, error) {
247278 var builder strings.Builder
248279 builder .WriteString (params )
249280
281+ // Save original prompt and restore it when done (if batch is initialized)
282+ if s .batch != nil {
283+ originalPrompt := s .Prompt ()
284+ defer s .lineIo .SetPrompt (originalPrompt )
285+ }
286+
250287 for ! isExitParenBalanced (builder .String ()) {
251288 // Show continuation prompt
252289 s .lineIo .SetPrompt (" -> " )
0 commit comments