Getting Table and Schema Details for Indexes.. DB_ID issue

  • Hi

    Trying to get the below query to work and having a little trouble, Basically if you run it as is, it will return the results of the current DB that you are in,

    I want to be able to change the DB_ID() to the DB ID of another DB and get those results

    However if i change the DB_ID() to say "6" for example i get nothing back? or the result of theDB im still in i.e "master"

    SELECT DISTINCT

    '[' + sm.[Name] + '].[' + OBJECT_NAME(s.[object_id]) + ']'

    FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS s

    INNER JOIN sys.indexes AS i ON s.object_id = i.object_id AND s.index_id = i.index_id

    INNER JOIN sys.objects AS o ON s.[object_id] = o.[object_id]

    INNER JOIN sys.schemas sm ON sm.[schema_id] = o.[schema_id]

    WHERE s.avg_fragmentation_in_percent > 10

    ORDER BY '[' + sm.[Name] + '].[' + OBJECT_NAME(s.[object_id]) + ']'

    Any help will be much appreciated

    Thanks

  • UnicornsRreal (11/18/2011)


    Hi

    Trying to get the below query to work and having a little trouble, Basically if you run it as is, it will return the results of the current DB that you are in,

    I want to be able to change the DB_ID() to the DB ID of another DB and get those results

    However if i change the DB_ID() to say "6" for example i get nothing back? or the result of theDB im still in i.e "master"

    SELECT DISTINCT

    '[' + sm.[Name] + '].[' + OBJECT_NAME(s.[object_id]) + ']'

    FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS s

    INNER JOIN sys.indexes AS i ON s.object_id = i.object_id AND s.index_id = i.index_id

    INNER JOIN sys.objects AS o ON s.[object_id] = o.[object_id]

    INNER JOIN sys.schemas sm ON sm.[schema_id] = o.[schema_id]

    WHERE s.avg_fragmentation_in_percent > 10

    ORDER BY '[' + sm.[Name] + '].[' + OBJECT_NAME(s.[object_id]) + ']'

    Any help will be much appreciated

    Thanks

    DECLARE @sql VARCHAR(MAX)

    SELECT @sql = 'USE ' + QUOTENAME(name) + '

    SELECT DISTINCT QUOTENAME(sm.[Name]) + ''.'' + QUOTENAME(OBJECT_NAME(s.[object_id]))

    FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS s

    INNER JOIN sys.indexes AS i ON s.object_id = i.object_id AND s.index_id = i.index_id

    INNER JOIN sys.objects AS o ON s.[object_id] = o.[object_id]

    INNER JOIN sys.schemas sm ON sm.[schema_id] = o.[schema_id]

    WHERE s.avg_fragmentation_in_percent > 10

    ORDER BY QUOTENAME(sm.[Name]) + ''.'' + QUOTENAME(OBJECT_NAME(s.[object_id]))'

    FROM sys.databases

    WHERE name = 'master'

    EXEC(@SQL)


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Damn that was quick!!!!

    thanks 🙂

  • EXEC ('USE REMOTE_TEST

    SELECT DISTINCT QUOTENAME(sm.[Name]) + ''.'' + QUOTENAME(OBJECT_NAME(s.[object_id]))

    AS [Table] FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS s

    INNER JOIN sys.indexes AS i ON s.object_id = i.object_id AND s.index_id = i.index_id

    INNER JOIN sys.objects AS o ON s.[object_id] = o.[object_id]

    INNER JOIN sys.schemas sm ON sm.[schema_id] = o.[schema_id]WHERE s.avg_fragmentation_in_percent > 10

    ORDER BY QUOTENAME(sm.[Name]) + ''.'' + QUOTENAME(OBJECT_NAME(s.[object_id]))

    FROM sys.databases

    WHERE name = REMOTE_TEST''') AT [LINKED_SERVER]

    IS there away to get this query working using a linked server?

    I tried but keep getting

    Msg 156, Level 15, State 1, Line 8

    Incorrect syntax near the keyword 'FROM'.

  • What's the version of the db on the remote server?

    This is missing a double quote before the db name.

    WHERE name = REMOTE_TEST'''

  • If you're explicitly setting the "USE" value, then you don't need to query sys.databases anyway.

    Try: -

    EXEC ('USE [REMOTE_TEST]

    SELECT DISTINCT QUOTENAME(sm.[Name]) + ''.'' + QUOTENAME(OBJECT_NAME(s.[object_id]))

    AS [Table] FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS s

    INNER JOIN sys.indexes AS i ON s.object_id = i.object_id AND s.index_id = i.index_id

    INNER JOIN sys.objects AS o ON s.[object_id] = o.[object_id]

    INNER JOIN sys.schemas sm ON sm.[schema_id] = o.[schema_id]WHERE s.avg_fragmentation_in_percent > 10

    ORDER BY QUOTENAME(sm.[Name]) + ''.'' + QUOTENAME(OBJECT_NAME(s.[object_id]))') AT [LINKED_SERVER]


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • I keep missing the simple things... sorry and thanks for your help

  • UnicornsRreal (11/18/2011)


    I keep missing the simple things... sorry and thanks for your help

    So long as you show that you've tried, no-one minds helping out 😉


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Haha thanks! 🙂 You are a legend have a good weekend!!!:-D

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply