jeudi 18 décembre 2014

Identify unattached File Size in SQL Server


From a similar post on this forum, I have modified the below code as per comments and able to get the orphaned files in the drives. The code is :



EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
GO

if object_id('tempdb.dbo.#os_files') is not null
drop table #os_files
create table #os_files([filename] varchar(2000))

insert into #os_files exec xp_cmdshell 'DIR D:\SQL04\*.mdf /b /s'
insert into #os_files exec xp_cmdshell 'DIR D:\SQL04\*.ldf /b /s'

delete from #os_files where filename is null

update #os_files set filename=rtrim(filename)

select
os.filename as orphaned_files
from
#os_files os
left outer join master.sys.master_files db on rtrim(db.physical_name) = os.filename
where
db.database_id is null


EXEC sp_configure 'xp_cmdshell', 0;
EXEC sp_configure 'show advanced options', 0;
RECONFIGURE;
GO


Is it possible in any way to modify the above code and get the size of the files in the output apart from physical file names? Also is there any other way to modify the above code to search for files other than .mdf and .ldf files in the content volumes, where the database and log files reside. In our environment we have folders where mdf and ldf files are kept separately. Hence, the need.


The output that I'm expecting should be like this:



PHYSICAL_FILENAME SIZE
file1 s1
file2 s2


Kindly look into this issue and give your suggestion. Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire