Tuesday, October 8, 2024

Data Fil Shrink in Chunk in SQL server

 Use Apriso_IR1_PREPRD20

go

SELECT
    a.FILEID AS file_id,
    CONVERT(DECIMAL(12,2),ROUND(a.size/128.000,2)) AS [file_size_mb],
    CONVERT(DECIMAL(12,2),ROUND(FILEPROPERTY(a.name,'SpaceUsed')/128.000,2)) AS [space_used_mb] ,
    CONVERT(DECIMAL(12,2),ROUND((a.size-FILEPROPERTY(a.name,'SpaceUsed'))/128.000,2)) AS [free_space_mb],
    a.NAME AS name,
    a.FILENAME AS physical_name
FROM
    sys.sysfiles a



==============================================

Find script to shrink datafile by 2 GB.


Use Apriso_IR1_PREPRD20
go

declare @fsize int, @esize int, @fno int
Set @esize = 1118697.38 --Provide the existing size of the file in MB 81920000 KB
Set @fsize = 862312.56 --Provide the final size of the file needed in MB
Set @fno = 1 --Provide the file number that needs to be shrunk
While (@fsize <= @esize)
Begin

Print 'DBCC SHRINKFILE ('+rtrim(cast(@fno as varchar(5)))+','+rtrim(cast(@esize as varchar(10)))+')'+Char(10)+'Go'
Set @esize = @esize - 1024

End

No comments:

Post a Comment

Thanks For Visit

DBA discussion

 Here is a detailed prompt for creating a database administrator (DBA) automation tool. This prompt can be used with GitHub Copilot, ChatGPT...