restoring sql server databases - SQL server 2000 to 2005 express

Monday, June 2nd, 2008

This should be an easy procedure but its not.

Something I found really useful was this:

RESTORE HEADERONLY FROM DISK = ‘c:\xxx.bak’
RESTORE FILELISTONLY FROM DISK = ‘c:\xxx.bak’

Basically from the SQL above you can find out the correct names of the xxx_Data and xxx_Log files for the next command:

RESTORE DATABASE newXXXX
FROM DISK = ‘c:\xxx.bak’
WITH REPLACE,
MOVE ‘xxxx_Data’ TO ‘C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\xxxx.mdf’
, MOVE ‘xxxx_Log’ TO ‘C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\xxxxx_log.ldf’

Currently I have the database ‘(Restoring…)’ in SQL Server 2005 Express but I cannot seem to get them out of their ‘Restoring’ process…..

These were useful:

http://www.mssqltips.com/tip.asp?tip=1150

http://doc.ddart.net/mssql/sql70/ra-rz_11.htm
Still got it ‘(Restoring…)’ but this may help someone:

Restore VerifyOnly From Disk=’c:xxxx.bak’

How to work out dates sql server 2005 / transact SQL

Monday, May 12th, 2008

http://msdn.microsoft.com/en-us/library/ms186724.aspx

Its really easy to do - best to do all of this on the database - that’s what it is for!

This finds stuff that is from 6 months ago until today:

WHERE yourDate BETWEEN DATEADD(MONTH, -6, CURRENT_TIMESTAMP) AND CURRENT_TIMESTAMP

Automatically backing up an SQL server database and sending it to a new location

Wednesday, May 7th, 2008

http://www.codeproject.com/KB/database/BackupUtility.aspx

SQL Server 2005 express maintenance

Wednesday, January 9th, 2008

http://www.microsoft.com/technet/prodtechnol/sql/2005/mgsqlexpwssmse.mspx

Scheduling Backups for SQL Server 2005 Express

Saturday, October 13th, 2007

http://www.mssqltips.com/tip.asp?tip=1174

Problem
One problem with SQL Server 2005 Express is that it does not offer a way to schedule jobs. In a previous tip, Free Job Scheduling Tool for SQL Server Express and MSDE, we looked at a free tool that allows you to create scheduled jobs for SQL Server. The one issue people often face though is what to install and what not to install on their production servers and therefore these items go without resolution. One very important part of managing SQL Server is to ensure you run backups on a set schedule. I often hear about corrupt databases and no backups, so let’s take a look at another approach of scheduling backups using the included tools in both the operating system and SQL Server.

Solution
There are two components to this; the first is the backup command and the second is the scheduling needed to run the backups.

Backup Commands
There are a few things that we need to setup. The first is to create a stored procedure that allows us to dynamically generate the backup file name as well as what type of backup to run Full, Differential or Transaction Log backup. The default for this stored procedure is to create the backups in the “C:\Backup” folder. This can be changed to any folder you like.

The following stored procedure should be created in the master database. This is just one way of handling this. There are several other options and enhancements that can be made.

USEÂ [master]
GO
/****** Object:  StoredProcedure [dbo].[sp_BackupDatabase]    Script Date: 02/07/2007 11:40:47 ******/
SETÂ ANSI_NULLSÂ ON
GO
SETÂ QUOTED_IDENTIFIERÂ ON
GO

–Â =============================================
– Author: Edgewood Solutions
– Create date: 2007-02-07
– Description: Backup Database
–Â Parameter1:Â databaseName
– Parameter2: backupType F=full, D=differential, L=log
–Â =============================================
CREATEÂ PROCEDUREÂ [dbo].[sp_BackupDatabase]
@databaseName sysname@backupType CHAR(1)
AS
BEGIN
SETÂ
NOCOUNTÂ ON;

DECLARE @sqlCommand NVARCHAR(1000)
DECLARE @dateTime NVARCHAR(20)

SELECT @dateTime REPLACE(CONVERT(VARCHARGETDATE(),111),‘/’,) +
REPLACE(CONVERT(VARCHARGETDATE(),108),‘:’,)

IF @backupType ‘F’
SET @sqlCommand ‘BACKUP DATABASE ‘ @databaseName +
‘ TO DISK = ”C:\Backup\’ @databaseName ‘_Full_’ @dateTime ‘.BAK”’

IF @backupType ‘D’
SET @sqlCommand ‘BACKUP DATABASE ‘ @databaseName +
‘ TO DISK = ”C:\Backup\’ @databaseName ‘_Diff_’ @dateTime ‘.BAK” WITH DIFFERENTIAL’

IF @backupType ‘L’
SET @sqlCommand ‘BACKUP LOG ‘ @databaseName +
‘ TO DISK = ”C:\Backup\’ @databaseName ‘_Log_’ @dateTime ‘.TRN”’

EXECUTE sp_executesql @sqlCommand
END

The second part of this is to create a SQLCMD file to run the backup commands. Here is a simple SQLCMD file that backups databases master, model and msdb.

This file gets saved as backup.sql and for our purposes this is created in the “C:\Backup” folder, but again this could be put anywhere.

sp_BackupDatabase ‘master’, ‘F’
GO
sp_BackupDatabase ‘model’, ‘F’
GO
sp_BackupDatabase ‘msdb’, ‘F’
GO
QUIT


Scheduling
Included with the Windows operating system is a the ability to setup and run scheduled tasks. This is generally not used for SQL Server environments, because SQL Server Agent is so robust and gives you a lot more control and options for setting up re-occurring jobs. With SQL Server 2005 Express the only choice is to set a scheduled task at the operating system level or look for some third party tool.

To setup a scheduled task you need to open the folder where you can create a new scheduled task. This can be found under Accessories -> System Tools -> Scheduled Tasks or under Control Panel.

The first thing to do is to click on “Add Scheduled Task” and the following wizard will run.

Select the application that you want to run. For our purposes we will be using SQLCMD.EXE. In order to find SQLCMD.EXE you will need to click on the Browse… button.

You should be able to find this in the following directory “C:\Program Files\Microsoft SQL Server\90\Tools\Binn”.

Give the scheduled task a name and specify when to perform the task.

Specify the time that this should be run.

Provide the credentials for the account that will run this task.

Finish and save the task. One thing you want to do is click on the “Open advanced properties” so you can edit the command.

Below is the advanced properties screen. You will need to change the “Run” command to the following:

sqlcmd -S serverName -E -i C:\Backup\Backup.sql

This is broken down as follows:

  • sqlcmd
  • -S (this specifies the server\instance name for SQL Server)
  • serverName (this is the server\instance name for SQL Server)
  • -E (this allows you to make a trusted connection)
  • -i (this specifies the input command file)
  • C:\Backup\Backup.sql (this is the file that we created above with the command steps)

That should do it. The scheduled task should now be setup.

If you want to run the command now to make sure it works go back to the Scheduled Tasks view and right click on the task and select “Run”.

Next Steps

  • Although this is a pretty simple example this should allow you to backup your SQL Server databases pretty easily
  • Modify the process to handle errors and also to take other parameters
  • Also take a look at this tip,Free Job Scheduling Tool for SQL Server Express and MSDE, to see if this tool makes more sense for your environment

Freeing up space in SQL Server 2005

Sunday, September 30th, 2007

I. Â Shrink the log file size at the right time

I found out this trick:

 Immediately after I use the SSIS package or Import the data to the database ( highlight the database->Tasks->Import data … ), or Export the data from the database ( highlight the database->Tasks->Export data … ),  I can shrink the log file to the desired size, for example, 1MB.  That is, highlight the database->Tasks->Shrink->Files

 set the file size, say, 1MB.

Then, click OK and you are done.

Â

Â

II. Eliminate the log file completely

Sometimes we just do not need the big log file. For example, I have 40GB log file. IÂ am sure I do not need this log file and want to get rid of it completely to free up the hard drive space. The logic is

a. Detach the database

b. Rename the log file

c. Attach the database without the log file

d. Delete the log file

Â

Let’s say, the database name is testDev. In the SQL Server Management Studio,

  1. Highlight the database-> Tasks->Detach..-> Click OK
  2. Go to log file folder -> rename the testDev_log.ldf to be like testDev_log-aa.ldf,
  3. Highlight Databases->Attach…-> Click Add -> add the database testDev, highlight the log file and click the ‘Remove’ button. This means you only attach testDev.mdf
  4. After this is done, you can verify the contents of the attached database and then delete the log file.

Â

This way we can safely delete the log file and free up the space.

http://www.codeproject.com/useritems/truncate_log_SQL_server.asp?df=100&forumid=315417&exp=0&select=1655485

Restoring a database SQL Server 2005

Wednesday, August 8th, 2007

restore database [DATABASE_NAME] from disk = ‘C:\wherever\your_backup.bak’ with replace

http://weblogs.asp.net/plip/archive/2006/10/26/SQL-Server-2005-Backups.aspx

Backing up databases SQL Server 2005

Sunday, July 22nd, 2007

Had a problem with a saving to a different location, this was useful:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=733113&SiteID=1

Running a store procedure in query analyzer

Wednesday, June 20th, 2007

Running a store procedure in query analyzer:
declare @success int
exec sptriggerexport @retval = @success out
print @success

Selecting data from different databases in SQL queries

Friday, June 1st, 2007

realy easy:

select * from forum.dbo.tblauthor

select * from [database-name].[owner].[table]

Also useful to point out that if you are selecting from a database with - or other characters in the name then just use [:

select * from [rails-07-2].dbo.tblauthor

EASY!