@@ -6446,7 +6446,7 @@ impl<'a> Parser<'a> {
6446
6446
/// DECLARE
6447
6447
// {
6448
6448
// { @local_variable [AS] data_type [ = value ] }
6449
- // | { @cursor_variable_name CURSOR }
6449
+ // | { @cursor_variable_name CURSOR [ FOR ] }
6450
6450
// } [ ,...n ]
6451
6451
/// ```
6452
6452
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
@@ -6462,14 +6462,19 @@ impl<'a> Parser<'a> {
6462
6462
/// ```text
6463
6463
// {
6464
6464
// { @local_variable [AS] data_type [ = value ] }
6465
- // | { @cursor_variable_name CURSOR }
6465
+ // | { @cursor_variable_name CURSOR [ FOR ] }
6466
6466
// } [ ,...n ]
6467
6467
/// ```
6468
6468
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
6469
6469
pub fn parse_mssql_declare_stmt(&mut self) -> Result<Declare, ParserError> {
6470
6470
let name = {
6471
6471
let ident = self.parse_identifier()?;
6472
- if !ident.value.starts_with('@') {
6472
+ if !ident.value.starts_with('@')
6473
+ && !matches!(
6474
+ self.peek_token().token,
6475
+ Token::Word(w) if w.keyword == Keyword::CURSOR
6476
+ )
6477
+ {
6473
6478
Err(ParserError::TokenizerError(
6474
6479
"Invalid MsSql variable declaration.".to_string(),
6475
6480
))
@@ -6493,7 +6498,14 @@ impl<'a> Parser<'a> {
6493
6498
_ => (None, Some(self.parse_data_type()?)),
6494
6499
};
6495
6500
6496
- let assignment = self.parse_mssql_variable_declaration_expression()?;
6501
+ let (for_query, assignment) = if self.peek_keyword(Keyword::FOR) {
6502
+ self.next_token();
6503
+ let query = Some(self.parse_query()?);
6504
+ (query, None)
6505
+ } else {
6506
+ let assignment = self.parse_mssql_variable_declaration_expression()?;
6507
+ (None, assignment)
6508
+ };
6497
6509
6498
6510
Ok(Declare {
6499
6511
names: vec![name],
@@ -6504,7 +6516,7 @@ impl<'a> Parser<'a> {
6504
6516
sensitive: None,
6505
6517
scroll: None,
6506
6518
hold: None,
6507
- for_query: None ,
6519
+ for_query,
6508
6520
})
6509
6521
}
6510
6522
0 commit comments