LAST_QUERY_PLAN_STATS works under the lightweight query execution statistics profiling infrastructure, so we need to ensure both Last_Query_Plan_Stats Lightweight_Query_Profiling is on at database scope (though Lightweight_Query_Profiling is default on at SQL2019 but it can be turned off as well). Reference: https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management- views/sys-dm-exec-query-plan-stats-transact-sql?view=sql-server-ver15