如何动态地将列添加到临时表?

时间:2015-04-23 07:31:16

标签: sql-server sql-server-2008

我试图获取数据库列表中所有表的大小。我遇到的问题是,每个数据库中的表名都相同。如何将第一列添加到带有数据库名称的#tmpTableSizes中:

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 '?'"

2 个答案:

答案 0 :(得分:1)

直截了当的解决方案:再使用一个“缓冲区”表,首先插入缓冲区表,然后从中进入“主”临时表,并指定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 into #tempTableSizes
select 'AAABD', * from #tempTableSizesDB

truncate table #tempTableSizesDB

insert #tmpTableSizesDB
EXEC BBBDB.dbo.sp_MSforeachtable @command1="EXEC sp_spaceused '?'"

insert into #tempTableSizes
select 'BBBDB', * from #tempTableSizesDB

truncate table #tempTableSizesDB

insert #tmpTableSizesDB
EXEC CCCDB.dbo.sp_MSforeachtable @command1="EXEC sp_spaceused '?'"

insert into #tempTableSizes
select 'CCCDB', * from #tempTableSizesDB

或者你可以使用动态sql进行循环:

create table dbo.#DBases (Name nvarchar(100))
insert into dbo.#DBases
select 'AAABD'
union all 
select 'BBBDB'
union all 
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 for select Name from dbo.#DBases
open cur

fetch next from cur into @DBName

while (@@fetch_status = 0)
begin
    select @stmt =  'truncate table dbo.#tmpTableSizesDB 

                     insert into dbo.#tmpTableSizesDB
                     EXEC ' + @DBName + '.dbo.sp_MSforeachtable @command1="EXEC sp_spaceused ''?''"

                     insert into dbo.#tmpTableSizes
                     select ''' + @DBName + ''', * from dbo.#tmpTableSizesDB'

    exec sp_executesql @stmt = @stmt

    fetch next from cur into @DBName
end

close cur
deallocate cur

drop table dbo.#tmpTableSizesDB
drop table dbo.#DBases 

select * from dbo.#tmpTableSizes

答案 1 :(得分:1)

简单,

添加列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)
);

您必须在执行插入时列出列并更新所有新插入行的dbname(这些是dbname为null的唯一行)

 insert #tmpTableSizes 
   ( tableName, numberofRows, reservedSize, dataSize, indexSize, unusedSize)
 EXEC AAABD.dbo.sp_MSforeachtable @command1="EXEC sp_spaceused '?'";

 update #tmpTableSizes set dbname='AAABD' where dbname is null;

因为你添加了做循环的想法:

 exec sp_msforeachdb 
 'insert #tmpTableSizes 
   ( tableName, numberofRows, reservedSize, dataSize, indexSize, unusedSize)
 EXEC ?.dbo.sp_MSforeachtable @command1=''EXEC ?.dbo.sp_spaceused ''!'' @replacechar=''!''
 ', '?', 
 'update #tmpTableSizes set dbname=''?'' where dbname is null';

不太确定我是否添加了足够',您必须检查