Using WITH NOLOCK Table Hint in Query Using View - Does it Propagate Within the View?

前端 未结 2 1460
醉梦人生
醉梦人生 2021-01-01 08:06

If a \"WITH NOLOCK\" query hint is used on a View in SQL Server, does it propagate that hint to the view definition itself, even if NOLOCK is NOT used for the raw tables in

相关标签:
2条回答
  • 2021-01-01 08:52

    Just to supplement Rory's excellent answer.

    He writes "Yes, NOLOCK will propagate to the tables used by the view definition (at least in SQL Server 2005)."

    In fact this will work in SQL 2000 as well. From BOL:

    Because select_statement uses the SELECT statement, it is valid to use and hints as specified in the FROM clause. For more information, see FROM and SELECT.

    0 讨论(0)
  • 2021-01-01 08:58

    Yes, NOLOCK will propagate to the tables used by the view definition (at least in SQL Server 2005).

    See Table Hints in MSDN:

    In SQL Server 2005, all lock hints are propagated to all the tables and views that are referenced in a view. Also, SQL Server performs the corresponding lock consistency checks.

    However,

    If a table contains computed columns and the computed columns are computed by expressions or functions accessing columns in other tables, the table hints are not used on those tables. This means the table hints are not propagated. For example, a NOLOCK table hint is specified on a table in the query. This table has computed columns that are computed by a combination of expressions and functions that access columns in another table. The tables referenced by the expressions and functions do not use the NOLOCK table hint when accessed.

    If you're using indexed views you might want to read a bit more as there are some special cases there too.

    Also see View Resolution for more info.

    0 讨论(0)
提交回复
热议问题