Tuesday, February 18, 2014

Generate insert scripts with data

There have been instances where we need to create insert statements for a table and share it across different dev /test environments.
We can do this pretty easily using SSMS. (Below screen shots are from SQL 2012)

First, right click on database from where you want to generate insert scripts with data -> Tasks -> Generate Scripts..

Next, Generate and Publish scripts wizard opens,
Click next on introduction page,

On Choose object page, select the required database object you want to script and then click Next.

On Set scripting options page, click on Advanced button as show below

On General listings, select drop down for "Types of data to script" and select "Schema and data" and then click OK.

then select the option whether to save it as file, or open in clipboard or to save it in new query window.
This way you can share insert scripts with data to your dev/test team.

Hope this helps !

Monday, February 17, 2014

Restoring DB from network path /remote server using SQL Server

If you are reading this, then you might have already restored database in local server instance. There are scenario's where we need to restore database from a network path. So the question is how to do this? let's find it out .

Scenario:
Consider we have 2 servers "ServerA" and "ServerB". We have to restore database from ServerA to ServerB without copying the .bak file over the network (probably copying is waste of time).

Solution:
Follow the below steps,
  1. Create a folder in ServerA (let's name it as Backups). Share the folder and grant "Everyone" full control permission on the folder. So now the network path of this folder will be "\\ServerA\Backups".
  2. Take the backup of database you want to restore, into the folder created above (i.e. Backups).
  3. Log into ServerB -> Open MyComputer -> click on Map network drive as show below
  4. Once clicked you will find the below screen
  5. Select drive name of your choice and mention the network path. In our case Drive = S: and network path ="\\ServerA\Backups", click finish, Once this is done you should be able to see this drive in My Computer explorer.
  6. Now that we have mapped the network drive in explorer, we also need to register the same in SSMS.
  7. Open SSMS in ServerB(preferably as administrator), connect to db local instance and run the below scripts. (Make changes based on your scenario for drive and network path).
  8. Once this is done, right click on databases-> restore database->Source as Device->  Browse -> Backup media type as File -> Add-> Now you should be able to see the network drive. Select the database you want to restore. 
Hope this helps :-)