They are usually recompiled automatically. I probably don’t know if this is guaranteed, but this is what I observed - if you change (for example, add an index) the objects referenced by sproc, then it will recompile.
create table mytable (i int identity) insert mytable default values go 100 create proc sp1 as select * from mytable where i = 17 go exec sp1
If you look at the plan for this execution, it will show the table scan as expected.
create index mytablei on mytable(i) exec sp1
The plan has changed to index search.
EDIT: ok I came up with a query that works - it gives you all the sproc names that have a link to this table in the plan cache. You can associate the sproc name with the sp_recompile syntax to generate a group of sp_recompile statements that you can execute.
;WITH XMLNAMESPACES (default 'http://schemas.microsoft.com/sqlserver/2004/07/showplan') ,TableRefs (SProcName, ReferencedTableName) as ( select object_name(qp.objectid) as SProcName, objNodes.objNode.value('@Database', 'sysname') + '.' + objNodes.objNode.value('@Schema', 'sysname') + '.' + objNodes.objNode.value('@Table', 'sysname') as ReferencedTableName from sys.dm_exec_cached_plans cp outer apply sys.dm_exec_sql_text(cp.plan_handle) st outer apply sys.dm_exec_query_plan(cp.plan_handle) as qp outer apply qp.query_plan.nodes('//Object[@Table]') as objNodes(objNode) where cp.cacheobjtype = 'Compiled Plan' and cp.objtype = 'Proc' ) select * from TableRefs where SProcName is not null and isnull(ReferencedTableName,'') = '[db].[schema].[table]'
source share