Archive for the ‘SQL Server 2008’ Category
Setting up email notifications in SQL Server 2005/2008
A business critical SQL Server job failed during the weekend without anyone knowing about it. When some errors are not predictable, a good system should at least send notifications when things go wrong. In this case, it would have been appropriate if an email was sent to the relevant person with as much details possible about the error.
In SQL Server, we need to setup the following two features:
- Firstly, Database Mail; it should be enabled and configured. This has been covered in detail here.
- Secondly, an Operator that will be used by SQL Agent jobs to notify the relevant person or group.
Let’s create a new Operator:
In the next step, all we need is specify the email address of the person or group to receive notifications
Now that the Operator is ready, any sql agent job can use it
Last but not least, let’s make sure that SQL Server Agent is able to use the Database Mail profile
That’s it.
Setting Database Mail on SQL Server 2005/2008
I was creating a web service that processes data and saves into relevant tables. Should there be any error in the process, an error record was created. Although there are many ways to be alerted when an error occurred, I felt that in this case it was more appropriate to use a Stored Procedure to send an error email.
In SQL Server 2005/2008, the Database Mail feature need to be setup. The Database Mail feature allows SQL Server to send emails to end users. The only pre-requisite is to have an SMTP server available.
Database Mail is now configured and ready for use. Let’s send a test mail to confirm that it is configured properly.
Now that we have Database Mail working, we can use the following syntax to send email:
EXEC [msdb]..[sp_send_dbmail] @profile_name = 'testProfile', @recipients = 'To@email.com', @subject = 'Error Message', @body = 'Include error message', @body_format = 'HTML'
Set Database Compatibility in SQL Server
I have recently upgraded SQL servers from 2000 to 2008. SQL server doesn’t automatically upgrade the databases to the latest version ie 100.
So I had to change the version of each database manually using the following script:
ALTER DATABASE database_name
SET COMPATIBILITY_LEVEL = { 80 | 90 | 100 }
80 = SQL Server 2000
90 = SQL Server 2005
100 = SQL Server 2008
To update all databases at once within the SQL instance, the following script might be useful.
select 'ALTER DATABASE ' + name + ' SET COMPATIBILITY_LEVEL = 100'
from sys.databases
where name not in ('master','tempdb','model','msdb')
That’s it.
Happy New Year!
Best wishes and lots of success for 2010!
Cheers.
SQL Server Login Transfer – Password not required
While working on sql server consolidation, I was required to transfer all logins to the new sql server instance. I was worried about not knowing the passwords for a few Windows domain logins. I then realized that passwords are not required when transfering them.
All one needs to do is run the scripts available at the following link:
http://support.microsoft.com/kb/918992
The script will generate the script for creating all logins under the sql instance. For example:
– Login: Test
CREATE LOGIN [Test_Login] WITH PASSWORD = 0x01006B867DAEEEBC1207A2232FA7B155A7F7670543341FFF012F HASHED, SID = 0x0393B11B9324C04F930A7F951F05EFFF, DEFAULT_DATABASE = [master], CHECK_POLICY = OFF, CHECK_EXPIRATION = OFF
As you can see in the above example, the password is included albeit encrypted.
The good thing is that you are not required to know the password. The bad thing is that you will end up with a new login having a different sid, therefore not matching with the user of the same name in the database. In that case, all you need to do is to run the following script (courtesy of sqlservercentral.com) to find and remap them:
/**************************ORPHANED USERS****************/
/*** To be run against each db ***/
DECLARE @UserName nvarchar(255)
DECLARE Cursor_OrphanedUser cursor for
SELECT NAME FROM sysusers
WHERE issqluser = 1 and (sid is not null and sid <> 0×01)
and suser_sname(sid) is NOT null ORDER BY name
OPEN Cursor_OrphanedUser
FETCH NEXT FROM Cursor_OrphanedUser INTO @UserName
WHILE (@@fetch_status = 0)
BEGIN
PRINT @UserName + ‘ Synchronization of Logins in Progress’
EXEC sp_change_users_login ‘Update_one’, @UserName, @UserName
FETCH NEXT FROM Cursor_OrphanedUser INTO @UserName
END
CLOSE Cursor_OrphanedUser
DEALLOCATE Cursor_OrphanedUser
/************************END ORPHANED USERS********************/
Hope you will find this useful.
Enabling remote access to sql server 2008 on a windows 2008 server
I recently installed SQL Server 2008 on a brand new server (HP Proliant GL360 G6) with Windows Server 2008 Standard on it . It all went fine and I could start Sql Server Studion Management tool right after the installation.
It is only when I was trying to access that server remotely that I got into problems! It just couldn’t connect to the sql server and was getting the folowing error:

SQL Server 2008 Remote Access error
I tried accessing via IP address as well but that wasn’t successfull either. A look into the firewall was then in order. By default in Windows 2008 Server every ports, except a very few ones, are disabled. If you are using the default port for your sql engine, the port number 1433 needs to be enabled. I have also enabled port number 1434 for SQL Browser service. The following screen shots shows the step needed to allow remote access to sql server 2008 on a windows 2008 server.
First of all, you need to ensure that Remote Connections are allowed within SQL Server configuration:

Right click on the registered server and choose Properties

Check Allow remote connections box
Once this is done, you can start looking into the Firewall setting.

This is how to access Windows Firewall setting on Windows Server 2008

Firewall main screen

Click on Inbound Rules

Righ click on Inbound Rules and choose Add...

Choose Port

Choose TCP and type the Port number

Choose Allow the connection

Uncheck if further restrictions apply in your environment

Name it accordingly

That's it!
Now, if you also want to set the SQL Browser service port, you just need to choose UDP instead of TCP.

SQL Browser service - UDP Port 1434
Good luck!


















