18 setembro 2008

[SQL SERVER] HABILITAR RELACIONAMENTOS DE UMA TABELA

DECLARE
@NOME_FOREIGN VARCHAR(100),
@PARENT INT,
@NOME_TABELA VARCHAR(100)

DECLARE NOMES_FOREIGN CURSOR FOR
SELECT NAME, PARENT_OBJ FROM DBO.SYSOBJECTS
WHERE OBJECTPROPERTY(ID,N'ISFOREIGNKEY') = 1
-- AND NAME LIKE '%' + @SISTEMA + '%' ESCAPE '#'
and name like '%NOME DA TABELA%'

OPEN NOMES_FOREIGN

FETCH NEXT FROM NOMES_FOREIGN INTO @NOME_FOREIGN, @PARENT

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @NOME_TABELA = NAME FROM DBO.SYSOBJECTS WHERE ID = @PARENT

PRINT 'HABILITA CHAVE ESTRANGEIRA: ' + @NOME_TABELA + ' - ' + @NOME_FOREIGN
EXEC('ALTER TABLE [' + @NOME_TABELA + '] CHECK CONSTRAINT [' + @NOME_FOREIGN + ']')

FETCH NEXT FROM NOMES_FOREIGN INTO @NOME_FOREIGN, @PARENT
END

CLOSE NOMES_FOREIGN
DEALLOCATE NOMES_FOREIGN

0 comentários: