create proc myproc
as
begin
DECLARE @name VARCHAR(50),@fileName VARCHAR(256),@deleteInt int
CREATE TABLE ##Temp
(
Count integer NOT NULL,
Name nvarchar(50)
);
DECLARE db_cursor CURSOR FOR
SELECT top 1000
t.NAME AS TableName--,
--i.name AS indexName,
--SUM(p.rows) AS RowCounts,
--SUM(a.total_pages) AS TotalPages,
--SUM(a.used_pages) AS UsedPages,
--SUM(a.data_pages) AS DataPages,
--(SUM(a.total_pages) * 8) / 1024 AS TotalSpaceMB,
--(SUM(a.used_pages) * 8) / 1024 AS UsedSpaceMB,
--(SUM(a.data_pages) * 8) / 1024 AS DataSpaceMB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
WHERE
t.NAME NOT LIKE 'dt%' AND
i.OBJECT_ID > 255 AND
i.index_id <= 1
GROUP BY
t.NAME, i.object_id, i.index_id, i.name
--8 desc
--SELECT name ,size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0 AS AvailableSpaceInMB FROM sys.database_files order by 2; select * from sys.database_files
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = 'select count(*),'+ '''' + @name + '''' + ' FROM dba.' + @name
INSERT ##Temp (Count,Name)
EXEC (@fileName)
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
DELETE FROM ##Temp where COUNT <= 10000
set @deleteInt = 0
DECLARE db_cursor1 CURSOR FOR select name from ##Temp
OPEN db_cursor1
FETCH NEXT FROM db_cursor1 INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
Set @deleteInt = (select top 1 COUNT from ##Temp where Name = @name)
SET @deleteInt = @deleteInt - 10000
SET @fileName = 'DELETE top(' + CAST(@deleteInt AS VARCHAR(10)) + ')' + 'FROM dba.' + @name
print @fileName
exec (@filename)
FETCH NEXT FROM db_cursor1 INTO @name
END
CLOSE db_cursor1
DEALLOCATE db_cursor1
end
go
exec myproc
as
begin
DECLARE @name VARCHAR(50),@fileName VARCHAR(256),@deleteInt int
CREATE TABLE ##Temp
(
Count integer NOT NULL,
Name nvarchar(50)
);
DECLARE db_cursor CURSOR FOR
SELECT top 1000
t.NAME AS TableName--,
--i.name AS indexName,
--SUM(p.rows) AS RowCounts,
--SUM(a.total_pages) AS TotalPages,
--SUM(a.used_pages) AS UsedPages,
--SUM(a.data_pages) AS DataPages,
--(SUM(a.total_pages) * 8) / 1024 AS TotalSpaceMB,
--(SUM(a.used_pages) * 8) / 1024 AS UsedSpaceMB,
--(SUM(a.data_pages) * 8) / 1024 AS DataSpaceMB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
WHERE
t.NAME NOT LIKE 'dt%' AND
i.OBJECT_ID > 255 AND
i.index_id <= 1
GROUP BY
t.NAME, i.object_id, i.index_id, i.name
--ORDER BY
---- OBJECT_NAME(i.object_id) --8 desc
--SELECT name ,size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0 AS AvailableSpaceInMB FROM sys.database_files order by 2; select * from sys.database_files
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = 'select count(*),'+ '''' + @name + '''' + ' FROM dba.' + @name
INSERT ##Temp (Count,Name)
EXEC (@fileName)
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
DELETE FROM ##Temp where COUNT <= 10000
set @deleteInt = 0
DECLARE db_cursor1 CURSOR FOR select name from ##Temp
OPEN db_cursor1
FETCH NEXT FROM db_cursor1 INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
Set @deleteInt = (select top 1 COUNT from ##Temp where Name = @name)
SET @deleteInt = @deleteInt - 10000
SET @fileName = 'DELETE top(' + CAST(@deleteInt AS VARCHAR(10)) + ')' + 'FROM dba.' + @name
print @fileName
exec (@filename)
FETCH NEXT FROM db_cursor1 INTO @name
END
CLOSE db_cursor1
DEALLOCATE db_cursor1
end
go
exec myproc
No comments:
Post a Comment