Estimating backup size

  • how do i get run query against all databases , I tried following but it does not seems to be working, it returns results from only one database

     

    declare @cmd varchar(8000)='select SUM (reserved_page_count) * 8 from sys.dm_db_partition_stats'
    declare @db sysname
    declare @use varchar(8000)
    declare c1 cursor for select name from master..sysdatabases where dbid <>2
    open c1
    fetch next from c1 into @db
    while @@fetch_status=0
    begin
    set @use='use [' + cast(@db as varchar(200)) + '];'+ @cmd
    print @use
    exec(@cmd)
    fetch next from c1 into @db
    end
    close c1
    deallocate c1
    go
    exec sp_msforeachdb "select SUM (reserved_page_count) * 8 from sys.dm_db_partition_stats"

  • sp_msforeachdb accepts parameters as well, there's no need for your own cursor.

    This would work:exec sp_msforeachdb 'USE [?];

    IF DB_ID(''?'') != 2 BEGIN
      SELECT ''[?]'' AS DBName, SUM (reserved_page_count) * 8 AS Estimation
      FROM sys.dm_db_partition_stats;
    END';

    Edit: Fixed odd paste. Is it just me, or do pastes go funny on the new forum sometimes..?

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thom A - Tuesday, January 17, 2017 9:17 AM

    sp_msforeachdb accepts parameters as well, there's no need for your own cursor.

    This would work:exec sp_msforeachdb 'USE [?];

    IF DB_ID(''?'') != 2 BEGIN
      SELECT ''[?]'' AS DBName, SUM (reserved_page_count) * 8 AS Estimation
      FROM sys.dm_db_partition_stats;
    END';

    Edit: Fixed odd paste. Is it just me, or do pastes go funny on the new forum sometimes..?

    Thank you Sir

  • goher2000 - Tuesday, January 17, 2017 9:04 AM

    how do i get run query against all databases , I tried following but it does not seems to be working, it returns results from only one database

     

    declare @cmd varchar(8000)='select SUM (reserved_page_count) * 8 from sys.dm_db_partition_stats'
    declare @db sysname
    declare @use varchar(8000)
    declare c1 cursor for select name from master..sysdatabases where dbid <>2
    open c1
    fetch next from c1 into @db
    while @@fetch_status=0
    begin
    set @use='use [' + cast(@db as varchar(200)) + '];'+ @cmd
    print @use
    exec(@cmd)

    fetch next from c1 into @db
    end
    close c1
    deallocate c1
    go
    exec sp_msforeachdb "select SUM (reserved_page_count) * 8 from sys.dm_db_partition_stats"

    I higlighted the error in your code.
    🙂

    _____________
    Code for TallyGenerator

  • Sergiy - Wednesday, January 18, 2017 2:55 PM

    goher2000 - Tuesday, January 17, 2017 9:04 AM

    how do i get run query against all databases , I tried following but it does not seems to be working, it returns results from only one database

     

    declare @cmd varchar(8000)='select SUM (reserved_page_count) * 8 from sys.dm_db_partition_stats'
    declare @db sysname
    declare @use varchar(8000)
    declare c1 cursor for select name from master..sysdatabases where dbid <>2
    open c1
    fetch next from c1 into @db
    while @@fetch_status=0
    begin
    set @use='use [' + cast(@db as varchar(200)) + '];'+ @cmd
    print @use
    exec(@cmd)

    fetch next from c1 into @db
    end
    close c1
    deallocate c1
    go
    exec sp_msforeachdb "select SUM (reserved_page_count) * 8 from sys.dm_db_partition_stats"

    I higlighted the error in your code.
    🙂

    lo, my bad.... thanks, I have fixed the code, however I am going with sp_foreachdb method. 

    declare @cmd varchar(8000)='select SUM (reserved_page_count) * 8 from sys.dm_db_partition_stats'
    declare @db sysname
    declare @use varchar(8000)
    declare c1 cursor for select name from master..sysdatabases where dbid <>2
    open c1
    fetch next from c1 into @db
    while @@fetch_status=0
    begin
    set @use='use [' + cast(@db as varchar(200)) + '];'+ @cmd
    print @use
    exec(@USE)
    fetch next from c1 into @db
    end
    close c1
    deallocate c1

  • goher2000 - Wednesday, January 18, 2017 3:53 PM

    Sergiy - Wednesday, January 18, 2017 2:55 PM

    goher2000 - Tuesday, January 17, 2017 9:04 AM

    how do i get run query against all databases , I tried following but it does not seems to be working, it returns results from only one database

     

    declare @cmd varchar(8000)='select SUM (reserved_page_count) * 8 from sys.dm_db_partition_stats'
    declare @db sysname
    declare @use varchar(8000)
    declare c1 cursor for select name from master..sysdatabases where dbid <>2
    open c1
    fetch next from c1 into @db
    while @@fetch_status=0
    begin
    set @use='use [' + cast(@db as varchar(200)) + '];'+ @cmd
    print @use
    exec(@cmd)

    fetch next from c1 into @db
    end
    close c1
    deallocate c1
    go
    exec sp_msforeachdb "select SUM (reserved_page_count) * 8 from sys.dm_db_partition_stats"

    I higlighted the error in your code.
    🙂

    lo, my bad.... thanks, I have fixed the code, however I am going with sp_foreachdb method. 

    declare @cmd varchar(8000)='select SUM (reserved_page_count) * 8 from sys.dm_db_partition_stats'
    declare @db sysname
    declare @use varchar(8000)
    declare c1 cursor for select name from master..sysdatabases where dbid <>2
    open c1
    fetch next from c1 into @db
    while @@fetch_status=0
    begin
    set @use='use [' + cast(@db as varchar(200)) + '];'+ @cmd
    print @use
    exec(@USE)
    fetch next from c1 into @db
    end
    close c1
    deallocate c1

    here is what I am using now, for those who want to use this must run update stats so you may get better estimation

    exec sp_msforeachdb "use [?];select '?' dbname, SUM (reserved_page_count) * 8 Estimated_Backup_size from sys.dm_db_partition_stats"

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

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