Brisanje referencijalnog integriteta

DECLARE @database nvarchar(50)
DECLARE @table nvarchar(50)

set @database = ‘DataLab’ — UNIJETI NAZIV BAZE
set @table = ‘tHE_SetItem’ — NAZIV TABELE

declare @schema nvarchar(128), @tbl nvarchar(128), @constraint nvarchar(128)
DECLARE @sql nvarchar(255)
declare cur cursor fast_forward for
select distinct cu.constraint_schema, cu.table_name, cu.constraint_name
from information_schema.table_constraints tc
join information_schema.referential_constraints rc on rc.unique_constraint_name = tc.constraint_name
join information_schema.constraint_column_usage cu on cu.constraint_name = rc.constraint_name
where tc.constraint_catalog = @database and tc.table_name = @table
open cur
fetch next from cur into @schema, @tbl, @constraint
while @@fetch_status <> -1
begin
select @sql = ‘ALTER TABLE ‘ + @schema + ‘.’ + @tbl + ‘ DROP CONSTRAINT ‘ + @constraint
exec sp_executesql @sql
fetch next from cur into @schema, @tbl, @constraint
end
close cur
deallocate cur
go

delete from tPA_SysRef
where anRefId <> 0 and not ‘rt’ + SubString(acDetailTable, 2, 255) + ‘_’ + acMasterTable + ‘_’ + Cast(anRefId as VarChar) in (select Name from sysobjects where xType = ‘F’)