Quantcast
Channel: Forum SQL Server Database Engine
Viewing all articles
Browse latest Browse all 15889

Alter TAble Query error : Could not drop object 'dbo.Participants' because it is referenced by a FOREIGN KEY constraint.

$
0
0

I am using SQL Server 2008 and SQL Server Management Studio to run queries to tables in a database.  

I am attempting to alter an existing table that contains a foreign key.   I need to add some columns and remove others but not make any changes to either the primary or foriegn key.   I am running the following query and gettign the errors  (after the query):

USE [EXTEND_D]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK__Participa__FK_Pr__40F9A68C]') AND parent_object_id = OBJECT_ID(N'[dbo].[Participants]'))
ALTER TABLE [dbo].[Participants] DROP CONSTRAINT [FK__Participa__FK_Pr__40F9A68C]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK__Participa__FK_Pr__41EDCAC5]') AND parent_object_id = OBJECT_ID(N'[dbo].[Participants]'))
ALTER TABLE [dbo].[Participants] DROP CONSTRAINT [FK__Participa__FK_Pr__41EDCAC5]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK__Participa__FK_Pr__42E1EEFE]') AND parent_object_id = OBJECT_ID(N'[dbo].[Participants]'))
ALTER TABLE [dbo].[Participants] DROP CONSTRAINT [FK__Participa__FK_Pr__42E1EEFE]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK__Participa__FK_Pr__43D61337]') AND parent_object_id = OBJECT_ID(N'[dbo].[Participants]'))
ALTER TABLE [dbo].[Participants] DROP CONSTRAINT [FK__Participa__FK_Pr__43D61337]
GO

USE [EXTEND_D]
GO

/****** Object:  Table [dbo].[Participants]    Script Date: 12/17/2012 10:28:50 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Participants]') AND type in (N'U'))
DROP TABLE [dbo].[Participants]
GO

USE [EXTEND_D]
GO

/****** Object:  Table [dbo].[Participants]    Script Date: 12/17/2012 10:28:50 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Participants](
 [Participant_ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
 [StudyID] [varchar](10), NULL,
 [LastName] [varchar](50), NULL,
 [FirstName] [varchar](50), NULL,
 [StreetAddress] [varchar](50), NULL,
 [StreetAddress2] [varchar](25), NULL,
 [City] [varchar] (50), NULL,
 [State] [varchar] (2), NULL,
 [ZipCode] [varchar] (10), NULL,
 [Phone][varchar] (16), NULL,
 [EmailAddress] [varchar] (50), NULL,
 [CancerDiagnosis] [varchar] (50), NULL,
 [DateDiagnosis] [datetime] NULL,
 [HospitalName] [varchar] (50), NULL,
 [DOB] [datetime] NULL,
 [CreatedBy] [varchar](50), NULL,
 [DateCreated] [datetime] NOT NULL,
 [FK_ProjectID] [numeric](18, 0) NOT NULL,
 CONSTRAINT [PK_Participants] PRIMARY KEY CLUSTERED
(
 [Participant_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

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Participants]  WITH CHECK ADD FOREIGN KEY([FK_ProjectID])
REFERENCES [dbo].[Projects] ([Projects_ID])
GO

ALTER TABLE [dbo].[Participants]  WITH CHECK ADD FOREIGN KEY([FK_ProjectID])
REFERENCES [dbo].[Projects] ([Projects_ID])
GO

ALTER TABLE [dbo].[Participants]  WITH CHECK ADD FOREIGN KEY([FK_ProjectID])
REFERENCES [dbo].[Projects] ([Projects_ID])
GO

ALTER TABLE [dbo].[Participants]  WITH CHECK ADD FOREIGN KEY([FK_ProjectID])
REFERENCES [dbo].[Projects] ([Projects_ID])
GO

 

Msg 3726, Level 16, State 1, Line 4
Could not drop object 'dbo.Participants' because it is referenced by a FOREIGN KEY constraint.
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'NULL'.
Msg 319, Level 15, State 1, Line 24
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

 

Does anyone know how to work around the foreign key constraint to make these changes?

Thanks,

Laurie


Viewing all articles
Browse latest Browse all 15889

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>