Desativar, ativar eliminar e recriar Chaves Estrangeiras (Foreign Keys) SQL
Autor: Wagner Aparecido Miranda | Publicado: 2013-09-14T17:55:00-07:00

por Wagner Aparecido Miranda -&nbsp;W2R Consultoria e Sistemas&nbsp;

Problema
As chaves estrangeiras (FK) são projetados para manter a integridade referencial em seu banco de dados. Quando usado corretamente FKS permitem ter certeza de que seus dados estão intactos e há registros de nenhum órfão. No outro lado do uso de FKS para manter a integridade referencial, eles também se tornam um problema quando você precisa mudar estruturas de tabela ou modificar temporariamente dados que possam violar a restrição de chave estrangeira. Outras dicas foram escritos que mostram como identificar seus FKS e por que você deve usá-los, mas o que é a melhor abordagem para manipular restrições FK para fazer a estrutura ou alterações de dados?

Solução
Abaixo está um script que pode ser usado para encontrar todas as chaves estrangeiras que fazem referência a tabela primária que você deseja trabalhar. Neste roteiro você fornecer o nome da tabela eo nome do esquema (proprietário do objeto). O script irá retornar uma lista de afirmações que podem ser copiados e colados em uma janela de consulta para fazer essas mudanças.

O roteiro também leva três valores de parâmetros diferentes, dependendo da ação que você deseja tomar:

DISABLE&nbsp;- isso irá criar o comando para desativar todas as restrições FK que fazem referência a tabela que você está trabalhando com
ENABLE- isso irá criar o comando para ativar todas as restrições FK que fazem referência a tabela que você está trabalhando com
DROP&nbsp;- isso irá criar um comando para retirar todas as restrições FK e criar um comando para criar todas as restrições FK que fazem referência a tabela está trabalhando com
Os valores abaixo de usar uma tabela no banco de dados Corpore, assim você pode simplesmente copiar e colar este código e executar esse exemplo contra o banco de dados.


SET&nbsp;NOCOUNT&nbsp;ONDECLARE&nbsp;@operation&nbsp;VARCHAR(10)&nbsp;&nbsp;DECLARE&nbsp;@tableName&nbsp;sysname&nbsp;&nbsp;DECLARE&nbsp;@schemaName&nbsp;sysname&nbsp;&nbsp;SET&nbsp;@operation&nbsp;=&nbsp;'DROP' ---- ENABLE,&nbsp;DISABLE,&nbsp;DROP&nbsp;&nbsp;
SET&nbsp;@tableName&nbsp;=&nbsp;'GCCUSTO'&nbsp;&nbsp;
SET&nbsp;@schemaName&nbsp;=&nbsp;'dbo'&nbsp;

DECLARE&nbsp;@cmd&nbsp;NVARCHAR(1000)
DECLARE&nbsp;&nbsp;&nbsp;@FK_NAME&nbsp;sysname,&nbsp;&nbsp;&nbsp;@FK_OBJECTID&nbsp;INT,&nbsp;&nbsp;&nbsp;@FK_DISABLED&nbsp;INT,&nbsp;&nbsp;&nbsp;@FK_NOT_FOR_REPLICATION&nbsp;INT,&nbsp;&nbsp;&nbsp;@DELETE_RULE&nbsp;&nbsp;&nbsp;&nbsp;smallint,&nbsp;&nbsp;&nbsp;@UPDATE_RULE&nbsp;&nbsp;&nbsp;&nbsp;smallint,&nbsp;&nbsp;&nbsp;@FKTABLE_NAME&nbsp;sysname,&nbsp;&nbsp;&nbsp;@FKTABLE_OWNER&nbsp;sysname,&nbsp;&nbsp;&nbsp;@PKTABLE_NAME&nbsp;sysname,&nbsp;&nbsp;&nbsp;@PKTABLE_OWNER&nbsp;sysname,&nbsp;&nbsp;&nbsp;@FKCOLUMN_NAME&nbsp;sysname,&nbsp;&nbsp;&nbsp;@PKCOLUMN_NAME&nbsp;sysname,&nbsp;&nbsp;&nbsp;@CONSTRAINT_COLID&nbsp;INTDECLARE&nbsp;cursor_fkeys&nbsp;CURSOR&nbsp;FOR&nbsp;&nbsp;&nbsp;SELECT&nbsp;&nbsp;Fk.name,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fk.OBJECT_ID,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fk.is_disabled,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fk.is_not_for_replication,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fk.delete_referential_action,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fk.update_referential_action,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OBJECT_NAME(Fk.parent_object_id)&nbsp;AS&nbsp;Fk_table_name,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;schema_name(Fk.schema_id)&nbsp;AS&nbsp;Fk_table_schema,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TbR.name&nbsp;AS&nbsp;Pk_table_name,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;schema_name(TbR.schema_id)&nbsp;Pk_table_schema&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;&nbsp;&nbsp;&nbsp;sys.foreign_keys&nbsp;Fk&nbsp;LEFT&nbsp;OUTER&nbsp;JOIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys.tables&nbsp;TbR&nbsp;ON&nbsp;TbR.OBJECT_ID&nbsp;=&nbsp;Fk.referenced_object_id&nbsp;--inner&nbsp;join&nbsp;&nbsp;&nbsp;WHERE&nbsp;&nbsp;&nbsp;TbR.name&nbsp;=&nbsp;@tableName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;schema_name(TbR.schema_id)&nbsp;=&nbsp;@schemaName
OPEN&nbsp;cursor_fkeys&nbsp;&nbsp;FETCH&nbsp;NEXT&nbsp;FROM&nbsp;&nbsp;&nbsp;cursor_fkeys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INTO&nbsp;@FK_NAME,@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;statement&nbsp;for&nbsp;disabling&nbsp;FK&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF&nbsp;@operation&nbsp;=&nbsp;'DISABLE'&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SET&nbsp;@cmd&nbsp;=&nbsp;'ALTER&nbsp;TABLE&nbsp;['&nbsp;+&nbsp;@FKTABLE_OWNER&nbsp;+&nbsp;'].['&nbsp;+&nbsp;@FKTABLE_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;']&nbsp;&nbsp;NOCHECK&nbsp;CONSTRAINT&nbsp;['&nbsp;+&nbsp;@FK_NAME&nbsp;+&nbsp;']'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT&nbsp;@cmd&nbsp;&nbsp;&nbsp;END&nbsp;&nbsp;&nbsp;--&nbsp;criar declaração para excluir uma FK e também para a recriação de FK&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IF&nbsp;@operation&nbsp;=&nbsp;'DROP'&nbsp;&nbsp;&nbsp;BEGIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- comando drop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SET&nbsp;@cmd&nbsp;=&nbsp;'ALTER&nbsp;TABLE&nbsp;['&nbsp;+&nbsp;@FKTABLE_OWNER&nbsp;+&nbsp;'].['&nbsp;+&nbsp;@FKTABLE_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;']&nbsp;&nbsp;DROP&nbsp;CONSTRAINT&nbsp;['&nbsp;+&nbsp;@FK_NAME&nbsp;+&nbsp;']'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRINT&nbsp;@cmd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- criar processo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DECLARE&nbsp;@FKCOLUMNS&nbsp;VARCHAR(1000),&nbsp;@PKCOLUMNS&nbsp;VARCHAR(1000),&nbsp;@COUNTER&nbsp;INT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- cria o cursor para pegar a coluna do FK&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DECLARE&nbsp;cursor_fkeyCols&nbsp;CURSOR&nbsp;FOR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT&nbsp;&nbsp;COL_NAME(Fk.parent_object_id,&nbsp;Fk_Cl.parent_column_id)&nbsp;AS&nbsp;Fk_col_name,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COL_NAME(Fk.referenced_object_id,&nbsp;Fk_Cl.referenced_column_id)&nbsp;AS&nbsp;Pk_col_name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;&nbsp;&nbsp;&nbsp;sys.foreign_keys&nbsp;Fk&nbsp;LEFT&nbsp;OUTER&nbsp;JOIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys.tables&nbsp;TbR&nbsp;ON&nbsp;TbR.OBJECT_ID&nbsp;=&nbsp;Fk.referenced_object_id&nbsp;INNER&nbsp;JOIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys.foreign_key_columns&nbsp;Fk_Cl&nbsp;ON&nbsp;Fk_Cl.constraint_object_id&nbsp;=&nbsp;Fk.OBJECT_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