﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / SQL Server Newbies  / Quickest Way to Find an Index / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 21:23:05 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>Thanks LynnJim</description><pubDate>Wed, 26 Sep 2012 10:52:58 GMT</pubDate><dc:creator>JC-3113</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>[quote][b]JC-3113 (9/19/2012)[/b][hr]I am on 2005. Will this not work then ?Jim[/quote]Yes, it will work on SQL Server 2005.  You need to check the settings for QUOTED_IDENTIFIER on your server.Just verified after firing up SQL Server 2005 on my laptop.</description><pubDate>Wed, 19 Sep 2012 09:08:53 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>I am on 2005. Will this not work then ?Jim</description><pubDate>Wed, 19 Sep 2012 08:47:34 GMT</pubDate><dc:creator>JC-3113</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>ok got a little fartherno idea what this means:declare @idxname sysname;set @idxname = 'idx_DatePosted';declare @idxname sysname;set @idxname = 'idx_DatePosted';declare @SQLCmd varchar(max);select @SQLCmd = stuff((select 'union all' + char(13) + char(10) + 'select ''' + db.name + ''' as DatabaseName, object_name(idx.object_id) as TableName, idx.name as IndexName from ' + db.name +'.sys.indexes idx where idx.object_id in (select object_id from ' + db.name + '.sys.tables) and idx.name = ''' + @idxname +''' ' + char(13) + char(10)from sys.databases dbwhere db.name not in ('master','tempdb','model','msdb','ReportServer','ReportServerTempDB')for xml path(''),type).value('.','varchar(max)'),1,11,'');print @SQLCmd;exec(@SQLCmd);goMsg 1934, Level 16, State 1, Server NSAB-SS80-SQL-N, Line 6SELECT failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or query notifications and/or xml data type methods.</description><pubDate>Wed, 19 Sep 2012 08:45:44 GMT</pubDate><dc:creator>JC-3113</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>Like this:[code="sql"]declare @idxname sysname;set @idxname = 'idx_DatePosted';[/code]I am finding that I am writing more code using SQL Server 2008 syntax and forget to change it for SQL Server 2005.</description><pubDate>Wed, 19 Sep 2012 08:31:37 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>[quote][b]JC-3113 (9/19/2012)[/b][hr]Hi Lynnreceiving this error executing the code:HResult 0x8B, Level 15, State 1Cannot assign a default value to a local variable.Msg 137, Level 15, State 2, Server NSAB-SS80-SQL-N, Line 5Must declare the scalar variable "@idxname".Jim[/quote]Look at ALL the code I posted.  Also, separate the declaration of the variable and the assigning of a value.  What I have posted works in SQL Server 2008 and up.</description><pubDate>Wed, 19 Sep 2012 08:29:35 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>Hi Lynnreceiving this error executing the code:HResult 0x8B, Level 15, State 1Cannot assign a default value to a local variable.Msg 137, Level 15, State 2, Server NSAB-SS80-SQL-N, Line 5Must declare the scalar variable "@idxname".Jim</description><pubDate>Wed, 19 Sep 2012 08:24:27 GMT</pubDate><dc:creator>JC-3113</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>Something like this should work:[code="sql"]declare @idxname sysname = 'idx_DatePosted';declare @SQLCmd varchar(max);select @SQLCmd = stuff((select 'union all' + char(13) + char(10) + 'select ''' + db.name + ''' as DatabaseName, object_name(idx.object_id) as TableName, idx.name as IndexName from ' + db.name +'.sys.indexes idx where idx.object_id in (select object_id from ' + db.name + '.sys.tables) and idx.name = ''' + @idxname +''' ' + char(13) + char(10)from sys.databases dbwhere db.name not in ('master','tempdb','model','msdb','ReportServer','ReportServerTempDB')for xml path(''),type).value('.','varchar(max)'),1,11,'');print @SQLCmd;exec(@SQLCmd);[/code]</description><pubDate>Tue, 18 Sep 2012 11:47:29 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>Hi roryp 96873 never heard of quotenamewill look into it.Thanksjim</description><pubDate>Tue, 18 Sep 2012 11:30:50 GMT</pubDate><dc:creator>JC-3113</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>[quote][b]JC-3113 (9/18/2012)[/b][hr]Hi roryp 96873 I had to modify the code as it does not like '-' in the database name so i bracketed them. I also concatenated the database name to the index so you know where the index resides as it is in more than one database in my case.Jimcode generates sql to find an index in every databaseselect   'select ' + '''[' + name + '].''' + '+ name from [' + name + '].sys.indexes i where i.name =  ''AllDocs_PK''' from    master.sys.databaseswhere    name not in ('master', 'tempdb', 'model', 'msdb');go[/quote]Glad you got it working Jim.  Usually the [url=http://msdn.microsoft.com/en-us/library/ms176114.aspx]quotename[/url] function is better than manually adding the brackets though.  Your code will most likely be fine, but if there are any names with special characters in them that need escaping, like a square bracket, quotename will account for that.  It's unlikely (and probably bad form if you do) you have databases with square brackets in them, but quotename() will handle them.</description><pubDate>Tue, 18 Sep 2012 11:17:37 GMT</pubDate><dc:creator>roryp 96873</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>Hi roryp 96873 I had to modify the code as it does not like '-' in the database name so i bracketed them. I also concatenated the database name to the index so you know where the index resides as it is in more than one database in my case.Jimcode generates sql to find an index in every databaseselect   'select ' + '''[' + name + '].''' + '+ name from [' + name + '].sys.indexes i where i.name =  ''AllDocs_PK''' from    master.sys.databaseswhere    name not in ('master', 'tempdb', 'model', 'msdb');go</description><pubDate>Tue, 18 Sep 2012 11:07:03 GMT</pubDate><dc:creator>JC-3113</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>Hi Kingston'even though thi seeems to run, it is geberating an errorThanksJim1&amp;gt; IF OBJECT_ID('tempdb..#tmp_Indexes') IS NOT NULL2&amp;gt;      DROP TABLE #tmp_Indexes3&amp;gt;4&amp;gt; DECLARE      @strSQL VARCHAR(2000)5&amp;gt; DECLARE      @IndexName VARCHAR(1000)6&amp;gt;7&amp;gt; CREATE TABLE #tmp_Indexes8&amp;gt; (9&amp;gt;      DatabaseName    VARCHAR(100),10&amp;gt;     IndexName       VARCHAR(1000)11&amp;gt; )12&amp;gt;13&amp;gt; SET @IndexName = 'AllDocs_PK' -- You can enter the name of the index here14&amp;gt;15&amp;gt; SET @strSQL = ' SELECT ''?'', name FROM ?..sysindexes WHERE name LIKE ''%' + @IndexName + '%'' '16&amp;gt;17&amp;gt; INSERT      #tmp_Indexes( DatabaseName, IndexName )18&amp;gt; EXECUTE sp_MSforeachdb @strSQL19&amp;gt;20&amp;gt; SELECT * FROM #tmp_Indexes21&amp;gt;22&amp;gt; IF OBJECT_ID('tempdb..#tmp_Indexes') IS NOT NULL23&amp;gt;     DROP TABLE #tmp_Indexes24&amp;gt; goMsg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.Msg 102, Level 15, State 1, Server NSAB-SS80-SQL-N, Line 1Incorrect syntax near '-'.</description><pubDate>Tue, 18 Sep 2012 11:03:10 GMT</pubDate><dc:creator>JC-3113</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>Thanks KingstonI will take a look seeJim</description><pubDate>Tue, 18 Sep 2012 09:28:37 GMT</pubDate><dc:creator>JC-3113</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>You can use the un-documented SP "sp_MSforeachdb" for this[code="sql"]IF OBJECT_ID('tempdb..#tmp_Indexes') IS NOT NULL	DROP TABLE #tmp_IndexesDECLARE	@strSQL	VARCHAR(2000)DECLARE	@IndexName VARCHAR(1000)CREATE TABLE #tmp_Indexes(	DatabaseName	VARCHAR(100),	IndexName	VARCHAR(1000))SET	@IndexName = 'mst_Employees_IX01' -- You can enter the name of the index hereSET	@strSQL = ' SELECT ''?'', name FROM ?..sysindexes WHERE name LIKE ''%' + @IndexName + '%'' '			INSERT	#tmp_Indexes( DatabaseName, IndexName )EXECUTE sp_MSforeachdb @strSQLSELECT * FROM #tmp_IndexesIF OBJECT_ID('tempdb..#tmp_Indexes') IS NOT NULL	DROP TABLE #tmp_Indexes[/code]Edit: Added a comment in the code</description><pubDate>Tue, 18 Sep 2012 05:36:13 GMT</pubDate><dc:creator>Kingston Dhasian</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>Thanks very much roryp 96873i will give it a shotJim</description><pubDate>Mon, 17 Sep 2012 12:03:16 GMT</pubDate><dc:creator>JC-3113</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>You can generate the SQL to look for the index you have in each of your databases by using something like this:[code="sql"]select 'select *from ' + name + '.sys.indexes iwhere i.name =  ''typeYourIndexNameHere'''from master.sys.databaseswhere name not in ('master', 'tempdb', 'model', 'msdb')[/code]Might save you a little bit of time.</description><pubDate>Mon, 17 Sep 2012 11:31:59 GMT</pubDate><dc:creator>roryp 96873</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>[quote][b]JC-3113 (9/17/2012)[/b][hr]Hi Lynnyou mean i have to look in each database seperately ?no view that encompases al the databases ?Jim[/quote]Basically, yes.  There are ways around it but I don't have time to enumerate them at the moment.</description><pubDate>Mon, 17 Sep 2012 11:21:42 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>Hi Lynnyou mean i have to look in each database seperately ?no view that encompases al the databases ?Jim</description><pubDate>Mon, 17 Sep 2012 11:11:43 GMT</pubDate><dc:creator>JC-3113</dc:creator></item><item><title>RE: Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>The system view sys.indexes in each database.</description><pubDate>Mon, 17 Sep 2012 11:07:47 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>Quickest Way to Find an Index</title><link>http://www.sqlservercentral.com/Forums/Topic1360329-1291-1.aspx</link><description>Hi Folkscan comeone tell me the quickest way to find an index in some 100 plus databases ?is there a view or system table that houses that info that I can search through ?All I have is the index nameThanksJim</description><pubDate>Mon, 17 Sep 2012 10:42:55 GMT</pubDate><dc:creator>JC-3113</dc:creator></item></channel></rss>