Author Archive

Two-character month and day in Expression Builder

I needed to set a variable to YYYYMMDD.

When using the Month() and Day() function in Expression Builder, it returned a single character back when the value was less than 10. So the value I was getting back was YYYYMD using the following expression:

(DT_WSTR, 4) YEAR( GETDATE()  ) + (DT_WSTR,2)DatePart("m", getdate()) + (DT_WSTR,2)DatePart("d", getdate())

The workaround is to use the RIGHT() function:

(DT_WSTR, 4) YEAR( GETDATE()  ) + RIGHT("0" + (DT_WSTR,2)DatePart("m", getdate()), 2) + RIGHT("0" + (DT_WSTR,2)DatePart("d", getdate()), 2)

The above will return the right format: YYYYMMDD.

Run SQL queries in a batch file

I recently had to delete records from a table regularly, every 10 minutes. I first obviously thought of setting up  a SQL Agent Job, easy!!!

Now here is my problem, I am using SQL Server Express Edition, which doesn’t come with SQL Server Agent features. So the SQL way wasn’t an option. I then realised that I could setup a scheduled task in Windows (XP) that would run a batch file every 10 minutes, job done!

Here is an example of the batch file:

@ECHO OFF
cd C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn
SQLCMD -SserverName\instanceName -Uusername -Ppassword -Q"delete from DatabaseName.dbo.TableToDelete"
@ECHO OFF

Save it as something like Query.bat and call it via a scheduled task.

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:

  1. Firstly, Database Mail; it should be enabled and configured. This has been covered in detail here.
  2. Secondly, an Operator that will be used by SQL Agent jobs to notify the relevant person or group.

Let’s create a new Operator:

Right click on Operators and click New Operator...

Right click on Operators and click New Operator...

In the next step, all we need is specify the email address of the person or group to receive notifications

Specify an email address

Specify an email address

Now that the Operator is ready,  any sql agent job can use it

Job property - notification tab

Job property - notification tab

Last but not least, let’s make sure that SQL Server Agent is able to use the Database Mail profile

SQL Server Agent properties

SQL Server Agent properties

Alert System - choose the mail profile to use for sending notifications

Alert System - choose the mail profile to use for sending notifications

Restart SQL Server Agent

Restart SQL Server Agent

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.

Right click on Database Mail and check Configure Database Mail...

Create a new profile called testProfile

Create testAccount SMTP account that will be associated with the testProfile

testAccount created

Public Profiles

Configure System Parameters

Configure System Parameters

Summary

Summary

Confirmation

Confirmation

Database Mail is now configured and ready for use. Let’s send a test mail to confirm that it is configured properly.

Send test email

Send test email

Send test email

Send test email

Email Sent

Check your inbox...

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.

SSIS 2008 Error – The Microsoft ACE.OLEDB.12.0 provider is not registered on the local machine

I recently upgraded one of my Data Import SSIS 2005 package to 2008. That package ran fine in VS2005. Visual Studio 2008 happily upgraded it to the latest version with the usual Provider upgrade warnings, Provider=SQLNCLI10.1; there was no particular mention of the above error.

The package contains a connection to a 2007 excel file, here is how it looks like:

package

package

So here is the error popup I got when I clicked the preview button, the Excel Connection Manager was failing to connect to the file:

Microsoft.ACE.OLEDB.12.0 is not registered

Microsoft.ACE.OLEDB.12.0 is not registered

Solution:

Download the latest “2007 Office System Driver: Data Connectivity Components”  from here and install it.

After the install, I could happily preview the data in the excel file.

Hope this helps.

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.

Error when activating Windows Server 2008

Having just installed Windows Server 2008 Standard from scratch on a brand new server, I was ready to activate Windows.

When installing Windows, I did provide the registration key when asked. But when going through the activation process, I got an error.

I then decided to change the product key and enter again the same key as I did during the registration process and that solved the problem.

Please find below the steps I went through:

Activate Windows 2008

Activate Windows 2008

Click on Activate Windows online now

Click on Activate Windows online now

Activating...

Activating...

Windows activation error

Windows activation error

ActivateWindows05

Click on change product key

Enter the product key again

Enter the product key again

Activating...

Activating...

Activation successful

Activation successful

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

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

Right click on the registered server and choose Properties

Check Allow remote connections box

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

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

Firewall main screen

Firewall main screen

Click on Inbound Rules

Click on Inbound Rules

Righ click on Inbound Rules and choose Add...

Righ click on Inbound Rules and choose Add...

Choose Port

Choose Port

Choose TCP and type the Port number

Choose TCP and type the Port number

Choose Allow the connection

Choose Allow the connection

Uncheck if further restrictions apply in your environment

Uncheck if further restrictions apply in your environment

Name it accordingly

Name it accordingly

That's it!

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

SQL Browser service - UDP Port 1434

Good luck!