How to audit SQL Server schema updates - sql-server

Is there a way to see who updated which database object and when? I would like to be able to see which developer made a change to some table or stored procedure (or any other object) historically.
I know that we can implement DDL triggers and start capturing this information from this point on but is there a way to see historical data. Database is on SQL Server 2008 R2 Standard edition.

There is no standard way to see this data but it might be possible using 3rd party tools. If your database was in full recovery mode then you can try reading transaction log using 3rd party reader such as ApexSQL Log or Quest Toad. ApexSQL Log specializes in log reading and has more options for this while Toad is similar to SSMS and has many other options for general database management.

Native tools indeed do not provide such option, as for 3rd party tools I suggest you to take a look at SQL Server Change Reporter
from NetWrix (disclosure: I work for them). It has both freeware version as well as free trial. The product shows what changes were made to objects, who made the changes and when. This should be a solution you are looking for.

Related

Software to migrate database changes using SQL "patch" files?

I have 2 MSSQL databases in 2 servers and I need to replicate amendments in schema and in particular tables. Most preferably I need SQL commands to do so. The system should be able to take a snapsnot of initial state of the database and later generate an SQL that would display the changes.
I need to record only particular tables changes in data and I also need to detect changes in table format (alterations of schema). It is desirable that manual adjustment of replication SQL would be possible (so only necessary changes are reflected, in case automated rules allow some unnecessary queries to end up in the patch.
Thanks !
Have a look at RedGate SQL Developer bundle. Unfortunately I'm not aware of any good open source tool for MS SQL Server.
I think that especially SQL Compare could be helpfull for you. If you are using Visual Studio Team Edition, you can have a look at the VS Database projects. I've heard that some teams are successfully using it for their database change management, but I never tried it myself.

Alternatives to snapshot functionality, SQL Server Standard

For testing purposes I would like to reset a complete database to a certain state (data and structure). I would like to do this automated. Preferably a command line instruction before I start my tests.
After some searching I discovered that SQL Server offers an great solution for this. Snapshots. After writing a test query I discovered that the standard edition of SQL Server does NOT support this. :-(
Given the fact that I want this to be automated: what are my options? Overwriting the data files immediately doesn't sound like a good option to me...
Some other information:
Windows 7
Microsoft SQL Server Standard Edition (64-bit), 10.50.1600.1
Database size is app. 1 gigabyte
This is only meant for testing. So the database is NOT used by other users when I'm returning to my 'snapshot'.
More information about snapshots:
http://msdn.microsoft.com/en-us/library/ms190677.aspx
This page also states that the standard edition does not support this. :-(
Thank you in advance!
Detach the MDF file, copy it somewhere, re-attach it.
When you want to reset the database, detatch the MDF, copy the old one over the top, and re-attach it.
Alternatively backup and restore.
If you're doing this for testing purposes, you can use Developer Edition:
SQL Server 2008 Developer includes all of the functionality of Enterprise Edition, but is licensed only for development, test, and demo use.
I homebrewed a solution like this because I wasn't happy with the way replication was running (and I was also using standard edition) - maybe my solution will lead you in the right direction:
http://trycatchfinally.net/2009/09/moving-a-sql-server-database-to-another-server-on-a-schedule-without-using-replication/
Basically, it takes a periodic backup, zips it, FTPs it somewhere, and the remote server checks for new files, extracts them, restores them, and then emails you to let you know a new replica has been restored.
For testing, you can use Developers Edition, which has snapshots and is quite cheap. However, we prefer to build a new test database from scripts in source control - that allows us to easily determine the changes, quickly roll out a testing system on any box, and saves us a lot of time.

How to query from log files (.ldf) of Sql Server 2005 Express edition?

I want to do this, because I would like to know how many times a particular row has been changed.
Is this possible?
Thanks
Reading the log file either takes a commercial tool, or an incredible amount of SQL internals knowledge to achieve. You can see some of the raw output by using:
Select * from ::fn_DBlog(null,null)
Actually decoding to find the same record being altered and ensuring any alteration was committed etc would be a difficult task to put it lightly. So it is 'possible' but not very 'probable' that you will be able to do it.
If you need that functionality within a database then you should be looking at triggers / logic within the code.
Late answer but I hope it will be useful to new readers…
One more function you can try is DBCC LOG but unfortunately this is undocumented function same like fn_dblog.
Problem with transaction log in SQL Server is that it was never meant to be used for this but only to allow point in time recovery and transaction properties.
There is a commercial log reader from ApexSQL that you can try.
Here are also couple similar posts that might get you in the right direction.
Read the log file (*.LDF) in sql server 2008
SQL Server Transaction Log Explorer/Analyzer
you can use this program to do it
http://www.red-gate.com/products/SQL_Log_Rescue/index.htm
Consider using SQL Server 2008.
There is a feature new to SQL Server 2008 called Change Data Capture that does exactly what you require, that is to track data modifications over time.
Looking to inspect the log file in order to track changes is not a wise practice. Doing so will provide you with a limited history, the scope of which would also be dependent on the Recovery Model that you use for your database.
You could "roll your own" solution with a small amount of development, by using a log table and populating it using SQL Server Triggers. The suitability of such a solution is of course dependent on your business case.
Take a look at the following TechNet article for some interesting reading:
Tracking Changes in Your Enterprise Database

SQL Server diff tool

Working on a team where people are prone to amending dev SQL Server tables and forgetting about it, or preparing a change for deployment and having to wait for that deployment. This leaves our dev and live tables inconsistent, causing problems when SPROCs are pushed live.
Is there a tool whereby I can enter a SPROC name and have it check all tables referenced in it in the dev and live DBs, and notify of any differences?
I know two excellent tools for diffing SQL database structures - they don't specifically look inside stored procedures at their text, but they'll show you structural differences in your databases:
RedGate SQL Compare
ApexSQL's SQL Diff
Redgate also has a SQL Dependency Tracker which visualizes object dependencies and could be quite useful here.
Marc
For SQL Server 2005/2008, Open DBDiff works pretty well. The great part about this is that it's free. Also note that I am writing this answer for version 0.9 which currently works for SQL 2005/2008.
It'll show you the differences between the database schema between a source database you specify and the destination database you specify. There are also buttons you can click that can update or create the table that is in question.
I would recommend SQL compare and SQL Data Compare from Redgate Software. I worked with these tools for several projects and they did a great job. Documenting changes is also a good thing to do, but some changes are to complex to write your own SQL code for (including juggling data around between tables).
The redgate tools create scripts in a matter of seconds and those scripts are almost always correct (some older versions had a hard time with table dependencies in big databases, but when playing around with the statements (in a begin transaction / rollback) I was able to quickly fix those problems).
Another strong point in the redgate suites is that you can save your comparison project. This is especially useful when you don't want to convert a certain table (or data), you can exclude them. When loading the project the next time the software will automatically ignore those tables.
One disadvantage is the cost of the software (smaller companies I worked with did not want to buy the software). SQL compare and SQL data compare together will cost you about 800 dollars, but if you look at the time you will save when releasing you will save a lot of money. There is also a trial you can play around with (30 days I believe).
SQLDBDiff is a nice and user-friendly and lite tool.
SQLDBDiff supports SQL Server 2000 to 2016 and also SQL Azure.
SQLDBDiff available with both free with limited use and full with a trial.
More Screen
Try Microsoft Visual Studio Database Edition aka Data Dude (formerly for Database Professionals). It'll do a complete schema comparison and generate the necessary scripts to upgrade the target schema.
Of course, this shouldn't replace a proper build process ;-)
If you need a quick schema comparison tool for SQL Server, you should take a look at dbForge Schema Compare for SQL Server.
I've made a MssqlMerge utility that allows to compare (and merge) MSSQL database data and programming objects. It also allows to search for particular word or phrase across table definitions and programming objects.

How to validate the clients database against my database schema?

Our clients use SQLServer/Oracle databases. Over the years, we've sent them many update scripts which they had to run manually. Most of the time, everything went smooth, but every now and then a script did not run completely to the end or had some errors in it (which weren't detected at the time of the upgrade). Also, sometimes even "smart users" added indexes/tables into those databases themselves, for whatever reason. Later on, those irregularities lead to problems.
Now I have been tasked to figure out a way to verify/validate our clients databases against our own database schema (tables, datatypes, indexes, views, ...). The output should be some kind of difference file indicating what is missing/what should not be in the database. I could do this in code (C++) from inside our application or I can create an external tool for just this one purpose.
Now before I start coding, I wanted to ask if there is already a tool out there that would produce the necessary results, or that at least could help me produce a decent xml file from our master-databases (Oracle and SQLServer)? Or is there a library which could help me write my own tool?
I've used this technique before and it doesn't require buying any tools.
Enterprise Manager has a "Create Script" feature. Perform this on your reference database and the comparison database. Select the appropriate options to generate scripts for the objects you care about. Next, just compare the two generated files with your favorite diff tool.
You can do a similar procedure with Oracle tools that let you export the DDL scripts.
There are three options using Red Gate's tools:
Have your client run the comparison.
You would need to convince your
clients to purchase a license of SQL
Compare and send them a schema
snapshot of your database.
Write an application of your own using Red
Gate's SQL Comparison SDK ($595 for
10 distributions) which can be run
at the client site.
Ask your client to send you a schema snapshot and
run the comparison yourself using your own
copy of SQL Compare. Red Gate
supplies a free schema snapshot tool
called SQL Snapper that will create
snapshots that can then be emailed to you
by your client. As this doesn't include any data, it may be something your client is willing to consider.
The SQL Snapper tool and SQL Comparison SDK sample code can be downloaded from our labs.red-gate.com website.
Oracle compatibility is now available in the form of an Early Access Build. If you're interested or would like to try out the tool visit the product page. You can use this for free until the full release of the tool.
David Atkinson, Product Manager, Red Gate Software.
We use Redgate SQL Compare for this and it's served us well over the years.
We also use Redgate SQL Data Compare for comparing the content of lookup tables.
The folks at redgate have a great tool called SQL Compare.
Can you create a schema dump like MySQL's SHOW CREATE TABLES?
If you're on Windoze, I have used Advanced Query Tool for years, and can attest that, for the money, it does more than anything else. In particular, it will generate a diff report between databases. It is ODBC/VB6, and can run against dozens of databases. Check it out. (No, I am not of QueryTool nor do I own any part of it, just a happy client.)

Resources