Repository: tobymao/sqlglot

Issue:
Regression in Parsing CASE Statement in T-SQL
```t-sql
import sqlglot as sg 

sg.parse("""
SELECT *FROM tester r11
LEFT OUTER JOIN  [CORE].[dim_test] r24 on CASE WHEN r11.id='03.01' THEN '11' ELSE r11.[id] END = r24.ProductGroup_IdOrig
					LEFT OUTER JOIN  TRANSF.tester2  srms ON
						srms.c1=s.c1 and 
						srms.c2=s.c2 and 
						srms.c3=s.c3 and 
						srms.c4=s.c4
""", "tsql")
```
This worked until 10.5.6

the error message:
```
Traceback (most recent call last):
  File "c:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\minimalrepo.py", line 3, in <module>
    sg.parse("""
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\__init__.py", line 57, in parse
    return dialect.parse(sql, **opts)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\dialects\dialect.py", line 151, in parse
    return self.parser(**opts).parse(self.tokenizer.tokenize(sql), sql)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 675, in parse
    return self._parse(
           ^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 737, in _parse
    expressions.append(parse_method(self))
                       ^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 885, in _parse_statement       
    expression = self._parse_set_operations(expression) if expression else self._parse_select()
                                                                           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 1325, in _parse_select
    self._parse_query_modifiers(this)
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 1415, in _parse_query_modifiers    join = self._parse_join()
           ^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 1533, in _parse_join
    kwargs["on"] = self._parse_conjunction()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 1906, in _parse_conjunction    
    return self._parse_tokens(self._parse_equality, self.CONJUNCTION)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 2978, in _parse_tokens
    this = parse_method()
           ^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 1909, in _parse_equality       
    return self._parse_tokens(self._parse_comparison, self.EQUALITY)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 2978, in _parse_tokens
    this = parse_method()
           ^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 1912, in _parse_comparison     
    return self._parse_tokens(self._parse_range, self.COMPARISON)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 2978, in _parse_tokens
    this = parse_method()
           ^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 1915, in _parse_range
    this = self._parse_bitwise()
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 1980, in _parse_bitwise        
    this = self._parse_term()
           ^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 2003, in _parse_term
    return self._parse_tokens(self._parse_factor, self.TERM)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 2978, in _parse_tokens
    this = parse_method()
           ^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 2006, in _parse_factor
    return self._parse_tokens(self._parse_unary, self.FACTOR)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 2978, in _parse_tokens
    this = parse_method()
           ^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 2011, in _parse_unary
    return self._parse_at_time_zone(self._parse_type())
                                    ^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 2019, in _parse_type
    this = self._parse_column()
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 2146, in _parse_column
    this = self._parse_field()
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 2231, in _parse_field
    return self._parse_primary() or self._parse_function() or self._parse_id_var(any_token)
                                    ^^^^^^^^^^^^^^^^^^^^^^
    return self.NO_PAREN_FUNCTION_PARSERS[token_type](self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 557, in <lambda>
    TokenType.CASE: lambda self: self._parse_case(),
                                 ^^^^^^^^^^^^^^^^^^
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 2593, in _parse_case
    self.raise_error("Expected END after CASE", self._prev)
  File "C:\Projects\SQLServer\SQLTransformer\bmsdna.sqltransform\.venv\Lib\site-packages\sqlglot\parser.py", line 783, in raise_error
    raise error
sqlglot.errors.ParseError: Expected END after CASE. Line 3, Col: 89.
   tester r11
LEFT OUTER JOIN  [CORE].[dim_test] r24 on CASE WHEN r11.id='03.01' THEN '11' ELSE r11.[id] END = r24.ProductGroup_IdOrig
                                        LEFT OUTER JOIN  TRANSF.tester2  srms ON
                                                srms.c1=s.c1 and
```



Discussion:
I'll take a look, it's possibly something I messed up while I was working on the other T-SQL issues. Thanks for reporting.

Resolving patch:
diff --git a/sqlglot/dialects/tsql.py b/sqlglot/dialects/tsql.py
--- a/sqlglot/dialects/tsql.py
+++ b/sqlglot/dialects/tsql.py
@@ -247,7 +247,6 @@ class Tokenizer(tokens.Tokenizer):
             "DATETIME2": TokenType.DATETIME,
             "DATETIMEOFFSET": TokenType.TIMESTAMPTZ,
             "DECLARE": TokenType.COMMAND,
-            "END": TokenType.COMMAND,
             "IMAGE": TokenType.IMAGE,
             "MONEY": TokenType.MONEY,
             "NTEXT": TokenType.TEXT,
@@ -306,6 +305,11 @@ class Parser(parser.Parser):
             *parser.Parser.TYPE_TOKENS,  # type: ignore
         }
 
+        STATEMENT_PARSERS = {
+            **parser.Parser.STATEMENT_PARSERS,  # type: ignore
+            TokenType.END: lambda self: self._parse_command(),
+        }
+
         def _parse_system_time(self) -> t.Optional[exp.Expression]:
             if not self._match_text_seq("FOR", "SYSTEM_TIME"):
                 return None
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -861,6 +861,9 @@ def _advance(self, times: int = 1) -> None:
     def _retreat(self, index: int) -> None:
         self._advance(index - self._index)
 
+    def _parse_command(self) -> exp.Expression:
+        return self.expression(exp.Command, this=self._prev.text, expression=self._parse_string())
+
     def _parse_statement(self) -> t.Optional[exp.Expression]:
         if self._curr is None:
             return None
@@ -869,9 +872,7 @@ def _parse_statement(self) -> t.Optional[exp.Expression]:
             return self.STATEMENT_PARSERS[self._prev.token_type](self)
 
         if self._match_set(Tokenizer.COMMANDS):
-            return self.expression(
-                exp.Command, this=self._prev.text, expression=self._parse_string()
-            )
+            return self._parse_command()
 
         expression = self._parse_expression()
         expression = self._parse_set_operations(expression) if expression else self._parse_select()


Test patch:
diff --git a/tests/dialects/test_tsql.py b/tests/dialects/test_tsql.py
--- a/tests/dialects/test_tsql.py
+++ b/tests/dialects/test_tsql.py
@@ -6,6 +6,7 @@ class TestTSQL(Validator):
     dialect = "tsql"
 
     def test_tsql(self):
+        self.validate_identity("SELECT CASE WHEN a > 1 THEN b END")
         self.validate_identity("END")
         self.validate_identity("@x")
         self.validate_identity("#x")