Persist Some Data Between Database Backup and Restore - sql-server

I have a database in SQL Server 2008 (not R2)
A third party has the job of replacing the database regularly by restoring the data in the live environment from a .bak file made in the development environment. This leads to the destruction of any user generated data in that database. I am restricted in the live environment and cannot have two databases there.
One solution I am thinking about is to write a stored procedure that could save somehow the user generated data to some kind of local file and then once the development .bak is restored a second stored procedure could write this data back from the local file.
I'm familiar with using generate scripts that will generate a .sql file so maybe something similar to this, but it needs to be generated from a sql query that contains only the user generated data (these are specific rows of certain tables that are joined together - not the best design but it's what I have to work with).
Is it possible to generate a SQL script from a SQL query? Or is there some other kind of local file storage I could use. Something like a CSV file would be ok but I'm not aware of an easy way to automate restoring this. It will need to be restored with some very specific sql queries.

Related

Cleared SQL Server tables still retain some data

I made a custom application that is running from several years and is full of company data.
Now I need to replicate the application for another customer, so I set up a new server then i cloned the databases and empty all the tables.
Then I made a database and file shrink.
On the SQL Server side, the databases looks empty but if I run a grep search on the database files .mdf and .log I still can find recurrence of the previous company name also in system databases.
How do I really clean a SQL Server database?
Don't use backup/restore to clone a database for distribution to different clients. These commands copy data at the physical page/extent level, which may contain artifacts of deleted data, dropped objects, etc.
The best practice for this need is to create a new database with schema and system data from scratch using T-SQL scripts (ideally source controlled). If you don't already have these scripts, T-SQL scripts for schema/data can be generated from an existing database using the SMO API via .NET code or PowerShell. Here's the first answer I found with a search that uses the Microsoft.SqlServer.Management.SMO.Scripter class. Note you can include scripts data too (insert statements) by specifying the ScriptData scripting option for desired tables.

How to copy databases automatically from one server to another in efficient way (not backup and restore)

every once in a while I need to move a database from one server (QA) to another (production )
until now I used to perform backup on one server copy to the other server and then restore it...
is there a way to do it more efficiently?
maybe an automated job?
mirror? replication?
I'm searching for one-two clicks solution..
tnx.
I already made an automated script that does all of the following:
1)backup.
2)zip.
3)copy.
4)unzip.
5)restore.
Could you possible create an SSIS package (using the Transfer Database Task+ Transfer SQL Server Objects) that you can execute when you need it? You could create a 'template' package and use it for further use with just changing the connections.
See this blog post for some more details:
https://www.mssqltips.com/sqlservertip/2064/transfer-database-task-and-transfer-sql-server-objects-task-in-ssis/
From the link above:
Transfer Database Task: The Transfer Database Task is used to move a database to another SQL Server instance or create a copy on the same
instance (with different database name). This task works in two modes
[...]:
Offline : In this mode, the source database is detached from the source server after putting it in single user mode, copies of the mdf,
ndf and ldf files are moved to specified network location [...]
Online : In this mode, the task uses SMO to transfer the database objects to the destination server. In this mode, the database is
online during the copy and move operation, but it will take longer as
it has to copy each object from the database individually [...].
Transfer SQL Server Objects Task The Transfer SQL Sever Objects task is used to transfer one or more SQL Server objects to a different
database, either on the same or another SQL Server instance. This
allows you to select different types of objects you want to transfer.
You can select tables, views, stored procedures, user defined
functions etc. [...]
Not sure what it would take to make this package transferable among the different databases you may need to copy, but maybe it's worth exploring.
Good luck! :)

how to backup of specific tables from database sql server and save .bak file in computer using vb.net code

I want to backup some specific data of SQL database by using vb.net code. In fact i want to save this file as .BAK file and then i want to restore it.
I couldn't find anything. all the solutions in the web was about full backup of database and there was not any method to backup some tables via code.
SQL Server does not permit backing up a single table. Full stop. No provision is made for this whatsoever.
It does permit restoring a single table from a complete backup, but this does not seem to be what you want.

How to add data in database from .bak file by sql query

I have backup files. I want to add that data to the existing database not over write it.
Scenario:
The software is installed on different systems. The data is entered from different PC's. but the data is of same type. After taking the backup(.bak)file on run-time. Now i want to combine that all data in a single database not overwrite it. I have tried How to restore to a different database in sql server? I am out of that option and after so much googling i cant get answer.Just found to overwrite the database but i dont want to loose any of the data. Thankz

Backup remote SQL Server database to local

I'm trying to backup a live database to my computer and I can't find the option to do it. I'm connecting to it using Microsoft SQL Server Management Studio 2008 R2. I'm a MySQL monkey, so I'm used to being able to backup to .sql files and move them around.
Anyone have any idea how I can create a file backup of the database? I've found the backup option which only backs up on the server, or the export, which seems to only allow a single table, or code an SQL query, which I'm not too sure on, short of putting in something like SHOW TABLES;
Anyone have any ideas? I'm limited to readonly access for various reasons, nothing bad, promise!
You will only be able to backup the database to a location the service account for SQL has access to. If you have access to a central share on the server/network that you can access and the service can, you might backup to that location and then browse from your computer to pull it down.
If you are just wanting the database structure you could script the database out to a file. This would let you save it locally. If you also want the data though doing a full backup is the quickest way I know of.
EDIT
I would use the T-SQL BACKUP comand and include WITH COPY_ONLY to backup the database, since you stated this is a "live" database. If a scheduled job is performing backups against the database and you break in to do an additional one you will effect the backup recovery chain of the database. Using the COPY_ONLY will allow you to get a backup of the database without requiring it in the event of a recovery need.
You can also create sql dumps with Management Studio.
Right-click the database and select Tasks - Generate Scripts. This will open a wizard that allows you to select what the dump should include (e.g. tables, indices, views, ...).
Make sure you set "Script Data" to true if you want your dump to include inserts.
You can enter a valid UNC path in the Backup option.

Resources