Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Automate Default Constraints

By Arun Mallick,

The Problem

When I was working with one of my projects, I had a recurring problem. Each time I added new tables or columns to the Mart or Stage database, I had to check for default constraints for that table and then set defaults manually if they were not set earlier. Sometimes I used to forget to do this and as a result I usually got NULL values in the destination instead of proper default values.

The Solution

I have found a solution to my problem, which was to create one procedure that will take care of all these default constraint values in the destination database. What does my procedure do? Each time you create the database elements, you have to run my procedure and it will check for all the possibilities of default values in each column for every table. Once it finds any, it will drop the old constraints for that column and create a new one that will be appropriate for that column and as per the data type of that column.

The plus point for drop and recreating constraints is that it will always update your constraints value as per the new criteria. I used the following system objects for achieving this goal:

  • sys.tables
  • sys.syscolumns
  • sys.Types
  • sys.default_constraints

The procedure code is attached in the resource section below.

Code Description:

I have used a cursor DefaultCurSet for tracking each column in the given database. The cursor will fetch each column's information into the @TableName, @ColName, @DataType, @DefaultVlue, @ConstraintName variables. Then by using 'ALTER TABLE ' + @TableName + ' DROP CONSTRAINT ' + @ConstraintName I have dropped all constraints in that database. After all the constraints are successfully dropped, the procedure will show this message: 'All default constraints are successfully dropped'.

Similarly, for creating constraints, I used the same cursor DefaultCurSet as I have deallocated the cursor earlier. This time I have fetched each column's information into the @TableName, @ColName, @DataType, @DataTypeSize, @DefaultVlue, @ConstraintName variables.

The constraint name will be created like this: @ConstraintName = 'DF_' + @TableName + '_' + @ColName. Then with this code:

'ALTER TABLE ' + @TableName + 
 ' ADD CONSTRAINT ' + @ConstraintName

I create a new constraint. For example, in destination we have 'LastExtractedDate' and 'LastLoadDate' columns which must have getdate() as the default value. For this, we use this code:

IF @DataType = 'datetime' AND @ColName like '%Last%Date%'
 BEGIN
 SET @Query = 
 'ALTER TABLE ' + @TableName + 
 ' ADD CONSTRAINT ' + @ConstraintName + 
 ' DEFAULT getdate() FOR ' + @ColName
 EXECUTE sp_executesql @Query
 END

After the successful creation of all the constraints in all the columns, it will show the following message: 'All default constraints are successfully created'

The important thing here is that it will check for existence of any IDENTITY column in the table and exclude that column from the default constraint policy. I have achieved the same by adding the code below:

...........
sys.syscolumns SC 
 ON ST.[object_id] = SC.[id]
...........
WHERE SC.status <> 0x80
...........
Here 0x80 or 128 is used to identify whether that column has IDENTITY property set or not.

Conclusion

Now whenever you feel your database needs to be upgraded or modified, just run my procedure or include it in your package sequence; the procedure will take care of all the default constraints needed for your database.

Resources:

default_constraint.sql
Total article views: 4333 | Views in the last 30 days: 5
 
Related Articles
SCRIPT

Columns Explorer - With constraints create / drop scripts (no cursors)

Gets all columns infos, foreign keys (multicolumn fk handled), referenced primary keys, default and ...

FORUM

Default Constraint Error

Getting an error while creating a default constraint dynamically

FORUM

constraints on columns while creating the table

constraints on columns while creating the table

FORUM

problem with naming default constraint and using it while adding a not null column

problem with naming default constraint and using it while adding a not null column

SCRIPT

Fix Default Constraint Naming

This stored procedure can be used for applying a custom default constraint naming

 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones