How do I add an index to each table using sp_MSforeachtable? It keeps errors running.
EXEC sp_MSforeachtable @precommand = 'Announce @idx four;', @ command1 = 'set @idx =' 'idx_' + +? + '' _Meded_on ''; Print @ edex; If present (choose from sys.indexes * where object_id = object_id (n '' [dbo]] [?] '') And name = n '' @ idx '') index drop-off index [@idx] current [dbo]. [?] Create a nonclustered index [@idx] on [dbo] [] ([modified_on] ASC) [primary] ' One such error:?
Message 137, Level 15, State 1, line 2 scalar variable must be declared "@edex" I try to declare inside the command But then get this error:
Message 4104, level 16, state1, line 3 multi-identity identifier "dbo.DIAG_contractAuditHistory" could not be constrained. Here is an updated attempt to fix syntax errors, as well as test SQL also, but it still gave strange errors. I resolved the problem with a stored procedure and called the stored procedure for each table. EXEC sp_MSforeachtable @ command1 = 'vidar as vidar (256) declared; Set @idx = '' idx_ '' + SUBSTRING (''? '', 8, lane ('' '') - 8) + '' _modified_on ''; Print @ edex; If available (select * FROM sys.indexes WHERE object_id = OBJECT_ID (N ''? '') And name = @idx) Drop index [@idx] ON? If present, create nonclustered index [@idx] current (select sys.columns where object_id = object_id (n ''? '') And name = '' modified_on '')? ([Modified] on [primary] 'declare @cat as four; Set @ cat = 'dog'; Print @cat; EXEC sp_MSforeachtable 'print' 'idx_' '+ SUBSTRING (' '?' ', 8, Lane (' '' ') - 8) +' '_Meded_on' '; 'EXEC sp_MSforeachtable' print '' '' 'Print substrings' ('[MerchantNotes] [dbo]', 8, (lane ('[dbo] [merchantNotes]') - 9))? Select from * sys.columns where object_id = object_id (n '[dbo]. [Banks]') and name = 'modified_on' sys.indexes * Where object_id = object_id (N'contractPaymentHistory ') and name =' idx_contractPaymentHistory_modified_on ' This will work for about two dozen tables, then it gives strange errors such as
message 1934, level 16, state 1, line 9 Build Index failed because the following SET options are incorrect settings: 'QUOTED_IDENTIFIER' Verify that the SET option is calculated using the calculated column and / or filtered index Correct and / or query notifications and / or XML data type methods and / or spatial index operations are indexed for use with indexed views and / or indexed.
So I ended up using a different strategy and created a stored procedure to create an index:
I have never tried to use @precommand before, but if you add that part to @ command1 If so, then it should work. How to do the other problem you ? , this is actually actually changing the DO schema part object_id (n '' [dbo]] [?] '') object_id ("?") and the same goes for with and drop index edit: This code should work.
EXEC sp_MSforeachtable 'char; Set @idx = '' idx_ '' + "?" + '' _Meded_on ''; If present (select * FROM sys.indexes WHERE object_id = OBJECT_ID ("?") And name = N '' @ idx '') Drop index [@idx] ON? Make non-subsidized INDEX [@idx] active? [Primary] ' ([modified_on] ASC)
No comments:
Post a Comment