Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2472,10 +2472,11 @@ impl fmt::Display for DeclareAssignment {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum DeclareType {
/// Cursor variable type. e.g. [Snowflake] [PostgreSQL]
/// Cursor variable type. e.g. [Snowflake] [PostgreSQL] [MsSql]
///
/// [Snowflake]: https://docs.snowflake.com/en/developer-guide/snowflake-scripting/cursors#declaring-a-cursor
/// [PostgreSQL]: https://www.postgresql.org/docs/current/plpgsql-cursors.html
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-cursor-transact-sql
Cursor,

/// Result set variable type. [Snowflake]
Expand Down
22 changes: 17 additions & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6446,7 +6446,7 @@ impl<'a> Parser<'a> {
/// DECLARE
// {
// { @local_variable [AS] data_type [ = value ] }
// | { @cursor_variable_name CURSOR }
// | { @cursor_variable_name CURSOR [ FOR ] }
// } [ ,...n ]
/// ```
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
Expand All @@ -6462,14 +6462,19 @@ impl<'a> Parser<'a> {
/// ```text
// {
// { @local_variable [AS] data_type [ = value ] }
// | { @cursor_variable_name CURSOR }
// | { @cursor_variable_name CURSOR [ FOR ]}
// } [ ,...n ]
/// ```
/// [MsSql]: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16
pub fn parse_mssql_declare_stmt(&mut self) -> Result<Declare, ParserError> {
let name = {
let ident = self.parse_identifier()?;
if !ident.value.starts_with('@') {
if !ident.value.starts_with('@')
&& !matches!(
self.peek_token().token,
Token::Word(w) if w.keyword == Keyword::CURSOR
)
{
Err(ParserError::TokenizerError(
"Invalid MsSql variable declaration.".to_string(),
))
Expand All @@ -6493,7 +6498,14 @@ impl<'a> Parser<'a> {
_ => (None, Some(self.parse_data_type()?)),
};

let assignment = self.parse_mssql_variable_declaration_expression()?;
let (for_query, assignment) = if self.peek_keyword(Keyword::FOR) {
self.next_token();
let query = Some(self.parse_query()?);
(query, None)
} else {
let assignment = self.parse_mssql_variable_declaration_expression()?;
(None, assignment)
};

Ok(Declare {
names: vec![name],
Expand All @@ -6504,7 +6516,7 @@ impl<'a> Parser<'a> {
sensitive: None,
scroll: None,
hold: None,
for_query: None,
for_query,
})
}

Expand Down
4 changes: 4 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,10 @@ fn parse_mssql_declare() {
],
ast
);

let declare_cursor_for_select =
"DECLARE vend_cursor CURSOR FOR SELECT * FROM Purchasing.Vendor";
let _ = ms().verified_stmt(declare_cursor_for_select);
}

#[test]
Expand Down