-----------------------------------------------------------------------------------------
-- Hack for automatically deleting BX foreign keys before SBO upgrade
-----------------------------------------------------------------------------------------
--
-- When SBO runs the upgrade process, it first does a 'DELETE T0 FROM [dbo].[CULG] T0'
-- CULG = company upgrade log table
-- we use this opportunity to delete our foreign keys
--
-----------------------------------------------------------------------------------------

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'BXP_RemoveAllBXForeignKeys_Trigger' AND type = 'TR') DROP TRIGGER BXP_RemoveAllBXForeignKeys_Trigger
GO

--CREATE TRIGGER BXP_RemoveAllBXForeignKeys_Trigger
--ON [CULG]
--FOR DELETE
--AS BEGIN
--
-- This will drop all BxFK_BXP* keys from the database
-- This is needed before SAP Business One upgrade
--
--declare @fkName nvarchar(max)
--declare @parentObj nvarchar(max)
--declare @tableName nvarchar(max)
--declare @tsql nvarchar(max)

--DECLARE cFKeys CURSOR
--	FOR SELECT name, parent_obj FROM sysobjects WHERE name LIKE 'BxFK_%' AND xtype = 'F'
--OPEN cFKeys
--FETCH NEXT FROM cFKeys INTO @fkName, @parentObj
--WHILE @@FETCH_STATUS = 0 BEGIN
--	SELECT @tableName = name FROM sysobjects where id = @parentObj
--	PRINT 'Table: ' + @tableName + ' dropping key: ' + @fkName
--	SET @tsql = 'ALTER TABLE [' + @tableName + '] DROP CONSTRAINT ' + @fkName
--	EXEC (@tsql)
--	FETCH NEXT FROM cFKeys INTO @fkName, @parentObj
--END
--CLOSE cFKeys
--DEALLOCATE cFKeys

--END
--GO
