Cannot execute SQL cursor in Azure Query Editor (Preview)

Omkar Pawar 260 Reputation points
2025-11-27T13:10:16.54+00:00

Hi, I am trying to run a simple cursor in Azure Query Editor (Preview).

In Azure Query Editor, it does not run and shows no output. But the same code works in SSMS 21.

Is this a limit in Query Editor? Do I need special permission?

Thanks.

Azure SQL Database
{count} votes

2 answers

Sort by: Most helpful
  1. Jerald Felix 9,835 Reputation points
    2025-11-28T08:25:11.3366667+00:00

    Hello Omkar Pawar,

    Thanks for raising this question in Q&A forum.

    I understand that you are trying to execute a SQL Cursor within the Azure Query Editor (likely for PostgreSQL or Azure SQL) and it is failing or returning an error indicating the cursor does not exist.

    This is a known limitation of the web-based Query Editor due to how it handles Transaction Scopes.

    1. The Root Cause:
      • Cursors rely on an active transaction to remain open.
      • The Azure Query Editor often runs in "Auto-Commit" mode. This means that as soon as your DECLARE CURSOR command finishes, the editor commits the transaction and closes the session.
      • When you try to run the next command (e.g., FETCH NEXT), the cursor no longer exists because the previous transaction was closed.
    2. Workaround (Anonymous Block):
      • If you are using PostgreSQL, try wrapping your entire logic (Declaration + Loop + Fetch) inside a single DO $$...$$ block. This forces the entire sequence to run as a single unit of work.
      • If you are using SQL Server, wrap the logic in BEGIN ... END.
    3. Recommended Solution:
      • The Query Editor is designed for quick, simple queries. For complex procedural operations like Cursors, it is highly recommended to use a desktop tool like Azure Data Studio, pgAdmin, or DBeaver. These tools maintain a persistent session, allowing you to step through cursor operations correctly.

    If helps, approve the answer.

    Best Regards,

    Jerald Felix


  2. Saraswathi Devadula 13,330 Reputation points Microsoft External Staff Moderator
    2025-12-04T03:45:41.7733333+00:00

    Hello Omkar Pawar

    It seems like you are having trouble executing a SQL cursor in the Azure Query Editor (Preview) while it works fine in SSMS. This could indeed be a limitation in the Query Editor, as it is primarily designed for lightweight querying and may not fully support advanced T-SQL features like cursors.

    Kindly please verify the below,

    1. Ensure that your user account has the necessary permissions to execute cursors. This might be different in the Azure Query Editor compared to SSMS.
    2. If you need to run complex T-SQL code that involves cursors, SSMS or another dedicated SQL client might still be the best option since they provide a more complete SQL environment.

    3.Confirm whether there are any known limitations or issues with cursors in the Azure Query Editor (Preview)
    https://learn.microsoft.com/en-us/azure/azure-sql/database/connect-query-portal?view=azuresql
    https://learn.microsoft.com/en-us/azure/azure-sql/database/query-editor?view=azuresql#considerations-and-limitations

    1. Make sure that your connection settings are correct, as issues with network settings, firewalls, or proxy configurations can occasionally cause queries to fail silently.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.