@@ -6489,7 +6489,7 @@ impl<'a> Parser<'a> {
6489
6489
/// DECLARE
6490
6490
// {
6491
6491
// { @local_variable [AS] data_type [ = value ] }
6492
- // | { @cursor_variable_name CURSOR }
6492
+ // | { @cursor_variable_name CURSOR [ FOR ] }
6493
6493
// } [ ,...n ]
6494
6494
/// ```
6495
6495
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
@@ -6505,14 +6505,19 @@ impl<'a> Parser<'a> {
6505
6505
/// ```text
6506
6506
// {
6507
6507
// { @local_variable [AS] data_type [ = value ] }
6508
- // | { @cursor_variable_name CURSOR }
6508
+ // | { @cursor_variable_name CURSOR [ FOR ] }
6509
6509
// } [ ,...n ]
6510
6510
/// ```
6511
6511
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
6512
6512
pub fn parse_mssql_declare_stmt(&mut self) -> Result<Declare, ParserError> {
6513
6513
let name = {
6514
6514
let ident = self.parse_identifier()?;
6515
- if !ident.value.starts_with('@') {
6515
+ if !ident.value.starts_with('@')
6516
+ && !matches!(
6517
+ self.peek_token().token,
6518
+ Token::Word(w) if w.keyword == Keyword::CURSOR
6519
+ )
6520
+ {
6516
6521
Err(ParserError::TokenizerError(
6517
6522
"Invalid MsSql variable declaration.".to_string(),
6518
6523
))
@@ -6536,7 +6541,14 @@ impl<'a> Parser<'a> {
6536
6541
_ => (None, Some(self.parse_data_type()?)),
6537
6542
};
6538
6543
6539
- let assignment = self.parse_mssql_variable_declaration_expression()?;
6544
+ let (for_query, assignment) = if self.peek_keyword(Keyword::FOR) {
6545
+ self.next_token();
6546
+ let query = Some(self.parse_query()?);
6547
+ (query, None)
6548
+ } else {
6549
+ let assignment = self.parse_mssql_variable_declaration_expression()?;
6550
+ (None, assignment)
6551
+ };
6540
6552
6541
6553
Ok(Declare {
6542
6554
names: vec![name],
@@ -6547,7 +6559,7 @@ impl<'a> Parser<'a> {
6547
6559
sensitive: None,
6548
6560
scroll: None,
6549
6561
hold: None,
6550
- for_query: None ,
6562
+ for_query,
6551
6563
})
6552
6564
}
6553
6565
0 commit comments