Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

comparing physcial file name of the database Expand / Collapse
Author
Message
Posted Monday, December 1, 2008 5:12 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, September 12, 2009 9:12 PM
Points: 65, Visits: 224
hi,

Iam using below query to compare the physical location specified,but it is not showing the correct result and as the sys.files it is in diffrent

Is there any other method to compare
as per the structure all my data folders to be in E:drive
asnd log will be in f drive
i want to know which db is not following the correct structure

print('Filename')
exec('use['+@database_name+']
insert #file_check
select '''+@database_name+''',
(select filename from sys.sysfiles where fileid =1 and filename not like ''H:\MSSQL\'+@database_name+'\DATA\'+@database_name+'_Data.mdf''),
(select filename from sys.sysfiles where fileid =2 and filename not like ''I:\MSSQL\'+@database_name+'\LOG\'+@database_name+'_Log.ldf'')
from sys.sysfiles;')

Is there any other method to find out,this is working for some databases
some databases it is not

it is checking Sys.files table and the entire string is matching only it displays the correct result.

Is sys.master files can be used for this

Or please let me know the best method to find out the wrong structure.


Post #611199
Posted Monday, December 1, 2008 5:43 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, May 23, 2014 5:59 AM
Points: 329, Visits: 470
This may help you

declare @files table(dbname varchar(100), filename varchar(1000))
insert into @files
exec sp_msforeachdb 'select ''?'', filename from ?..sysfiles'
select dbname,min(left(filename,1)),max(left(filename,1)) from @files
group by dbname
having min(left(filename,1))<>'E' or max(left(filename,1))<>'F'

Note that sp_msforeachdb is undocumented which you can simulate using
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/05/13/simulating-undocumented-procedures.aspx




Madhivanan

Failing to plan is Planning to fail
Post #611228
Posted Monday, December 1, 2008 6:27 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 4:53 AM
Points: 6,423, Visits: 13,817
to get a list of database and filenames use

select name, fileid, filename from sys.sysaltfiles


-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs"
Post #611263
Posted Monday, December 1, 2008 11:11 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, September 12, 2009 9:12 PM
Points: 65, Visits: 224
hi,
i need to display which database is not following the correct path,
as per the code i have written it should be that location,what iw ant is it should display if it is not matching
Post #611519
Posted Monday, December 1, 2008 11:15 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, September 12, 2009 9:12 PM
Points: 65, Visits: 224
hi,

thanks,my code is getting me the result in coupel of instances,onely few instances ,sys.files filename location is stored as case sensitive,that is why it is giving error messge,

is there any method to modify my code and get the result,i need to check all db,if it is not macthcing with the format specified,
i should get the result of databases which is not correct as per the folder structure..
Post #611528
Posted Monday, December 1, 2008 2:30 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, May 22, 2014 7:04 AM
Points: 1,688, Visits: 8,766
Try this:

DECLARE @database_name SYSNAME
DECLARE @iRowCount INT
DECLARE @t_TableNames_Temp TABLE
(table_name SYSNAME)
drop table #file_check
create table #file_check(DBName sysname,MDFFile sysname,LDFFile sysname)

INSERT @t_TableNames_Temp
SELECT name
FROM SYSDATABASES
WHERE name not in ('pubs','northwind','tempdb')
ORDER BY name
--Getting row count from table
SELECT @iRowCount = COUNT(*) FROM @t_TableNames_Temp
WHILE @iRowCount > 0
BEGIN
SELECT @database_name = table_name from @t_TableNames_Temp
exec('use['+@database_name+']
insert #file_check
select '''+@database_name+''',
(select rtrim(filename) from sysfiles where fileid =1 and filename not like ''H:\MSSQL\'+@database_name+'\DATA\'+@database_name+'_Data.mdf''),
(select rtrim(filename) from sysfiles where fileid =2 and filename not like ''I:\MSSQL\'+@database_name+'\LOG\'+@database_name+'_Log.ldf'')
from sysfiles;')
DELETE FROM @t_TableNames_Temp WHERE @database_name = table_name
SELECT @iRowCount = @iRowCount - 1
END
Select * from #file_check
SET NOCOUNT OFF

HTH
MJ
Post #611688
Posted Monday, December 1, 2008 4:48 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:55 PM
Points: 5,333, Visits: 25,266
Shine.mm If this copied correctly
(select filename from sys.sysfiles where fileid =1 and filename not like ''H:\MSSQL\'+@database_name+'\DATA\'+@database_name+'_Data.mdf''),

You have a EXTRA ' between like and and the drive letter H and after the _Data.mdf Modifiying your code to eliminate those additional characters

(select filename from sys.sysfiles where fileid =1
and filename not like ('H:\MSSQL\'+@database_name+'\DATA\'+@database_name+'_Data.mdf'))
does indeed generate a return.
sys.files filename location is stored as case sensitive,

I have tested the following 2 select statements and they show that the filename entry in sys.sysfiles is NOT case sensitive.

SELECT fileid, name
FROM sys.sysfiles
WHERE filename NOT LIKE ('C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\TIPSnTRICKS_Data.MDF')

SELECT fileid, name
FROM sys.sysfiles
WHERE filename NOT LIKE ('c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\tipsntricks_data.mdf')


If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Post #611749
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse