CREATE PROCEDURE [dbo].[MIG_GENERATE_DEFAULTS_Sp]
AS BEGIN
	
	DECLARE @Itr_Table NVARCHAR(MAX)
	DECLARE @Itr_TemplateID NVARCHAR(MAX)

	-- ITEM DEFAULTS

	DELETE FROM MIG_ITEM_DEFAULTS

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_ITEM_DEFAULTS(TEMPLATE_ID)
	SELECT TEMPLATE_ID
	FROM DEFAULTS_DEF
	WHERE OBJ_TITLE = 'ITEM' AND LINK_TITLE = ''
	
	DELETE FROM MIG_ITEM_DEFAULTS_EXTRA

	INSERT INTO MIG_ITEM_DEFAULTS_EXTRA(TEMPLATE_ID)
	SELECT TEMPLATE_ID
	FROM DEFAULTS_DEF
	WHERE OBJ_TITLE = 'ITEM' AND LINK_TITLE = ''
	
	DELETE FROM MIG_ITEM_DEFAULT_USERS

	INSERT INTO MIG_ITEM_DEFAULT_USERS(TEMPLATE_ID)
	SELECT TEMPLATE_ID
	FROM DEFAULTS_DEF
	WHERE OBJ_TITLE = 'ITEM' AND LINK_TITLE = ''

	-- ITEM DEFAULTS - END

	-- ITEM_BOM DEFAULTS

	DELETE FROM MIG_ITEM_BOM_DEFAULTS

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_ITEM_BOM_DEFAULTS(TEMPLATE_ID)
	SELECT TEMPLATE_ID
	FROM DEFAULTS_DEF
	WHERE OBJ_TITLE = 'ITEM' AND LINK_TITLE = 'CHILD_LINKS'
	
	-- ITEM_BOM DEFAULTS - END

	-- DOC DEFAULTS

	DELETE FROM MIG_DOC_DEFAULTS

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_DOC_DEFAULTS(TEMPLATE_ID)
	SELECT TEMPLATE_ID
	FROM DEFAULTS_DEF
	WHERE OBJ_TITLE = 'DOC' AND LINK_TITLE = ''
	
	DELETE FROM MIG_DOC_DEFAULT_USERS

	INSERT INTO MIG_DOC_DEFAULT_USERS(TEMPLATE_ID)
	SELECT TEMPLATE_ID
	FROM DEFAULTS_DEF
	WHERE OBJ_TITLE = 'DOC' AND LINK_TITLE = ''

	-- DOC DEFAULTS - END

	-- FOLDER DEFAULTS

	DELETE FROM MIG_FOLDER_DEFAULTS

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_FOLDER_DEFAULTS(TEMPLATE_ID)
	SELECT TEMPLATE_ID
	FROM DEFAULTS_DEF
	WHERE OBJ_TITLE = 'FOLDER' AND LINK_TITLE = ''
	
	-- FOLDER DEFAULTS - END

	-- FILE DEFAULTS

	DELETE FROM MIG_FILE_DEFAULTS

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_ITEM_DEFAULTS(TEMPLATE_ID)
	SELECT TEMPLATE_ID
	FROM DEFAULTS_DEF
	WHERE OBJ_TITLE = 'DATASET' AND LINK_TITLE = ''

	DELETE FROM MIG_FILE_DEFAULT_USERS

	INSERT INTO MIG_FILE_DEFAULT_USERS(TEMPLATE_ID)
	SELECT TEMPLATE_ID
	FROM DEFAULTS_DEF
	WHERE OBJ_TITLE = 'DATASET' AND LINK_TITLE = ''

	-- FILE DEFAULTS - END

	-- BP DEFAULTS

	DELETE FROM MIG_BUSINESS_PROCESS_DEFAULTS

	--Create an entry for all users (existing in Users Table)
	INSERT INTO MIG_BUSINESS_PROCESS_DEFAULTS(TEMPLATE_ID)
	SELECT TEMPLATE_ID
	FROM DEFAULTS_DEF
	WHERE OBJ_TITLE = 'EO' AND LINK_TITLE = ''
		
	DELETE FROM MIG_BP_DEFAULT_USERS

	INSERT INTO MIG_BP_DEFAULT_USERS(TEMPLATE_ID)
	SELECT TEMPLATE_ID
	FROM DEFAULTS_DEF
	WHERE OBJ_TITLE = 'EO' AND LINK_TITLE = ''


	-- BP DEFAULTS - END

	DECLARE RecordsCursor CURSOR  
	LOCAL FORWARD_ONLY FOR  
		SELECT
			CASE
				WHEN OBJ_TITLE = 'ITEM' AND LINK_TITLE = '' THEN 
					'ITEM'
				WHEN OBJ_TITLE = 'ITEM' AND LINK_TITLE = 'CHILD_LINKS' THEN 
					'ITEM_PS_DEF' --(BOM)
				WHEN OBJ_TITLE = 'DOC' AND LINK_TITLE = '' THEN 
					'DOC'
				WHEN OBJ_TITLE = 'FOLDER' AND LINK_TITLE = '' THEN 
					'FOLDER'
				WHEN OBJ_TITLE = 'FILE' AND LINK_TITLE = '' THEN 
					'DATASET' --(File)
				WHEN OBJ_TITLE = 'EO' AND LINK_TITLE = '' THEN 
					'EO' --(Business Process)
				ELSE
					'Unknown'
			END AS 'TABLE',
			TEMPLATE_ID
		FROM DEFAULTS_DEF
		WHERE	(OBJ_TITLE = 'ITEM' AND LINK_TITLE = '')
			OR	(OBJ_TITLE = 'ITEM' AND LINK_TITLE = 'CHILD_LINKS')
			OR	(OBJ_TITLE = 'DOC' AND LINK_TITLE = '') 
			OR	(OBJ_TITLE = 'FOLDER' AND LINK_TITLE = '') 
			OR	(OBJ_TITLE = 'FILE' AND LINK_TITLE = '') 
			OR	(OBJ_TITLE = 'EO' AND LINK_TITLE = '') 
		ORDER BY OBJ_TITLE, LINK_TITLE, TEMPLATE_ID

	OPEN RecordsCursor  
	WHILE 1 = 1   
	BEGIN
		FETCH NEXT FROM RecordsCursor INTO @Itr_Table, @Itr_TemplateID
		IF @@FETCH_STATUS <> 0
			BREAK

		exec MIG_SET_TEMPLATE_DEFAULTS_Sp @Itr_Table, @Itr_TemplateID
	END

	CLOSE RecordsCursor
END

-- exec MIG_GENERATE_DEFAULTS_Sp
-- SELECT * FROM MIG_ITEM_DEFAULTS




