DF058: Cursor is not declared as read-only.

The topic describes the DF058 T-SQL code analysis rule.

Category

EXECUTION RULES

Message

Cursor is not declared as read-only.

Description

It is recommended to declare a cursor with the READ_ONLY property if there are no plans to perform UPDATE or DELETE operations at the cursor position.

Additional information

Cursors declared with READ_ONLY are optimized for read-only operations. This optimization can lead to improved performance compared to cursors without the READ_ONLY property, especially when dealing with large result sets.

Additionally, declaring a cursor as READ_ONLY prevents accidental modifications to the underlying data set. Without the READ_ONLY property, developers may inadvertently attempt to perform UPDATE or DELETE operations, potentially leading to unintended changes or data corruption.

Noncompliant code example

DECLARE cur CURSOR LOCAL FORWARD_ONLY STATIC FOR SELECT
  FirstName
 ,LastName
FROM dbo.Employee

Compliant solution

DECLARE cur CURSOR LOCAL FORWARD_ONLY STATIC READ_ONLY FOR SELECT
  FirstName
 ,LastName
FROM dbo.Employee