Commit 8c923624 authored by CarpathiaLaptop's avatar CarpathiaLaptop

added MD3.DropReferencingFKs.sql that drop FKs from other tables so a table...

added MD3.DropReferencingFKs.sql that drop FKs from other tables so a table can either be dropped or its PK changed
parent c6e995a1
...@@ -75,21 +75,7 @@ BEGIN ...@@ -75,21 +75,7 @@ BEGIN
PRINT 'that are also being dropped. You will need to manually remove those' PRINT 'that are also being dropped. You will need to manually remove those'
PRINT 'as well from the MD3 files.' PRINT 'as well from the MD3 files.'
--look for FKs that REFERENCE the col being removed, or where an FK is bound to the column EXEC MD3.DropReferencingFKs @TableName, @SchemaName ,@ColumnName;
--this needs work.
IF EXISTS (
SELECT Const.*
FROM sys.objects o
CROSS APPLY MD3.f_GetForeignKeys (SCHEMA_NAME(o.schema_id),o.Name) Const
WHERE Const.ParentKeyColumns LIKE '%' + @ColumnName + '%'
OR Const.KeyColumns LIKE '%' + @ColumnName + '%'
)
BEGIN
PRINT 'Stubbed.'
PRINT 'We need to make a call to MD3.CreateForeignKey with the DropFK option here.'
RAISERROR ('An error occurred, please see MD3.CreateColumn',16,1)
END;
--remove any UNIQUE constraints --remove any UNIQUE constraints
EXEC MD3.RemoveUniqueConstraints EXEC MD3.RemoveUniqueConstraints
......
if exists (select * from sysobjects where type = 'P' and id = object_id('MD3.DropReferencingFKs'))
begin
drop proc MD3.DropReferencingFKs
end
go
CREATE PROC MD3.DropReferencingFKs
(
@tablename sysname
,@schemaName SYSNAME
,@ColumnName SYSNAME
)
AS
/*
drops all FKs that REFERENCE the given table/col.
Why is this needed?
-------------------
This is handy when you need to drop a table or a column and need to rid yourself of the referencing objects
MD3 will automatically recreate any FKs during 9ForeignKeys processing
*/
BEGIN
SET NOCOUNT ON
DECLARE @cmd NVARCHAR(1000) , @FK_NAME varchar(200), @sch varchar(200),@tbl VARCHAR(200);
DECLARE RemoveReferencingKeys CURSOR FOR
SELECT ForeignKeyName, SchemaName, TableName
FROM sys.objects o
CROSS APPLY MD3.f_GetForeignKeys (SCHEMA_NAME(o.schema_id),o.Name) Const
WHERE Const.ParentKeyColumns LIKE '%' + @ColumnName + '%'
OR Const.KeyColumns LIKE '%' + @ColumnName + '%'
AND o.schema_id = SCHEMA_ID(@schemaName) AND o.object_id = OBJECT_ID(@tablename);
OPEN RemoveReferencingKeys
FETCH NEXT FROM RemoveReferencingKeys INTO @FK_NAME, @sch,@tbl
WHILE (@@fetch_status = 0)
BEGIN
SELECT @FK_NAME,@sch,@tbl
EXEC MD3.CreateForeignKey
@SchemaName = @sch
,@TableName = @tbl
--,@KeyColumns VARCHAR(MAX) = NULL -- comma-separated list of key columns
,@ForeignKeyName = @FK_NAME
--,@ParentSchemaName SYSNAME = NULL
--,@ParentTableName SYSNAME = NULL
--,@ParentKeyColumns VARCHAR(MAX) = NULL -- comma-separated list of parent table key columns
--,@IsDelCascade BIT = 0 -- default is 0 (NO ACTION on delete) or 1 for CASCADE delete to turn on
--,@IsUpdCascade BIT = 0 -- default is 0 (NO ACTION on update) or 1 for CASCADE update to turn on
--,@AllowNoCheck BIT = 0 -- Allow NOCHECK (ie, existing data may violate FK constraint)
,@DropFK = 1 -- drop the FK based on name, do no other checking.
;
FETCH NEXT FROM RemoveReferencingKeys INTO @FK_NAME, @sch,@tbl;
END;
CLOSE RemoveReferencingKeys;
DEALLOCATE RemoveReferencingKeys;
END;
GO
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment