views:

45

answers:

3

Simple problem. I'm working on a single SQL Server database which is shared between several offices. Each office has their own schema inside this database, thus dividing the database in logical pieces. (Plus one schema that is shared between multiple offices.) The database is stored on a dedicated server and we use a single database to keep the backup/restore procedure easier.

The problem, however, is that the Accounting Office might be modifying a lot of data and then the Secretary Office makes a mistake which requires restoration of a backup. Unfortunately, restoring the backup means that Accounting will lose their recently added data.

So, the alternative solution is by restoring the backup into a new database, remove the data from the old accounting schema and move the data for accounting only from the backup top the original database. This is the current solution and it's time-consuming and error-prone.

So, is there a way to make backups of a single schema, possibly through code? And then to restore just that schema, probably through code too?

A: 

There is no way to backup and restore just a single schema.

However, you may try this approach: Restore the entire database (all schemas) to a different database xyz_OLD or something like that.

You could then fix the data using a script like:

UPDATE y
    SET col1=o.col1
    FROM xyz.YourTable y
        INNER JOIN xyz_Old.xyz.YourTable o ON y.PK=o.PK

INSERT INTO xyz.YourTable
        (col1, col2, col3,...)
    SELECT
        col1, col2, col3,...
        FROM xyz_Old.xyz.YourTable o
        WHERE NOT EXISTS (SELECT 1 FROM xyz.YourTable y WHERE o.PK=y.Pk)

etc...
KM
That won't work, since data will also be removed. The only suggestion I know that would work would be by trunking all tables in a single schema and then move them from the backup database to the schema. While possible, it just sounds horrible...
Workshop Alex
this works for deletes to, I just didn't write the DELETE, you delete from the local table where the old does not exist.
KM
+1  A: 

You could create a script that copies each of the schemas to a separate database (backup_Accounting, backup_Secretary, backup_Shared), and then creates a backup file for each of those databases. If you ever need to do a restore, you can restore the backup file into the appropriate database and then run a script to copy the data back into the main DB.

Josh Yeager
That's the idea I'm having. But how to create a script that will backup and restore all data from each and every table in a single schema? (Without needing to list all tables in it.)
Workshop Alex
+1  A: 

You could use filegroups and the partial backup command.

You'll need to move each schema to a different filegroup and then use partial backup/restores as required.

See here for info on partial backups: http://msdn.microsoft.com/en-us/library/ms191539.aspx

See here for info on file groups: http://msdn.microsoft.com/en-us/library/ms179316.aspx

See here for info on piecemeal restore: http://msdn.microsoft.com/en-us/library/ms177425.aspx

Daniel Renshaw
Interesting suggestion. Unfortunately we chose to combine everything inside a single database just to avoid having a lot of database files. Also, how do I make it clear to SQL Server which schema needs to be in which file? And will it keep all data for all tables within that schema inside the same file?
Workshop Alex
I don't think there's any way to automate the relationship between schema and filegroup. Which filegroup a table is created in is defined when the CREATE TABLE statement is issued (via the ON clause). If lots of people can create tables then enforcing the schema/filegroup relationship may be diffcicult to enforce. However, you might be able to use DDL triggers to enforce the relationship. See http://msdn.microsoft.com/en-us/library/ms190989(v=SQL.100).aspx (these are available in SQL2005 too).
Daniel Renshaw