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.
- 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 CURSORcommand 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.
- 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.
- If you are using PostgreSQL, try wrapping your entire logic (Declaration + Loop + Fetch) inside a single
- 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