Archive for November, 2009

Disabling SQL Server Constraints

Adding and removing constraints from an existing SQL Server database table is performed using the ALTER TABLE statement. The article Using Microsoft SQL Server Constraints provides more information on constraints and some good examples and Books Online has specific details, if needed.

The following statement is an example of adding a primary key constraint to the Account table:

ALTER TABLE [Account] ADD CONSTRAINT [PK_Account] PRIMARY KEY( AccountId )

A common issue that I’ve run into is not adding/removing constraints, but rather needing to disable constraints when deleting data or importing data from related tables, including the primary keys. Disabling constraints is a two-step process because the constraints must also be re-enabled. For example, the following statement disables the ‘FK_Account’ Foreign Key constraint in the Account table.  The keyword “NOCHECK” disables the constraint and “CHECK” enables it.

ALTER TABLE [Account] NOCHECK CONSTRAINT [FK_Account]

Issuing a statement or two isn’t bad, but does require that one knows the name of the constraint to disable. What if you had to deal with five, ten, fifteen or more tables? I needed an automated way of identifying the constraints and creating the ALTER statements based on the constraint type.

Using the INFORMATION_SCHEMA.TABLE_CONSTRAINTS view I was able to automate the process of creating and executing the statements using a variation of the script below. This script will list ALL the Foreign Key constraints and the corresponding table for all tables in the current database. The script can be filtered as needed, but for my purposes, all tables were needed.

SELECT  constraint_name, table_name
FROM    INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE   constraint_type = 'FOREIGN KEY'
ORDER   BY table_name

The above script was converted to the stored procedure below to disable/enable all the constraints of a specific type for all tables.

CREATE PROCEDURE [dbo].[ToggleConstraints]
(
    @type    VARCHAR(128) = 'FOREIGN KEY'
    ,@enable BIT = 1
)
AS
SET NOCOUNT ON

DECLARE @__CONSTRAINTS TABLE(Id INT IDENTITY(1,1), ConstraintName VARCHAR(128), TableName VARCHAR(128))
DECLARE @rows            INT
        ,@index          INT
        ,@sql            NVARCHAR(2000)
        ,@tableName      VARCHAR(128)
        ,@constraintName VARCHAR(128)

-- Retrieve the constraints using the INFORMATION SCHEMA View and place into
-- a table variable based on the specified type. Primary Key and Unique Key
-- constraints are excluded by default to prevent duplicate data from being
-- inserted, but can also be included by changing the constraint_type.

INSERT INTO @__CONSTRAINTS(ConstraintName, TableName)
SELECT constraint_name, table_name
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE constraint_type = @type
ORDER BY table_name

-- Set defaults
SELECT @rows = @@ROWCOUNT, @index = 1

-- Generate and execute the sql script
WHILE( @index <= @rows )
    BEGIN
        SELECT  @tableName = TableName, @constraintName = ConstraintName
        FROM    @__CONSTRAINTS
        WHERE   Id = @index

        -- Toggle the constraint based on the @enable parameter
        SET @sql = 'ALTER TABLE [' + @tableName + '] ' + CASE WHEN @enable = 0 THEN 'NOCHECK' ELSE 'CHECK' END + ' CONSTRAINT [' + @constraintName + ']'

        PRINT @sql

        -- EXEC sp_executesql @sql

        SET @index = @index + 1
    END

SET NOCOUNT OFF

Simply uncomment the EXEC line if you want to actually run the generated statements.


Pinning Visual Studio Projects to the Taskbar in Windows 7

I love the ability to have recent files listed and various shortcuts for documents of an application pinned to a program’s taskbar icon in Windows 7.  I had shortcuts to Visual Studio 2008 and Visual Studio 2005 pinned to the taskbar, but unfortunately, when you right click the taskbar icons you don’t get access to the most recently used solutions and projects – you only get the most recently used files.  You can’t even manually pin solutions or projects to them.  There is, however, a workaround!

Find your Visual Studio “VSLauncher.exe” application.  For me, it was located in: “C:\Program Files (x86)\Common Files\microsoft shared\MSEnv\VSLauncher.exe”.  Pin that to the taskbar.  You now have access to your most recently used items and you can also pin shortcuts to solutions and projects to that icon.

Here’s a screen shot (with some project & solution names partially blocked out).  Enjoy!