Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 sysfiles system table

Author  Topic 

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-06-11 : 17:48:13
BOL says that sysfiles "contains one row for each file in a database".

BOL also says that sysaltfiles "under special circumstances, contains rows corresponding to the files in a database"

Now to my question...

I am creating a stored procedure that will grab the last full backup of each of the user databases (well not pubs or Northwind of course) and restore it to another server. I am doing this to test out the backup. I need to use the WITH MOVE option in the RESTORE command since the paths will not be the same. In order to use the WITH MOVE option, I need to know what the logical file names are. To do this, you get the name column from sysfiles, well at least that's what I thought. But...

Now I was under the impression that statement number one from above was true. Well I've got this development server with a bunch of databases on it. Only the master database information is in sysfiles. The other databases are in sysaltfiles (plus master actually). I can easily get the information from sysaltfiles if it doesn't exist in sysfiles, but what I want to know is why aren't the rows there on my dev server for each of the database. I also want to know when sysaltfiles is used since it says under special circumstances. The only thing that I can think of is that the databases were created from backups and that's when it happens. What do you think?


Below is the code to get the last backup (doesn't include the call to sysfiles or sysaltfiles yet). The stored procedure will also have the KILL statements in case the database exists already and users are connected, and it will also do the restore.

SELECT bs.database_name, MAX(bms.physical_device_name)
FROM TESTRESTORE.msdb.dbo.backupset bs
INNER JOIN TESTRESTORE.msdb.dbo.backupmediafamily bms ON bs.media_set_id = bms.media_set_id
INNER JOIN TESTRESTORE.master.dbo.sysdatabases s ON bs.database_name = s.name
WHERE CONVERT(VARCHAR(20), bs.backup_finish_date, 101) = CONVERT(VARCHAR(20), GETDATE(), 101) AND
s.name NOT IN ('master', 'msdb', 'model', 'pubs', 'Northwind')
GROUP BY bs.database_name



Does anyone else have code that does something similar?

[EDIT] Forgot to mention that in my code that TESTRESTORE is a linked server that I setup for this. It will have a different name later though. The way that it is written right now is that the code needs to run on the server where the restore will occur. [/EDIT]

Tara

Edited by - tduggan on 06/11/2003 17:54:57

nr
SQLTeam MVY

12543 Posts

Posted - 2003-06-11 : 17:59:28
The database file info is in sysfiles in the that database. sysaltfiles is only in master.

I wondered about the special circumstances two - I needed to get the file sizes of all databases so went to dbname..sysfiles for each database.
All the entries are in master..sysaltfiles but haven't restored any.

You can also get the logical file info from the backup by restore filelist only.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-06-11 : 18:38:57
Ah yes, I forgot that sysfiles was in the user databases. I should have my ERD of the system tables on a wall, but I am out of room in my office.

I also forgot about the filelistonly option. How do you get that result set into a temporary table? Or in other words, how do I get LogicalName out of filelistonly so that I can use it?

Tara
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-06-11 : 18:42:16
Oh nevermind, I'll just use sysfiles in the user database. Much easier that way.

Tara
Go to Top of Page
   

- Advertisement -