December 23, 2008 at 11:14 am
December 23, 2008 at 11:16 am
Alter Table schema.table
Drop Constraint FK_Name
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
December 23, 2008 at 11:19 am
December 23, 2008 at 12:56 pm
on a similar issue, if you want to drop a FK on a column, but don't know the actual FK constraint name, i made the proc below...
you simply call
EXEC DROP_FK_FROM_SPECIFIC_COLUMN TABLENAME,COLUMNNAME
and it drops any Foreign keys that that field was referencing...especially handy if you have duplicate FK's:
[font="Courier New"]
CREATE PROCEDURE [dbo].[DROP_FK_FROM_SPECIFIC_COLUMN]
@TableName VARCHAR(30),
@ColumnName VARCHAR(30)
AS
BEGIN
DECLARE @Constraint_to_Delete VARCHAR(100)
DECLARE Constraint_Cursor CURSOR FOR
SELECT name AS ConstraintName FROM dbo.sysobjects
WHERE OBJECTPROPERTY(id, N'IsForeignKey') = 1
AND id IN
(SELECT constid FROM sysforeignkeys
INNER JOIN syscolumns ON sysforeignkeys.fkeyid = syscolumns.id
WHERE fkeyid IN (SELECT id FROM sysobjects
WHERE name = @Tablename AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
AND syscolumns.name = @ColumnName AND fkey =colid)
OPEN Constraint_Cursor
FETCH NEXT FROM Constraint_Cursor INTO @Constraint_to_Delete
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'ALTER TABLE ' + @TableName + ' DROP CONSTRAINT '+@Constraint_to_Delete
EXEC ( 'ALTER TABLE ' + @TableName + ' DROP CONSTRAINT '+@Constraint_to_Delete )
FETCH NEXT FROM Constraint_Cursor INTO @Constraint_to_delete
END
CLOSE Constraint_Cursor
DEALLOCATE Constraint_Cursor
END[/font]
Lowell
December 23, 2008 at 1:39 pm
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply