Home > SQL Server Tips > Database Administrator > Track changes to SQL Server 2000 and 2005 with one simple utility
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE ADMINISTRATOR

Track changes to SQL Server 2000 and 2005 with one simple utility


Michelle Gutzait
04.02.2008
Rating: -4.56- (out of 5)


Expert advice on database administration
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


The problem:
I am the systems administrator and SQL Server DBA in my organization. We're often required to immediately add databases, logins or jobs to the different SQL Server environments, especially in the development and testing environments. Since I can't allow myself to become a bottleneck for such changes, I had to delegate permissions for these SQL Server changes to the project managers. That said, I'd like to be notified regarding these changes. How can I achieve this goal without spending time on designing and developing a specific application? The problem is, I have both SQL Server 2000 and 2005 all over the place. I need something that will work for both environments.

The solution:
There are a few options to achieve my goal in SQL Server 2005, such as DDL triggers and event notifications. But SQL Server 2000 is more limited. To achieve my goal in both environments, I had to program a small utility that could run once a day or a few times a day, monitor changes in the respective system tables and send a message if changes are, in fact, detected. In order for that to work, SQL Mail (SQL 2000) or Database Mail (SQL 2005) must be configured already. I created a sp_send_dbmail stored procedure on my SQL 2000 instances that simply executes the xp_smtp_sendmail stored procedure.

Here are the example scripts I use in order to receive notification when new objects are created in my SQL Server instance. You can use these same scripts for modifications and deletions of the corresponding server objects:

  1. New job:

    A trigger can be created on the sysjobs table in the msdb database, so you don't need a daily job for that matter:

    new job
    (Click on image to download script)

    Tips on monitoring SQL Server:
  2. Monitoring server disk space in SQL Server
  3. Stored procedure to monitor long-running jobs in SQL 2000
  4. Screencast: SQL Server PerfMon counters for Windows OS
  5. Note: Triggers on system tables, such as sysjobs, are not always recommended since there is a danger that when the SQL Server version is upgraded, these triggers will be dropped or will stop functioning. Be sure to document these triggers and keep their source code for your successors and team members.

    For the other system tables in the master database (such as syslogins and sysdatabases), it's impossible to create triggers, so you should take another approach.

  6. New login:

    I create a table in tempdb holding the login names from the last time the job was executed. Every time it executes, I compare the contents of this table with syslogins and send a message with the new logins, if there are any. If the content of the table changed, I update the one in tempdb accordingly. It is better to create the table in a DBA database instead of in tempdb -- this way the table will not be dropped when SQL Server service is restarted for any reason. The following script should be executed every period of time (such as once a day):

    new login
    (Click on image to download script)

  7. New database:

    I do the same as I did with the new logins:

    new database
    (Click on image to download script)

A more realistic solution:
You can apply the sample code – except for the trigger that will execute anyway – when a new job is added from a central location by executing it as remote stored procedures.

For example, create the following on server_A, server_B and server_C:

executing as remote stored procedure
(Click on image to download script)

And run the following from server D (which is a central server) using Linked Servers:

exec Server_A.master.dbo.spDBA_NewDatabaseNotification
exec Server_B.master.dbo.spDBA_NewDatabaseNotification
exec Server_C.master.dbo.spDBA_NewDatabaseNotification
exec Server_D.master.dbo.spDBA_NewDatabaseNotification

Conclusion:
A simple way to receive notification about changes in your SQL Server objects, such as logins, databases and jobs, when you have both SQL Server 2000 and SQL Server 2005 is to monitor the delta of the system tables that hold the object's information or have a trigger on the object's table when it's possible.

ABOUT THE AUTHOR:   
Michelle Gutzait works as a principal database consultant for Victrix, a Montreal-based IT consulting firm specializing in information security, infrastructure, communication networks and applicative solutions and collaboration. Gutzait has been involved in IT for 20 years as a developer, business analyst and database consultant. For the past 10 years, she has worked exclusively with SQL Server, including SQL infrastructure and database design, performance tuning, security, high availability, disaster recovery, VLDBs, replication, T-SQL coding, DTS/SSIS packages, administrative and infrastructure tools development and reporting services.
Copyright 2008 TechTarget


Rate this Tip
To rate tips, you must be a member of SearchSQLServer.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
SQL Server stored procedures
SQL Server Service Broker Tutorial and Reference Guide
SQL Server trigger vs. stored procedure to receive data notification
How to use SQL Server 2008 hierarchyid data type
SQL Server stored procedures tutorial: Write, tune and get examples
Check SQL Server database and log file size with this stored procedure
SQL Server source code analysis and management adds database security
Configure SQL Server Service Broker for sending stored procedure data
Find size of SQL Server tables and other objects with stored procedure
Troubleshoot SQL Server 2005 temporary table performance problems
Use SQL Profiler to find long running stored procedures and commands

SQL Server overview
The sqlcmd utility in SQL Server
Top 10 SQL Server Tips of 2008
Tutorial: Performance tuning SQL Server via queries, indexes and more
Tutorial: SQL Server indexing tips to improve performance
How to create a SQL inner join and outer join: Basics to get started
SQL Server Service Broker Tutorial and Reference Guide
How to configure storage in SQL Server DB with more writes than reads
Avoid cursors in SQL Server with these methods to loop over records
Solve SQL Server errors and more from the DBA trenches -- part 2
Tutorial: Learn SQL Server basics from A-Z
SQL Server overview Research

Database Administrator
SQL Server Management Studio 2008: New features, part 1
How to create a SQL inner join and outer join: Basics to get started
SQL Server virtualization pros and cons: Weigh the performance impact
How to configure storage in SQL Server DB with more writes than reads
Solve SQL Server errors and more from the DBA trenches -- part 2
SQL Server database design disasters: How it all starts
SQL Server database design disasters: What not to do
How to create a SQL Server linked server to DB2
Virtual database storage for SQL Server: Friend or foe?
How to restore SQL Server database to transition server during upgrade

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
library  (SearchSQLServer.com)
trigger  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SQL Server Development - .NET, C#, T-SQL, Visual Basic
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts