44 {%- set full_relation = ' "' ~ relation .schema ~ ' "."' ~ relation .identifier ~ ' "' - %}
55 use [{{ relation.database }}];
66 if EXISTS (
7- SELECT * FROM
8- sys .indexes WHERE name = ' {{cci_name}}'
7+ SELECT *
8+ FROM sys .indexes with (nolock )
9+ WHERE name = ' {{cci_name}}'
910 AND object_id = object_id (' {{relation_name}}' )
1011 )
1112 DROP index {{full_relation}}.{{cci_name}}
3233declare @drop_xml_indexes nvarchar(max);
3334select @drop_xml_indexes = (
3435 select ' IF INDEXPROPERTY (' + CONVERT(VARCHAR(MAX), sys.tables.[object_id]) + ' , ' '' + sys.indexes.[name] + '' ' , ' ' IndexId' ' ) IS NOT NULL DROP INDEX [' + sys.indexes.[name] + ' ] ON ' + ' [' + SCHEMA_NAME (sys .tables .[schema_id]) + ' ].[' + OBJECT_NAME (sys .tables .[object_id]) + ' ]; '
35- from sys .indexes
36- inner join sys .tables on sys .indexes .object_id = sys .tables .object_id
36+ from sys .indexes with (nolock )
37+ inner join sys .tables with (nolock )
38+ on sys .indexes .object_id = sys .tables .object_id
3739 where sys .indexes .[name] is not null
3840 and sys .indexes .type_desc = ' XML'
3941 and sys .tables .[name] = ' {{ this.table }}'
@@ -52,8 +54,9 @@ select @drop_xml_indexes = (
5254declare @drop_spatial_indexes nvarchar (max );
5355select @drop_spatial_indexes = (
5456 select ' IF INDEXPROPERTY(' + CONVERT (VARCHAR (MAX ), sys .tables .[object_id]) + ' , '' ' + sys .indexes .[name] + ' '' , '' IndexId'' ) IS NOT NULL DROP INDEX [' + sys .indexes .[name] + ' ] ON ' + ' [' + SCHEMA_NAME (sys .tables .[schema_id]) + ' ].[' + OBJECT_NAME (sys .tables .[object_id]) + ' ]; '
55- from sys .indexes
56- inner join sys .tables on sys .indexes .object_id = sys .tables .object_id
57+ from sys .indexes with (nolock )
58+ inner join sys .tables with (nolock )
59+ on sys .indexes .object_id = sys .tables .object_id
5760 where sys .indexes .[name] is not null
5861 and sys .indexes .type_desc = ' Spatial'
5962 and sys .tables .[name] = ' {{ this.table }}'
@@ -116,8 +119,9 @@ select @drop_pk_constraints = (
116119declare @drop_remaining_indexes_last nvarchar (max );
117120select @drop_remaining_indexes_last = (
118121 select ' IF INDEXPROPERTY(' + CONVERT (VARCHAR (MAX ), sys .tables .[object_id]) + ' , '' ' + sys .indexes .[name] + ' '' , '' IndexId'' ) IS NOT NULL DROP INDEX [' + sys .indexes .[name] + ' ] ON ' + ' [' + SCHEMA_NAME (sys .tables .[schema_id]) + ' ].[' + OBJECT_NAME (sys .tables .[object_id]) + ' ]; '
119- from sys .indexes
120- inner join sys .tables on sys .indexes .object_id = sys .tables .object_id
122+ from sys .indexes with (nolock )
123+ inner join sys .tables with (nolock )
124+ on sys .indexes .object_id = sys .tables .object_id
121125 where sys .indexes .[name] is not null
122126 and sys .tables .[name] = ' {{ this.table }}'
123127 for xml path (' ' )
@@ -132,10 +136,10 @@ select @drop_remaining_indexes_last = (
132136
133137{% set idx_name = this .table + ' __clustered_index_on_' + columns|join (' _' ) %}
134138
135- if not exists (select * from sys . indexes
136- where
137- name = ' {{ idx_name }}' and
138- object_id = OBJECT_ID (' {{ this }}' )
139+ if not exists (select *
140+ from sys . indexes with ( nolock )
141+ where name = ' {{ idx_name }}'
142+ and object_id = OBJECT_ID (' {{ this }}' )
139143)
140144begin
141145
@@ -160,10 +164,10 @@ end
160164 {% set idx_name = this .table + ' __index_on_' + columns|join (' _' )|replace (" " , " _" ) %}
161165{% endif %}
162166
163- if not exists (select * from sys . indexes
164- where
165- name = ' {{ idx_name }}' and
166- object_id = OBJECT_ID (' {{ this }}' )
167+ if not exists (select *
168+ from sys . indexes with ( nolock )
169+ where name = ' {{ idx_name }}'
170+ and object_id = OBJECT_ID (' {{ this }}' )
167171)
168172begin
169173create nonclustered index
0 commit comments