Common SQL Tasks I Do and Scripts

Common SQL Tasks I Do and Scripts 

Restore a database from Backup :

RESTORE FILELISTONLY
 FROM DISK = '\\FileShare\Database\Database.bak'
RESTORE DATABASE SmarterTrack
 FROM DISK = '\\FileShare\Database\Database.bak'
 WITH REPLACE
 , MOVE 'SmarterTrackModel' TO 'G:\SQLServer\MSSQL10_50.DEV2008R2\MSSQL\DATA\SmarterTrack.mdf'
 , MOVE 'SmarterTrackModel_log' TO 'G:\SQLServer\MSSQL10_50.DEV2008R2\MSSQL\DATA\SmarterTrack_log.ldf'
EXEC sp_helpdb 'SmarterTrack'

Run Multiple SQL Files using Management Studio :
I find this little used option very handy.Enable SQLCMD Mode in Management studio.

:setvar path "C:\Dev\svn\database\Projects\NewMexico\DBSetup"
:r $(path)\01_ObjectsSetup.sql
:r $(path)\02_DomainDataLoad.sql
:r $(path)\03_TenantMandatoryData.sql
:r $(path)\04_NMScienceStandards.sql
:r $(path)\05_DistrictOrganizationLoad.sql
:r $(path)\06_SchoolOrganizationLoad.sql
:r $(path)\07_DemoOrganizationLoad.sql

How do I read SQL Server ErrorLog :

EXEC sys.xp_readerrorlog

How do I switch Primary / Secondary on Switch Mirroring Databases ( Swap Primary / Secondary ):
When the automatic failover is setup , the Primary / Secondary gets swapped during maintenance tasks.
To swap back the Primary/Secondary , connect to the server which’s primary and run the following command.

USE master
GO
ALTER DATABASE   SET PARTNER FAILOVER;
GO

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *