FETCH API_CURSOR ....

KZS 151 Reputation points
2021-12-13T07:06:30.453+00:00

I am trying to get more info about a "FETCH API_CURSOR0000000003" that is showing in the activity monitor with much execution and much plan count.
I tried different queries to get the sessionid to know the exact query behind the FETCH but I failed to find any positive information.
I tried: Select * from sys.dm_exec_cursors(0) but it doesn't show any information.
tried different sp_who/sp_who2
I tried the XEvent profiler and tracking each sessionid, and none gave me the specific cursor causing the issue.

157010-cursor.png

SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-12-14T19:56:47.027+00:00

    If they query is not returning any data right now, this is supposedly because there were no active cursors at the time you ran it.

    You could run this query with some frequency to see what you get.

    If you want to trace it, you should filter for sp_cursoropen and sp_cursorfetech. The latter is the one causing the FETCH APICURSOR, but sp_cursoropen will give you information about the query driving the cursor.

    Then again, while the number of plans is staggering, the other numbers do not really look frightening to me. Do you have a real performance problem?

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. YufeiShao-msft 7,146 Reputation points
    2021-12-13T09:18:10.153+00:00

    Hi,

    Have you tried the query in the link?

    it can list cursors for all sessions

        SELECT c.session_id, es.program_name, es.login_name, es.host_name, DB_NAME(es.database_id) AS DatabaseName, c.properties, c.creation_time, c.is_open, t.text  
        FROM sys.dm_exec_cursors (0) c  
        LEFT JOIN sys.dm_exec_sessions AS es ON c.session_id = es.session_id  
        CROSS APPLY sys.dm_exec_sql_text (c.sql_handle) t  
    

    I read somewhere that cursors are hard to trace without running the appropriate queries, and it is recommended to run scripts to trace in advance

    -------------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. YufeiShao-msft 7,146 Reputation points
    2021-12-13T08:30:30.453+00:00

    Hi @KZS ,

    This example will return sessions with open cursors, you can try:

    SELECT er.sql_handle, ec.sql_handle,  
    SUBSTRING(ers.text, (er.statement_start_offset/2)+1,  
    ((CASE er.statement_end_offset  
    WHEN -1 THEN DATALENGTH(ers.text)  
    ELSE er.statement_end_offset  
    END - er.statement_start_offset)/2) + 1) AS statement_text_er,  
    SUBSTRING(ecs.text, (ec.statement_start_offset/2)+1,  
    ((CASE ec.statement_end_offset  
    WHEN -1 THEN DATALENGTH(ecs.text)  
    ELSE ec.statement_end_offset  
    END - ec.statement_start_offset)/2) + 1) AS statement_text_ec  
    FROM sys.dm_exec_requests er cross apply sys.dm_exec_cursors(er.session_id) ec  
    CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) ers  
    CROSS APPLY sys.dm_exec_sql_text(ec.sql_handle) ecs  
    

    https://www.sqlskills.com/blogs/joe/hunting-down-the-origins-of-fetch-api_cursor-and-sp_cursorfetch/
    https://blog.sqlauthority.com/2015/01/10/sql-server-what-is-the-query-used-in-sp_cursorfetch-and-fetch-api_cursor/

    -------------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. KZS 151 Reputation points
    2021-12-13T08:41:00.81+00:00

    Why it doesn't show any information ?

    156989-noinfo.png

    0 comments No comments

  4. KZS 151 Reputation points
    2021-12-13T13:53:42.633+00:00

    the query is not showing any data

    0 comments No comments

Your answer

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