Search code examples
c#sql-serversql-execution-plan

Programatically read SQL Server's trigger query plan


I need to read all query plans related to a SQL command from C#. However, trigger's query plans are not returned in the reader. I'm using a simple code like this:

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "SET SHOWPLAN_XML ON";
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();

            // next method
            SqlCommand cmd = new SqlCommand(commandText, conn);
            cmd.CommandType = CommandType.Text;
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                AnalyzeQueryPlan(reader[0].ToString());
            }
            reader.Close();

But I'm missing the trigger's query plans. If I process the same query in a SSMS I can see them all. Is there a way how to read trigger's query plans in C#?


Solution

  • I'm not in a position to test this but imagine that you just need to use reader.NextResult() to advance to the next result and then process that.