Tuesday, April 24, 2012

How to script the SQL Server Agent Operators?


As part of the Disaster recovery procedures, I wanted to script out every server object that we had created. This included SQL Server jobs, logins, operators, linked servers and proxies. I was able to script out everything but the SQL Server Agent operators.

After several unsuccessfull Google searches my colleague found the following script to do this.

USE msdb
set nocount on;

create table #tbl (
id int not null,
name sysname not null,
enabled tinyint not null,
email_address nvarchar(100) null,
last_email_date int not null,
last_email_time int not null,
pager_address nvarchar(100) null,
last_pager_date int not null,
last_pager_time int not null,
weekday_pager_start_time int not null,
weekday_pager_end_time int not null,
Saturday_pager_start_time int not null,
Saturday_pager_end_time int not null,
Sunday_pager_start_time int not null,
Sunday_pager_end_time int not null,
pager_days tinyint not null,
netsend_address nvarchar(100) null,
last_netsend_date int not null,
last_netsend_time int not null,
category_name sysname null);

insert into #tbl
  EXEC sp_help_operator;

select 'USE msdb;' + char(13) + char(10) + 'if exists (select * from dbo.sysoperators where name =' + quotename(name, char(39)) + ') ' + char(13) + char(10) +
'exec sp_add_operator ' +
'@name = ' + quotename(name, char(39)) + ', ' +
'@enabled = ' + cast (enabled as char(1)) + ', ' +
'@email_address = ' + quotename(email_address, char(39)) + ', ' +
case
when pager_address is not null then '@pager_address = ' + quotename(pager_address, char(39)) + ', '
else ''
end +
'@weekday_pager_start_time = ' + ltrim(str(weekday_pager_start_time)) + ', ' +
'@weekday_pager_end_time = ' + ltrim(str(weekday_pager_end_time)) + ', ' +
'@Saturday_pager_start_time = ' + ltrim(str(Saturday_pager_start_time)) + ', ' +
'@Saturday_pager_end_time = ' + ltrim(str(Saturday_pager_end_time)) + ', ' +
'@Sunday_pager_start_time = ' + ltrim(str(Sunday_pager_start_time)) + ', ' +
'@Sunday_pager_end_time = ' + ltrim(str(Sunday_pager_end_time)) + ', ' +
'@pager_days = ' + cast(pager_days as varchar(3)) + 
case
when netsend_address is not null then ', @netsend_address = ' + quotename(netsend_address, char(39))
else ''
end +
case
when category_name != '[Uncategorized]' then ', @category_name = ' + category_name 
else ''
end +
'; ' + char(13) + char(10) + 'go'
from #tbl order by id;

drop table #tbl;

Mixed Mode authentication but did not provide a strong password.

Upgrade SQL Server 2000 to SQL Server 2005

Today I was upgrading a SQL Server 2000 instance to 2005 and was getting the following error during the install.

You selected Mixed Mode authentication, but did not provide a strong password.


Even after changing the password for SA and using a strong password we kept getting this error. After a couple of attempts it became apparent that the problem was not with the password.

Then came across the following link that explained the problem and the work around.
http://blogs.msdn.com/b/sqlserverfaq/archive/2009/07/15/upgrade-advisor-returns-error-while-upgrading-an-instance-of-sql-server-2000-to-sql-server-2005.aspx

In short the solution is to copy the BPAClient.dll file from the following location 'C:\Program Files\Microsoft SQL Server\90\Setup Bootstrap\BPA\bin' to 'C:\Program Files\Microsoft SQL Server\90\Setup Bootstrap\BPA'

After that the upgrade went without any hiccups. Once again thank you to Google.

If you do not find the file under the 'Program Files' folder then search under 'Program Files (x86)' folder.

Problem Event Name: CLR20r3

Install SQL Server 2008 R2 Enterprise Edition

Recently, I started installing SQL Server 2008 R2 Enterprise Edition and came across an exception. The install would fail and when I clicked on the report I saw the following message.

Problem signature:
  Problem Event Name:    CLR20r3
  Problem Signature 01:    setupvm.exe
  Problem Signature 02:    1.0.523.0
  Problem Signature 03:    4a7a0bdd
  Problem Signature 04:    mscorlib
  Problem Signature 05:    2.0.0.0
  Problem Signature 06:    4a27471d
  Problem Signature 07:    349e
  Problem Signature 08:    9a
  Problem Signature 09:    System.IO.IOException
  OS Version:    6.1.7600.2.0.0.272.7
  Locale ID:    2057

After searching on the net I found several cases where the install was failing for the same version. But after going through several issues, it became apparent that the issue was the install media.

So created a brand new ISO file for the installation media and used that to install SQL Server and that seemed to work.

Sometimes it is easier to begin from scratch than to keep troubleshooting these issues.

Monday, April 16, 2012

Run queries against different schema


Often times I run multiple queries in a TSQL script in a new query window. But some or all the objects might belong to a schema other than 'dbo'. This means we have to explicitly mention the schema name like
$schemaname.$tablename

But it might be difficult to edit the TSQL to mention the schemaname explicitly. As a DBA who comes from a different RDBMS background, I am used to commands such as

SET CURRENT SCHEMA ABC
or
USE SCHEMA ABC
or
ALTER SESSION SET CURRENT_SCHEMA='schemaname'


So I have always thought of a command or a query option to set the schema for all subsequent commands.

If you run the commands without the correct schema name then you will get the following error.

Msg 208, Level 16, State 1, Line 1
Invalid object name 'tablename'.

In order to run all subsequent queries against a different schema use the following command

EXECUTE AS USER = 'SCHEMANAME'