sql server - How to add column dynamically to temporary table? -


i'm trying size of tables in list of databases. problem have is, tables have same name in each database. how can add first column #tmptablesizes database name:

create table #tmptablesizes (     tablename varchar(100),     numberofrows varchar(100),     reservedsize varchar(50),     datasize varchar(50),     indexsize varchar(50),     unusedsize varchar(50) )  insert #tmptablesizes exec aaabd.dbo.sp_msforeachtable @command1="exec sp_spaceused '?'" insert #tmptablesizes exec bbbdb.dbo.sp_msforeachtable @command1="exec sp_spaceused '?'" insert #tmptablesizes exec cccdb.dbo.sp_msforeachtable @command1="exec sp_spaceused '?'" 

straightforward solution: use 1 more "buffer" table, first insert buffer table , "main" temporary table specification of dbname

create table #tmptablesizes (     dbname varchar(100),     tablename varchar(100),     numberofrows varchar(100),     reservedsize varchar(50),     datasize varchar(50),     indexsize varchar(50),     unusedsize varchar(50) )   create table #tmptablesizesdb (     tablename varchar(100),     numberofrows varchar(100),     reservedsize varchar(50),     datasize varchar(50),     indexsize varchar(50),     unusedsize varchar(50) )  insert #tmptablesizesdb exec aaabd.dbo.sp_msforeachtable @command1="exec sp_spaceused '?'"  insert #temptablesizes select 'aaabd', * #temptablesizesdb  truncate table #temptablesizesdb  insert #tmptablesizesdb exec bbbdb.dbo.sp_msforeachtable @command1="exec sp_spaceused '?'"  insert #temptablesizes select 'bbbdb', * #temptablesizesdb  truncate table #temptablesizesdb  insert #tmptablesizesdb exec cccdb.dbo.sp_msforeachtable @command1="exec sp_spaceused '?'"  insert #temptablesizes select 'cccdb', * #temptablesizesdb 

or can in in loop using dynamic sql:

create table dbo.#dbases (name nvarchar(100)) insert dbo.#dbases select 'aaabd' union  select 'bbbdb' union  select 'cccdb'   create table dbo.#tmptablesizes (     dbname varchar(100),     tablename varchar(100),     numberofrows varchar(100),     reservedsize varchar(50),     datasize varchar(50),     indexsize varchar(50),     unusedsize varchar(50) )   create table dbo.#tmptablesizesdb (     tablename varchar(100),     numberofrows varchar(100),     reservedsize varchar(50),     datasize varchar(50),     indexsize varchar(50),     unusedsize varchar(50) )  declare @stmt nvarchar(max), @dbname nvarchar(100)  declare cur cursor select name dbo.#dbases open cur  fetch next cur @dbname  while (@@fetch_status = 0) begin     select @stmt =  'truncate table dbo.#tmptablesizesdb                        insert dbo.#tmptablesizesdb                      exec ' + @dbname + '.dbo.sp_msforeachtable @command1="exec sp_spaceused ''?''"                       insert dbo.#tmptablesizes                      select ''' + @dbname + ''', * dbo.#tmptablesizesdb'      exec sp_executesql @stmt = @stmt      fetch next cur @dbname end  close cur deallocate cur  drop table dbo.#tmptablesizesdb drop table dbo.#dbases   select * dbo.#tmptablesizes 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -