• JC-3113 (9/18/2012)


    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.

    Jim

    code generates sql to find an index in every database

    select

    'select ' + '''[' + name + '].''' + '+ name from [' + name + '].sys.indexes i where i.name = ''AllDocs_PK'''

    from

    master.sys.databases

    where

    name not in

    (

    'master',

    'tempdb',

    'model',

    'msdb'

    );

    go

    Glad you got it working Jim. Usually the quotename 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.