Wednesday, December 9, 2015

Rename database

ALTER DATABASE AssetMgmtServices SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO

ALTER DATABASE AssetMgmtServices
MODIFY NAME = AssetMgmtServices_Nov2012
GO

ALTER DATABASE AssetMgmtServices_Nov2012
SET MULTI_USER
GO

Renaming databse mdf and ldf file

USE AssetMgmtServices_May2012

-- Replace all MyDBs with the name of the DB you want to change its name
USE AssetMgmtServices_May2012;

-- Changing Physical names and paths
-- Replace all NewMyDB with the new name you want to set for the DB
-- Replace 'C:\...\NewMyDB.mdf' with full path of new DB file to be used
ALTER DATABASE AssetMgmtServices_May2012 MODIFY FILE (NAME = 'AssetMgmtServices', FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER2012\MSSQL\DATA\AssetMgmtServices_May2012.mdf');


-- Replace 'C:\...\NewMyDB_log.ldf' with full path of new DB log file to be used
ALTER DATABASE AssetMgmtServices_May2012 MODIFY FILE (NAME = 'AssetMgmtServices_log', FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER2012\MSSQL\DATA\AssetMgmtServices_May2012_log.ldf');


-- Changing logical names
ALTER DATABASE AssetMgmtServices_May2012 MODIFY FILE (NAME = 'AssetMgmtServices', NEWNAME = 'AssetMgmtServices_May2012');
ALTER DATABASE AssetMgmtServices_May2012 MODIFY FILE (NAME = 'AssetMgmtServices_log', NEWNAME = 'AssetMgmtServices_May2012_log');


ALTER DATABASE AssetMgmtServices_May2012 SET OFFLINE WITH
ROLLBACK IMMEDIATE

--Physically change the file name

ALTER DATABASE AssetMgmtServices_May2012 SET ONLINE

Tuesday, December 8, 2015

BCP bulk export and import

To export data

declare @sql varchar(8000)select @sql = 'bcp [Database_name].[schema_name].[Table_name] out c:\Table_name.txt -c -t"|^" -T'

exec master..xp_cmdshell @sql



To import data

declare @sql varchar(8000)select @sql = 'bcp [Database_name].[schema_name].[Table_name] int c:\Table_name.txt -c -t"|^" -T'

exec master..xp_cmdshell @sql