Pages

Ads 468x60px

Social Icons

Featured Posts

Search

Simplest way to restore MSSQL Server database

Friday, 13 April 2012

Here's a simplest way I found (so far) to restore a database in MS SQL Server. First part lists "logicalnames" from the backup that you want to restore. Modify the path so it points to the actual path where your backup file is:
RESTORE FILELISTONLY FROM DISK = N'C:\[path]\[db_name_backup].bak'
 GO

This shows LogicalName and PhysicalName of the data and log backup files, which have to be used in the following script:
RESTORE DATABASE [db_name]
 FROM DISK = N'C:\[path]\[db_name_backup].bak'
 WITH FILE = 1,
 MOVE N'[data_file_logical_name]' TO N'C:\[restored_db_path]\[data_file_name].mdf',
 MOVE N'[log_file_logical_name]' TO N'C:\[restored_db_path]\[log_file_name].ldf',
 NOUNLOAD,
 STATS = 10
 GO

If you like myself do not like clicking through SQL Server Management Studio GUI, this script can help you automate database restore process.

How to delete all stored procedures from MSSQL database using cursor

Tuesday, 24 January 2012

Here is a code for deleting all stored procedures in SQL Server database using cursor.
DECLARE @name varchar(500)
DECLARE @sql varchar(max)
SET @sql = ''

DECLARE cur CURSOR
FOR SELECT [name] FROM sys.procedures
OPEN cur

FETCH NEXT FROM cur INTO @name
WHILE @@fetch_status = 0
BEGIN
SET @sql = 'DROP PROC ' + @name
PRINT @sql
EXEC (@sql)   
FETCH NEXT FROM cur INTO @name
END
CLOSE cur
DEALLOCATE cur
 

Most Popular