What's the difference between this two commands?
Refreshview is only for views and recompile only for stored procedures? or there are some other differences?
What's the difference between this two commands?
Refreshview is only for views and recompile only for stored procedures? or there are some other differences?
Both of these system procedure can be used on a view or table. However, they do very different things.
sp_refreshview 'MyView'
causes the metadata for a view to be refreshed. This means that any DDL changes (such as ALTER
) to base tables will be reflected in the view. This is particularly important for views which use select *
.
sp_recompile 'MyView'
causes all procedures, triggers and functions which reference the view to be recompiled and get a new query plan. It does not update the metadata for the view: if the view's base tables have changed then the metadata will still be incorrect.
You can see the difference in this fiddle. Notice how the new column was shown only after sp_refreshview
was executed.
You should use sp_refreshview. sp_recompile is only for triggers and stored procedures.