@@ -6446,7 +6446,7 @@ impl<'a> Parser<'a> {
64466446 /// DECLARE
64476447 // {
64486448 // { @local_variable [AS] data_type [ = value ] }
6449- // | { @cursor_variable_name CURSOR }
6449+ // | { @cursor_variable_name CURSOR [ FOR ] }
64506450 // } [ ,...n ]
64516451 /// ```
64526452 /// [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> {
64626462 /// ```text
64636463 // {
64646464 // { @local_variable [AS] data_type [ = value ] }
6465- // | { @cursor_variable_name CURSOR }
6465+ // | { @cursor_variable_name CURSOR [ FOR ] }
64666466 // } [ ,...n ]
64676467 /// ```
64686468 /// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
64696469 pub fn parse_mssql_declare_stmt(&mut self) -> Result<Declare, ParserError> {
64706470 let name = {
64716471 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+ {
64736478 Err(ParserError::TokenizerError(
64746479 "Invalid MsSql variable declaration.".to_string(),
64756480 ))
@@ -6493,7 +6498,14 @@ impl<'a> Parser<'a> {
64936498 _ => (None, Some(self.parse_data_type()?)),
64946499 };
64956500
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+ };
64976509
64986510 Ok(Declare {
64996511 names: vec![name],
@@ -6504,7 +6516,7 @@ impl<'a> Parser<'a> {
65046516 sensitive: None,
65056517 scroll: None,
65066518 hold: None,
6507- for_query: None ,
6519+ for_query,
65086520 })
65096521 }
65106522
0 commit comments