Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/dialect/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,22 @@ impl Dialect for MsSqlDialect {
}

fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
if parser.peek_keyword(Keyword::IF) {
if parser.parse_keyword(Keyword::BEGIN) {
if parser.peek_keyword(Keyword::TRANSACTION)
|| parser.peek_keyword(Keyword::WORK)
|| parser.peek_keyword(Keyword::TRY)
|| parser.peek_keyword(Keyword::CATCH)
|| parser.peek_keyword(Keyword::DEFERRED)
|| parser.peek_keyword(Keyword::IMMEDIATE)
|| parser.peek_keyword(Keyword::EXCLUSIVE)
|| parser.peek_token_ref().token == Token::SemiColon
|| parser.peek_token_ref().token == Token::EOF
{
parser.prev_token();
return None;
}
Some(parser.parse_begin_exception_end())
Comment on lines +148 to +162
Copy link
Contributor

Choose a reason for hiding this comment

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

would a condition like this do what we want?

if parse.peek_keywords(BEGIN, TRANSACTION) {
    None
} else if parse_keyword(BEGIN) {
    Some(parser.parse_begin_exception_end())
}

its not super clear to me why the current logic looks at WORK, TRY etc keywords?

Copy link
Member Author

@guan404ming guan404ming Jan 31, 2026

Choose a reason for hiding this comment

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

Thanks for the review. These keywords are checked because parse_begin() handles more than just BEGIN TRANSACTION. It also handles WORK, TRY, CATCH, DEFERRED, IMMEDIATE, EXCLUSIVE (since MsSql's supports_start_transaction_modifier() returns true), as well as bare BEGIN;. If any of these are missed, they'd be incorrectly routed to parse_begin_exception_end() and fail to parse.

For example, BEGIN TRY ... END TRY is valid MSSQL syntax. If we only check for BEGIN TRANSACTION, then BEGIN TRY would fall through to parse_begin_exception_end(), which would try to parse TRY as a SQL statement and fail. because TRY is supposed to be handled as a

} else if self.parse_keyword(Keyword::TRY) {
Some(TransactionModifier::Try)

Copy link
Member Author

Choose a reason for hiding this comment

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

Feel free to let me know if there are any not clear, thanks!

} else if parser.peek_keyword(Keyword::IF) {
Some(self.parse_if_stmt(parser))
} else if parser.parse_keywords(&[Keyword::CREATE, Keyword::TRIGGER]) {
Some(self.parse_create_trigger(parser, false))
Expand Down
71 changes: 71 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2554,3 +2554,74 @@ fn test_sql_keywords_as_column_aliases() {
}
}
}

#[test]
fn parse_mssql_begin_end_block() {
// Single statement
let sql = "BEGIN SELECT 1; END";
let stmt = ms().verified_stmt(sql);
match &stmt {
Statement::StartTransaction {
begin,
has_end_keyword,
statements,
transaction,
modifier,
..
} => {
assert!(begin);
assert!(has_end_keyword);
assert!(transaction.is_none());
assert!(modifier.is_none());
assert_eq!(statements.len(), 1);
}
_ => panic!("Expected StartTransaction, got: {stmt:?}"),
}

// Multiple statements
let sql = "BEGIN SELECT 1; SELECT 2; END";
let stmt = ms().verified_stmt(sql);
match &stmt {
Statement::StartTransaction {
statements,
has_end_keyword,
..
} => {
assert!(has_end_keyword);
assert_eq!(statements.len(), 2);
}
_ => panic!("Expected StartTransaction, got: {stmt:?}"),
}

// DML inside BEGIN/END
let sql = "BEGIN INSERT INTO t VALUES (1); UPDATE t SET x = 2; END";
let stmt = ms().verified_stmt(sql);
match &stmt {
Statement::StartTransaction {
statements,
has_end_keyword,
..
} => {
assert!(has_end_keyword);
assert_eq!(statements.len(), 2);
}
_ => panic!("Expected StartTransaction, got: {stmt:?}"),
}

// BEGIN TRANSACTION still works
let sql = "BEGIN TRANSACTION";
let stmt = ms().verified_stmt(sql);
match &stmt {
Statement::StartTransaction {
begin,
has_end_keyword,
transaction,
..
} => {
assert!(begin);
assert!(!has_end_keyword);
assert!(transaction.is_some());
}
_ => panic!("Expected StartTransaction, got: {stmt:?}"),
}
}