Archive for Май 2011
Количество строк во всех таблицах (T-SQL)
Иногда полезно для реверс инижинеринга
declare @tmp table (name varchar(50), rcount int) declare @sTblName varchar(50) declare @nRowCount int -- the rows declare @nObjectID int -- Object ID declare iter cursor for select TABLE_NAME from information_schema.tables open iter fetch next from iter into @sTblName while (@@FETCH_STATUS = 0) begin set @nObjectID = OBJECT_ID(@sTblName) if @nObjectID is not null begin select TOP 1 @nRowCount = rows from sysindexes where id = @nObjectID AND indid < 2 insert into @tmp values (@sTblName, @nRowCount) end fetch next from iter into @sTblName end close iter deallocate iter select * from @tmp order by rcount desc
UPD:
Если есть таблицы не в dbo схеме
declare @tmp table (name varchar(60), rcount int) declare @tblName varchar(50) declare @schema varchar(10) declare @nRowCount int -- the rows declare @nObjectID int -- Object ID declare iter cursor for select TABLE_NAME, TABLE_SCHEMA from information_schema.tables open iter fetch next from iter into @tblName, @schema while (@@FETCH_STATUS = 0) begin set @nObjectID = OBJECT_ID(@schema+'.'+@tblName) if @nObjectID is not null begin select TOP 1 @nRowCount = rows from sysindexes where id = @nObjectID AND indid < 2 insert into @tmp values (@tblName, @nRowCount) end fetch next from iter into @tblName, @schema end close iter deallocate iter
UPD2:
И индексы перебилдить все скопом (в тырнете нашёл)
DECLARE @TableName VARCHAR(255) DECLARE @sql NVARCHAR(500) DECLARE @fillfactor INT SET @fillfactor = 80 DECLARE TableCursor CURSOR FOR SELECT OBJECT_SCHEMA_NAME([object_id])+'.'+name AS TableName FROM sys.tables OPEN TableCursor FETCH NEXT FROM TableCursor INTO @TableName WHILE @@FETCH_STATUS = 0 BEGIN SET @sql = 'ALTER INDEX ALL ON ' + @TableName + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')' EXEC (@sql) FETCH NEXT FROM TableCursor INTO @TableName END CLOSE TableCursor deallocate TableCursor