Home Forums SQL Server 7,2000 T-SQL sp_MSForeachdb then sp_MSforeachtable RE: sp_MSForeachdb then sp_MSforeachtable

  • Just to mention that if the database name contains - like test-test then the script will fail.

    The database name needs to be enclosed between square brackets.

    I suggest this modified version

    USE [TempPAST]

    GO

    /****** Object: StoredProcedure [dbo].[usp_DataRowCounts] Script Date: 08/03/2010 12:22:42 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    alter PROCEDURE [dbo].[usp_DataRowCounts] AS

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    declare csr_db cursor for

    select [name] from master..sysdatabases

    where [dbid] >4

    declare @cmd varchar(max)

    declare @dbs varchar(256)

    declare @dbs2 varchar(256)

    open csr_db

    fetch next from csr_db into @dbs

    while @@fetch_status = 0

    BEGIN

    set @dbs2 = '[' + @dbs + ']'

    set @cmd = @dbs2 + '..sp_msforeachtable '' insert into TempPAST..tblTabledata(vchtablename,introws,vchreservedspace,vchData,vchIndexSize,vchUnusedSpace) exec sp_spaceused ''''?'''' '''

    print @cmd

    exec(@cmd)

    set @cmd = 'update TempPAST..tblTabledata set vchdatabasename = ''' + @dbs2 + ''' where vchdatabasename is null'

    print (@cmd)

    exec(@cmd)

    fetch next from csr_db into @dbs

    END

    close csr_db

    deallocate csr_db

    Thanks,

    Davide.