On an MS SQL Server 2008 (10.50.2811.0) Developer Edition (64-bit), running under Windows 2008 Server 2008 R2 Standard the script generation from within SQL Server Management Studio creates scripts where the same constraint is dropped multiple times.
A simple example includes two tables, tParent and tChild, where tChild’s ParentID references tParent’s ID. The script (Database context menu => Tasks => Generate Scripts…) being generated (for DROP AND CREATE) looks like this:
USE [TestDb]
GO
/****** Object: ForeignKey [FK_tChild_tParent] Script Date: 08/08/2012 08:11:51 ******/
ALTER TABLE [dbo].[tChild] DROP CONSTRAINT [FK_tChild_tParent]
GO
/****** Object: Table [dbo].[tChild] Script Date: 08/08/2012 08:11:51 ******/
ALTER TABLE [dbo].[tChild] DROP CONSTRAINT [FK_tChild_tParent]
GO
DROP TABLE [dbo].[tChild]
GO
/****** Object: Table [dbo].[tParent] Script Date: 08/08/2012 08:11:52 ******/
DROP TABLE [dbo].[tParent]
GO
/****** Object: Table [dbo].[tParent] Script Date: 08/08/2012 08:11:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tParent](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NOT NULL,
[Value] [int] NOT NULL,
CONSTRAINT [PK_tParent] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[tChild] Script Date: 08/08/2012 08:11:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tChild](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] NOT NULL,
[Name] [varchar](50) NOT NULL,
[Value] [int] NOT NULL,
CONSTRAINT [PK_tChild] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: ForeignKey [FK_tChild_tParent] Script Date: 08/08/2012 08:11:51 ******/
ALTER TABLE [dbo].[tChild] WITH CHECK ADD CONSTRAINT [FK_tChild_tParent] FOREIGN KEY([ParentID])
REFERENCES [dbo].[tParent] ([ID])
GO
ALTER TABLE [dbo].[tChild] CHECK CONSTRAINT [FK_tChild_tParent]
GO
Why are there two identical lines that both try to drop the FK_tChild_tParent constraint? And why is the comment for both lines different?
Appears it is a known bug, as referenced here on Microsoft Connect.
I can confirm the duplicate behavior doesn’t exist on SQL2012.