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?
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.
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
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...
Related
I did my homework on how to copy a database, some say need new name for mdf and log files, some say that REPLACE will take care of it.
I'm on same server, SQL Server 2016. I don't have freedom to create new files or do any copy or renames of them. Can not use DBCC as well (for CLONE).
SourceDB is existing db : 10 tables.
SourceDB_Data c:\SPath\SourceDB.mdf
SourceDB Log c:\SPath\SourceDB_log.ldf
NewDB is existing db : 3 tables, 2 views
NewDB_Data c:\NewDBPath\NewDB.mdf
NewDB_Log c:\NewDBPath\NewDB_log.ldf
Main thing that structure of those databases are totally different, but I need to copy structure and all content into NewDB. I can NOT drop/ recreate it, too many handles connected to NewDB and they will start ringing if it will disappear. Alas this rules I have. I can NOT drop this NewDB in any case.
Though. I can do DDL on NewDB, maybe as last resort I can recreate SourceDB Schema on it?
My plan still go with bkup just for the case. Will this work and keep same logical and physical files names on NewDB? Don't want to produce any error). Thanks
Bkup SourceDB
'c:\Path\to\SourceDB.buk'
Set NewDB into single mode
Run script:
Restore Database NewDB From Disk = 'c:\Path\to\SourceDB.bak' With Replace
Goal to get NewDB (keep old name) with NewDB.MDF names and and content of SourceDB.
I have developed a SQL Express database. I need to backup all but one table in that database in an automated way. I was thinking i could write a SQL script to do this, trigger it using sqlcmd from a batch file but not sure how to write that SQL script.
I was also thinking, if nothing else possible, i could create a second db that has the tables i want to backup then i write a script that copys data 'into' the second db and then do a auto backup of that entire db. This has the disadvantage of having a procrastinated unpacking of that backup when wanting to use it - its not a small install script.
Is this a possibility, is it the only option or is there tools for SQL Express to do this?
There is no option to exclude just one table while backing up .Few things i could think of
1.Right click database ->Tasks ->generate scripts ->exclude the table you want and choose to save the script and run this every time
2.you could also choose Export option,but since you are using SQL Express,you wont have the option to save this package
Keep the large table in a different database and just backup the original database. You can still use the large table even in a different database, i.e.
SELECT *
FROM MyDb.dbo.SomeTable s
JOIN OtherDb.dbo.LargeTable l
ON (expression);
I'd like to create a database backup using SSMS.
The backup file will be a .bak file, but I would like to ignore 1 column in a certain table, because this column isn't necessary, but it takes up 95% of the backup size.
The column values should all be replaced by 0x00 (column type is varbinary(max), not null).
What's the best way to do this?
FYI: I know how to generate a regular backup using Tasks => Back Up..
There is a long way of doing what you ask. Its basically create a new restored database, remove the non required data and then do a new backup again.
Create a Backup of the production database.
Restore the backup locally on production with a new name
Update the column with 0x00
Shrink the database (Shrink is helpful when doing a restore. This wont reduce the bak file size)
Take the backup of the new database (Also use Backup Compression to reduce the size even more)
Ftp the bak file
If you only needed a few tables, you could have used bcp but that looks out of the picture for your current requirement.
From SQL Server native backups, you can't. You'd have to restore the database to some other location and then migrate usefull data.
You can create a copy of your table without the column and backup using filegroups https://msdn.microsoft.com/en-us/library/ms191539(SQL.90).aspx
I want to copy all data from one database to another which has the same structure. The databases reside on the same machine & on same sql server.
I have googled a bit & have found solutions like this
INSERT states (statecode, statename)
SELECT statecode, statename
FROM server1.database1.dbo.states
But the problem is they are copying table by table & I have like more then 100 tables. I was thinking that is there a way to copy all of the data at once.
Views & stored procedures all should be copied.
Or should I be looking in some other direction to achieve this ...?
If this is a one-time need, use the (Database) > Tasks > Generate Scripts menu option in SQL Server Management Studio.
Some options:
Use the DB back up and restore tools to just move a big backup file. This is the simplest option.
Slave the 2nd instance off of the 1st. It'll keep it up to date, but can be a pain.
Use import export wizard to transfer the data from one DB to another DB and use Generate script for the Transfer the Procedure and views.
Check out tools like Red-Gate SQL Compare (for structural comparison) and SQL Data Compare (for data content compare). With Data Compare, you can also easily update one database from another (or a database backup, even).
They're not free - but if you have to do this several times over and over, just the time (not to speak of the hassle) you save yourself will easily outweigh the cost of purchasing these tools. Excellent stuff - highly recommended!
I have two backup files
1) is named 'backup.sql' with a bunch of SQL defining TABLES
2) is named 'backup' with a bunch of encoded data, which I believe are the ROWS
I need to restore these TABLES + ROWS, but all I am able to figure out is how to restore the tables.
Any tips on dealing with these files? It's the first time I ever deal with SQL Server.
The backup process would not create a file with actual SQL statements, it would create a binary file. So #1 is not a backup file (it's probably a script someone saved to re-create the schema).
I would try to use SQL Server Management Studio to restore the second file and see what happens. I don't think it will allow you to restore an invalid file, but I would take some basic precautions like backing up the system first.
What is the extension for the 'backup' file? Is the filename backup.bak? If you have a backup file created by sql server then it 'should' contain the logic to create both the tables and restore the data, but it could depend on how the backup was created.
---Edit
It is possible for a .SQL file to contain data values as well as the logic to create the tables/columns for a database. I used to run backups of a MySql database in this way a long time ago...it just is not seen very often with SQL server since it has built in backup/restore funcationality.
Seems unlikely they would export all the rows from all tables into CSV file, and given you said it looks encrypted, it's making me think that's your actual backup file.
try this, save a copy of the "backup" file, rename it to backup.bak and run this from SQL Server Management Studio
restore filelistonly from disk='C:\backup.bak'
(assuming your file is saved on the root of the C: drive)
Any results/errors?