CREATE PROCEDURE [MIG_SET_TEMPLATE_DEFAULTS_Sp]
(
	 @InputTable NVARCHAR(MAX),
	 @InputTemplateID NVARCHAR(MAX)
)
AS BEGIN

	DECLARE @InputClassID NVARCHAR(MAX)
	DECLARE @InputObjTitle NVARCHAR(MAX)
	DECLARE @InputLinkTitle NVARCHAR(MAX)

	DECLARE @Itr_ClassID NVARCHAR(MAX)
	DECLARE @Itr_ColumnName NVARCHAR(MAX)
	DECLARE @Itr_AttrName NVARCHAR(MAX)
	DECLARE @Itr_AttrType NVARCHAR(MAX)
	DECLARE @Itr_AttrRefType NVARCHAR(MAX)
	DECLARE @Itr_UdtpReference NVARCHAR(MAX)
	DECLARE @Itr_ClassReference NVARCHAR(MAX)
	DECLARE @Itr_AttrReference NVARCHAR(MAX)
	DECLARE @Itr_DefaultValue NVARCHAR(MAX)

	IF @InputTable = 'ITEM'
		BEGIN
			SET @InputClassID = 'ITEM'
			SET @InputObjTitle = 'ITEM'
			SET @InputLinkTitle = ''
		END
	ELSE IF @InputTable = 'ITEM_PS_DEF'
		BEGIN
			SET @InputClassID = 'ITEM_PS_DEF'
			SET @InputObjTitle = 'ITEM'
			SET @InputLinkTitle = 'CHILD_LINKS'
		END
	ELSE IF @InputTable = 'DOC'
		BEGIN
			SET @InputClassID = 'DOC'
			SET @InputObjTitle = 'DOC'
			SET @InputLinkTitle = ''
		END
	ELSE IF @InputTable = 'FOLDER'
		BEGIN
			SET @InputClassID = 'FOLDER'
			SET @InputObjTitle = 'FOLDER'
			SET @InputLinkTitle = ''
		END
	ELSE IF @InputTable = 'DATASET'
		BEGIN
			SET @InputClassID = 'FILE'
			SET @InputObjTitle = 'FILE'
			SET @InputLinkTitle = ''
		END
	ELSE IF @InputTable = 'EO'
		BEGIN
			SET @InputClassID = 'EO'
			SET @InputObjTitle = 'EO'
			SET @InputLinkTitle = ''
		END
	
	--Define cursor
	DECLARE RecordsCursor CURSOR  
	LOCAL FORWARD_ONLY FOR  
	SELECT --top 3
		ZZ_CLASS_ATTR.CLASS_ID, 
		OBJ_DEF_ATTR.LEGACY_ATTR AS "COLUMN_NAME",
		OBJ_DEF_ATTR.ATTR_NAME AS "ATTR_NAME",
		ZZ_CLASS_ATTR.ATTR_TYPE,
		ZZ_CLASS_ATTR.ATTR_REF_TYPE,
		ZZ_CLASS_ATTR.UDTP_REFERENCE,
		ZZ_CLASS_ATTR.CLASS_REFERENCE,
		ZZ_CLASS_ATTR.ATTR_REFERENCE,
		DEFAULT_ATTRS.DEFAULT_VALUE
	FROM ZZ_CLASS_ATTR 
	JOIN OBJ_DEF_ATTR
	ON OBJ_DEF_ATTR.OBJ_TITLE = @InputObjTitle
	AND OBJ_DEF_ATTR.LINK_TITLE = @InputLinkTitle
	AND OBJ_DEF_ATTR.LEGACY_ATTR = ZZ_CLASS_ATTR.ATTR_NAME
	JOIN DEFAULT_ATTRS
	ON DEFAULT_ATTRS.TEMPLATE_ID = @InputTemplateID
	AND DEFAULT_ATTRS.ATTR_NAME = OBJ_DEF_ATTR.ATTR_NAME
	WHERE ZZ_CLASS_ATTR.CLASS_ID = @InputClassID 
	ORDER BY ZZ_CLASS_ATTR.ATTR_NAME

	--Open cursor & iterate
	OPEN RecordsCursor  
	WHILE 1 = 1   
	BEGIN
		FETCH NEXT FROM RecordsCursor 
		INTO @Itr_ClassID, @Itr_ColumnName, @Itr_AttrName, @Itr_AttrType, @Itr_AttrRefType, 
			 @Itr_UdtpReference, @Itr_ClassReference,@Itr_AttrReference, 
			 @Itr_DefaultValue
		IF @@FETCH_STATUS <> 0
			BREAK

		IF @InputTable = 'ITEM'
			BEGIN
				--Update specific attribute default value.
				exec MIG_SET_ITEM_DEFAULTS_FIELD_VALUE_Sp	@InputTable, 
															@InputTemplateID,
															@Itr_ClassID, 
															@Itr_ColumnName, 
															@Itr_AttrName, 
															@Itr_AttrType, 
															@Itr_AttrRefType, 
															@Itr_UdtpReference, 
															@Itr_ClassReference, 
															@Itr_AttrReference, 
															@Itr_DefaultValue
			END

		ELSE IF @InputTable = 'ITEM_PS_DEF'
			BEGIN
				--Update specific attribute default value.
				exec MIG_SET_ITEM_BOM_DEFAULTS_FIELD_VALUE_Sp	@InputTable, 
															@InputTemplateID,
															@Itr_ClassID, 
															@Itr_ColumnName, 
															@Itr_AttrName, 
															@Itr_AttrType, 
															@Itr_AttrRefType, 
															@Itr_UdtpReference, 
															@Itr_ClassReference, 
															@Itr_AttrReference, 
															@Itr_DefaultValue
			END

		ELSE IF @InputTable = 'DOC'
			BEGIN
				--Update specific attribute default value.
				exec MIG_SET_DOC_DEFAULTS_FIELD_VALUE_Sp	@InputTable, 
															@InputTemplateID,
															@Itr_ClassID, 
															@Itr_ColumnName, 
															@Itr_AttrName, 
															@Itr_AttrType, 
															@Itr_AttrRefType, 
															@Itr_UdtpReference, 
															@Itr_ClassReference, 
															@Itr_AttrReference, 
															@Itr_DefaultValue
			END

			ELSE IF @InputTable = 'FOLDER'
			BEGIN
				--Update specific attribute default value.
				exec MIG_SET_FOLDER_DEFAULTS_FIELD_VALUE_Sp	@InputTable, 
															@InputTemplateID,
															@Itr_ClassID, 
															@Itr_ColumnName, 
															@Itr_AttrName, 
															@Itr_AttrType, 
															@Itr_AttrRefType, 
															@Itr_UdtpReference, 
															@Itr_ClassReference, 
															@Itr_AttrReference, 
															@Itr_DefaultValue
			END

			ELSE IF @InputTable = 'DATASET'
			BEGIN
				--Update specific attribute default value.
				exec MIG_SET_FILE_DEFAULTS_FIELD_VALUE_Sp	@InputTable, 
															@InputTemplateID,
															@Itr_ClassID, 
															@Itr_ColumnName, 
															@Itr_AttrName, 
															@Itr_AttrType, 
															@Itr_AttrRefType, 
															@Itr_UdtpReference, 
															@Itr_ClassReference, 
															@Itr_AttrReference, 
															@Itr_DefaultValue
			END

			ELSE IF @InputTable = 'EO'
			BEGIN
				--Update specific attribute default value.
				exec MIG_SET_BP_DEFAULTS_FIELD_VALUE_Sp	@InputTable, 
															@InputTemplateID,
															@Itr_ClassID, 
															@Itr_ColumnName, 
															@Itr_AttrName, 
															@Itr_AttrType, 
															@Itr_AttrRefType, 
															@Itr_UdtpReference, 
															@Itr_ClassReference, 
															@Itr_AttrReference, 
															@Itr_DefaultValue
			END


	END

	CLOSE RecordsCursor
END



