Click here to monitor SSC

FatherJack

SQL Q+A forum at ask.sqlservercentral.com | Follow fatherjack on Twitter

OK, I have my database ready, now what's missing?

Published Wednesday, December 29, 2010 3:00 AM

LOST: Holy GrailDuring the life of any database there will be times when the development makes a change that breaks functionality of an object somewhere else in the database. SQL Server does a good job in some places of making this impossible, or at least really difficult, but in other places there isn't even a murmur as you execute a script that will bring your system processes out in a nasty plague of error messages.


Where it works.
If you try to create a view based on a table or column that doesn't exist then you get told so quite rapidly. Open up a TSQL editor and connect to the AdventureWorks database and run this script to see what I mean:

SELECT   [m].[ID] ,
        
[m].[FName] ,
        
[m].[SName] ,
        
[m].[StartDate]
FROM    [Jack].[Members] AS m

Now, unless you have a Members table in a schema called Jack you will get this error message:
Msg 208, Level 16, State 1, Line 2
Invalid object name 'jack.Members'.
So, this stops you building an object like a view or procedure that tries to reference something that doesn't exist. SQL Server is saving you from yourself and delivering a product that doesn't work.


Where it doesn't work.
Now, let's try something else. Let's create the schema, a table, insert some data and then create a view based on that table, reasonable enough process for a lot of databases. I did that using this :

CREATE SCHEMA Jack
GO
CREATE TABLE [Jack].[Members]
  
(
            
ID INT IDENTITY
            
NOT NULL ,
            
FName VARCHAR(20) ,
            
SName VARCHAR(60) ,
            
StartDate DATE
  
)
GO
INSERT  INTO [Jack].[Members]
  
( [FName] ,
            
[SName] ,
            
[StartDate]
  
)
  
SELECT TOP 50 -- insert 50 fictitious members
        
[c].[FirstName] ,
        
[c].[LastName] ,
        
DATEADD(wk, ( ABS(CHECKSUM(NEWID())) % 600 + 45 ) * -1,
-- random number of weeks (more than 45, less than 600)
            
GETDATE()) AS startdate
  
FROM    [Person].[Contact] AS c
GO
CREATE VIEW [Jack].vGetMembers
        
AS
SELECT  
[m].[ID] ,
        
[m].[FName] ,
        
[m].[SName] ,
        
[m].[StartDate]
  
FROM    [Jack].[Members] AS m
GO

and prove it works using this:

SELECT   [vgm].[ID] ,
        
[vgm].[FName] + ' ' + [vgm].[SName] AS [Member Name],
        
[vgm].[StartDate]
  
FROM    [Jack].[vGetMembers] AS vgm
  
WHERE [vgm].[StartDate] > DATEADD(YEAR,-4,GETDATE())
-- members who joined in last 4 years
GO

OK, now if we assume this gets put in to production and works really well for a few years but then there is a business need to make some changes and the DBA that built the database isn't around or the Junior DBA runs a script in production instead of test . and so on and so on . and the table gets deleted.

DROP TABLE [Jack].[Members]
GO

Now we get this error if we try to reference the view:
Msg 208, Level 16, State 1, Procedure vGetMembers, Line 7
Invalid object name 'Jack.Members'.
Msg 4413, Level 16, State 1, Line 2
Could not use view or function 'Jack.vGetMembers' because of binding errors.
The view has been rendered useless as an object that it relies on is no longer available. How would you go about checking the database for other places where this table was referenced? Well, Red Gate have a feature in their next version of SQL Compare Prompt that will let you scan your database from the Object Explorer tree to check for any such situations. Simply Right Click the database you want to check and select Find Invalid Objects, the UI then shows you all cases of objects that wont work due to a reliance on other objects that no longer exist.
InvalidObjectsBlog
So, if you want to see what relics there are in any of your databases that wont work when someone tries to use them, take a look at SQL Prompt 5 from Red Gate. You get a 14 day free trial of the full product here - http://www.red-gate.com/products/SQL_Prompt/index.htm

Now, as my Mother always told me, don't forget to clear up after yourself:

DROP VIEW [Jack].[vGetMembers]
GO
DROP TABLE [Jack].[Members]
GO
DROP SCHEMA [Jack]
Full disclosure:
I am a Friend of Red Gate and therefore have a wholly biased opinion of their awesome DBA tools. I don't get paid by them to say nice things, I don't get free licenses to use for my job but I do get to see some of their products ahead of other people and am therefore prepared for the shock of how much easier my job will become with each new enhancement.
LiveJournal Tags: ,,
by fatherjack
Filed Under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Jonathan Allen said:

LiveJournal Tags: How To,SQL Server,Tips and Tricks,TSQL,Reporting Services  A little while ago I wrote...
January 24, 2011 7:47 AM

What do you think?

(required) 
(optional)
(required) 

About fatherjack

DBA since 1999 working for not-for-profit company. http://twitter.com/fatherjack,
Latest articles
Checking Out SQL Backup Pro 7’s New Automatic Backup Verification
 Wouldn't it be great to offload the daily chore of checking the integrity of your production... Read more...

Chuck Lathrope: DBA of the Day
 Chuck Lathrope was a finalist for the Exceptional DBA of the Year award in 2009. We contacted him to... Read more...

Backups, What Are They Good For?
 Pixar recently confessed, in an engaging video, that Toy Story 2 was almost lost due to a bad backup,... Read more...

C# Async: What is it, and how does it work?
 The biggest new feature in C#5 is Async, and its associated Await (contextual) keyword. Anybody who is... Read more...

SQL Server 2012 AlwaysOn
 SQL Server AlwaysOn provides a high-availability and Disaster-recovery solution for SQL Server 2012. It... Read more...