Stored Procedure question

  • Hello I have the stored procedure that I need to run on several databases. It is stored in the master database.

    The issue is when the following queries are run it only returns tables from the master database. Is there a way to do something like a "USE" database in the stored proc? I know you can not actually use the the "USE" command. I basically need to be able to run this sp on any given database and have the below queries return tables and indexes from that database, not master.

    SELECT TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME

    FROM INFORMATION_SCHEMA.TABLES

    WHERE TABLE_TYPE = 'BASE TABLE'

    SELECT table_catalog + '.' + table_schema + '.' + ObjectName, ObjectId, IndexName, IndexId, LogicalFrag

    FROM #fraglist, INFORMATION_SCHEMA.TABLES

    WHERE #fraglist.ObjectName = INFORMATION_SCHEMA.TABLES.TABLE_NAME

    AND LogicalFrag >= @maxfrag

    AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0

    CREATE PROCEDURE [dbo].[sp_RebuildIndxs]

    AS

    SET NOCOUNT ON

    DECLARE @tablename VARCHAR (128)

    DECLARE @execstr VARCHAR (255)

    DECLARE @objectid INT

    DECLARE @indexid INT

    DECLARE @indexname VARCHAR (255)

    DECLARE @frag DECIMAL

    DECLARE @maxfrag DECIMAL

    DECLARE @fillfactor INT

    -- Decide on the maximum fragmentation to allow

    SELECT @maxfrag = 1.0

    SELECT @fillfactor = 70

    -- Declare cursor

    DECLARE tables CURSOR FOR

    SELECT TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME

    FROM INFORMATION_SCHEMA.TABLES

    WHERE TABLE_TYPE = 'BASE TABLE'

    -- Create the table

    CREATE TABLE #fraglist (

    ObjectName CHAR (255),

    ObjectId INT,

    IndexName CHAR (255),

    IndexId INT,

    Lvl INT,

    CountPages INT,

    CountRows INT,

    MinRecSize INT,

    MaxRecSize INT,

    AvgRecSize INT,

    ForRecCount INT,

    Extents INT,

    ExtentSwitches INT,

    AvgFreeBytes INT,

    AvgPageDensity INT,

    ScanDensity DECIMAL,

    BestCount INT,

    ActualCount INT,

    LogicalFrag DECIMAL,

    ExtentFrag DECIMAL)

    -- Open the cursor

    OPEN tables

    -- Loop through all the tables in the database

    FETCH NEXT

    FROM tables

    INTO @tablename

    PRINT RTRIM(@tablename)

    WHILE @@FETCH_STATUS = 0

    BEGIN

    -- Do the showcontig of all indexes of the table

    INSERT INTO #fraglist

    EXEC ('DBCC SHOWCONTIG (''' + @tablename + ''')

    WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS')

    FETCH NEXT

    FROM tables

    INTO @tablename

    END

    -- Close and deallocate the cursor

    CLOSE tables

    DEALLOCATE tables

    -- Declare cursor for list of indexes to be defragged

    DECLARE indexes CURSOR FOR

    SELECT table_catalog + '.' + table_schema + '.' + ObjectName, ObjectId, IndexName, IndexId, LogicalFrag

    FROM #fraglist, INFORMATION_SCHEMA.TABLES

    WHERE #fraglist.ObjectName = INFORMATION_SCHEMA.TABLES.TABLE_NAME

    AND LogicalFrag >= @maxfrag

    AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0

    -- Open the cursor

    OPEN indexes

    -- loop through the indexes

    FETCH NEXT

    FROM indexes

    INTO @tablename, @objectid, @indexname, @indexid, @frag

    WHILE @@FETCH_STATUS = 0

    BEGIN

    PRINT 'Executing ALTER INDEX ' + RTRIM(@indexname) + ' ON ' + RTRIM(@tablename) + ' REBUILD WITH (FILLFACTOR = ' + CAST(@fillfactor AS CHAR(3)) + ')'

    SELECT @execstr = 'ALTER INDEX ' + RTRIM(@indexname) + ' ON ' + RTRIM(@tablename) + ' REBUILD WITH (FILLFACTOR = ' + CAST(@fillfactor AS CHAR(3)) + ')'

    EXEC (@execstr)

    FETCH NEXT

    FROM indexes

    INTO @tablename, @objectid, @indexname, @indexid, @frag

    END

    -- Close and deallocate the cursor

    CLOSE indexes

    DEALLOCATE indexes

    -- Delete the temporary table

    DROP TABLE #fraglist

    Any comments are greatly appreciated.

    Thank you

    -David

    Best Regards,

    ~David

  • 'Use MyDatabase' should accomplish this

    Since you asked specifically about "Use" I assume you have tried it and it didnt work?

  • You can not use a "USE " statement within a stored procedure. I am not sure what you are suggesting here. Could you please clarify?

    Thank you

    -David

    Best Regards,

    ~David

  • The best way to do that might be to set it up to be able to run from Master, but using the current DB's context.

    Take a look at the following:

    http://weblogs.sqlteam.com/mladenp/archive/2007/01/18/58287.aspx

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • This is perfect. It works! Thank you very much.

    Best Regards,

    ~David

Viewing 5 posts - 1 through 4 (of 4 total)

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