CREATE PROCEDURE [dbo].[MIG_SET_FOLDER_DEFAULTS_FIELD_VALUE_Sp]
(
	@InputTable NVARCHAR(MAX),
	@InputTemplateID NVARCHAR(MAX),
	@InputClassID NVARCHAR(MAX),
	@InputColumnName NVARCHAR(MAX),
	@InputAttrName NVARCHAR(MAX),
	@InputAttrType NVARCHAR(MAX),
	@InputAttrRefType NVARCHAR(MAX),
	@InputUdtpReference NVARCHAR(MAX),
	@InputClassReference NVARCHAR(MAX),
	@InputAttrReference NVARCHAR(MAX),
	@InputDefaultValue NVARCHAR(MAX)
)
AS    
BEGIN 
	
	DECLARE @MigInterimTable NVARCHAR(MAX)
	DECLARE @UdcdValue NVARCHAR(MAX)
	DECLARE @SQL_QUERY NVARCHAR(MAX)
	DECLARE @D001 NVARCHAR(MAX)

	SET @UdcdValue = NULL

	IF @InputTable = 'FOLDER'
		SET @MigInterimTable = 'MIG_FOLDER_DEFAULTS'

	-- SELECT '            AND @InputColumnName <> ''' + COLUMN_NAME + '''' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'FOLDER' ORDER BY COLUMN_NAME

	IF @InputTable = 'FOLDER'
	BEGIN
		IF      @InputColumnName <> 'AUTHOR'
            AND @InputColumnName <> 'CLASSIFICATION'
            AND @InputColumnName <> 'CREATE_DATE'
            AND @InputColumnName <> 'DATE1'
            AND @InputColumnName <> 'DATE2'
            AND @InputColumnName <> 'DATE3'
            AND @InputColumnName <> 'DATE_APPROVAL'
            AND @InputColumnName <> 'DATE_EFF_FROM'
            AND @InputColumnName <> 'DATE_EFF_THRU'
            AND @InputColumnName <> 'DB_ZZ_ID'
            AND @InputColumnName <> 'DERIVED_FROM'
            AND @InputColumnName <> 'DERIVED_FROM_REV'
            AND @InputColumnName <> 'FOLDER_KEY'
            AND @InputColumnName <> 'FOLDER_NAME'
            AND @InputColumnName <> 'FOLDER_TYPE'
            AND @InputColumnName <> 'INTEGER1'
            AND @InputColumnName <> 'INTEGER2'
            AND @InputColumnName <> 'INTEGER3'
            AND @InputColumnName <> 'INTEGER4'
            AND @InputColumnName <> 'INTEGER5'
            AND @InputColumnName <> 'LIST1'
            AND @InputColumnName <> 'LIST2'
            AND @InputColumnName <> 'LIST3'
            AND @InputColumnName <> 'OBJECT_TYPE'
            AND @InputColumnName <> 'ORIGINATING_APP'
            AND @InputColumnName <> 'PREV_REVISION'
            AND @InputColumnName <> 'PROJECT_ID'
            AND @InputColumnName <> 'REVISION'
            AND @InputColumnName <> 'STATUS'
            AND @InputColumnName <> 'STRING1'
            AND @InputColumnName <> 'STRING2'
            AND @InputColumnName <> 'STRING3'
            AND @InputColumnName <> 'STRING4'
            AND @InputColumnName <> 'STRING5'
            AND @InputColumnName <> 'UPDATED_ON'
            AND @InputColumnName <> 'ZZ_COUNTER'
            AND @InputColumnName <> 'ZZ_ID'
            AND @InputColumnName <> 'ZZ_SYSTIME'
			BEGIN
				RETURN
			END
	END

	----IF @InputAttrType = '' AND @InputDefaultValue = 'FOREVER'
	----	SET @InputDefaultValue = ''

	IF @InputDefaultValue = '(**Current-user**)' OR @InputDefaultValue = '(**Today**)'
		BEGIN
			--TODO:: Update D001 column
			IF @InputTable = 'FOLDER'
				BEGIN
					SET @D001 = ( SELECT D001 FROM MIG_FOLDER_DEFAULTS WHERE TEMPLATE_ID = @InputTemplateID )
				
					IF @D001 IS NULL
						SET @D001 = @InputColumnName
					ELSE
						SET @D001 = @D001 + '|' + @InputColumnName

					--UPDATE MIG_FOLDER_DEFAULTS SET D001 = @D001 WHERE TEMPLATE_ID = @InputTemplateID

					SET @SQL_QUERY = 'UPDATE ' + @MigInterimTable + ' SET '+ @InputColumnName + ' = @InputDefaultValue, 
									  D001 = @D001 
									  WHERE TEMPLATE_ID = @InputTemplateID'

		
					exec sp_executesql	@SQL_QUERY,
										N'@InputDefaultValue NVARCHAR(MAX), 
										@InputTemplateID NVARCHAR(MAX),
										@D001 NVARCHAR(MAX)',
										@InputDefaultValue = @InputDefaultValue,
										@InputTemplateID = @InputTemplateID,
										@D001 = @D001
			

				END
			
			RETURN
		END

	IF @InputTable = 'FOLDER'
		BEGIN
			IF @InputColumnName = 'REVISION' AND @InputDefaultValue = '(**Auto-number**)'
				BEGIN
					SET @InputDefaultValue = '(AUTO)'
				END
		END

	IF @InputUdtpReference IS NOT NULL
		BEGIN
			SELECT @UdcdValue =	(	SELECT TOP 1 ZZ_UDCDTYPE.VALUE_ID
									FROM ZZ_UDCDTYPE 
									WHERE TYPE_NAME = @InputUdtpReference
									AND SHORT_DESCR = @InputDefaultValue	)

		END

	IF @InputDefaultValue = ''
		RETURN

	IF @InputTable = 'FOLDER' AND @UdcdValue IS NOT NULL
		BEGIN
			SET @SQL_QUERY = 'UPDATE ' + @MigInterimTable + ' SET '+ @InputColumnName + ' = @UdcdValue WHERE TEMPLATE_ID = @InputTemplateID'

			exec sp_executesql	@SQL_QUERY,
								N'@InputTable NVARCHAR(MAX),
								@InputColumnName NVARCHAR(MAX), 
								@UdcdValue NVARCHAR(MAX), 
								@InputTemplateID NVARCHAR(MAX)',
								@InputTable = @InputTable,
								@InputColumnName = @InputColumnName,
								@UdcdValue = @UdcdValue,
								@InputTemplateID = @InputTemplateID
		END
	
	ELSE IF @InputTable = 'FOLDER' AND @UdcdValue IS NULL
	BEGIN		
		SET @SQL_QUERY = 'UPDATE ' + @MigInterimTable + ' SET '+ @InputColumnName + ' = @InputDefaultValue WHERE TEMPLATE_ID = @InputTemplateID'

		--IF @InputColumnName = 'ITEM_KEY'
			--BEGIN
				--SET @SQL_QUERY =	'UPDATE MIG_FOLDER_DEFAULTS
				--					SET ITEM_KEY = @InputDefaultValue
				--					WHERE TEMPLATE_ID = @InputTemplateID'

				IF UPPER(@InputDefaultValue) = 'FOREVER' 
					SET @InputDefaultValue = '99991231'

				exec sp_executesql	@SQL_QUERY,
									N'@InputDefaultValue NVARCHAR(MAX), 
									@InputTemplateID NVARCHAR(MAX)',
									@InputDefaultValue = @InputDefaultValue,
									@InputTemplateID = @InputTemplateID
			--END
	END
END




