DECLARE @OLD_CONTROLID INTEGER,
@NEW_CONTROLID INTEGER,
@OLD_CONTROL_SCREEN_ID INTEGER,
@NEW_CONTROL_SCREEN_ID INTEGER
	--delete from TBPMO_CONTROL_LANGUAGE where convert(date,added_when)	= convert(date,GETDATE())	
--delete from tbpmo_Control_Screen where convert(date,added_when)	= convert(date,GETDATE())		
--delete from tbpmo_Control where convert(date,added_when)	= convert(date,GETDATE())							
DECLARE c_REDO CURSOR FOR 
	select guid FROM TBPMO_CONTROL_IMPORT WHERE FORM_ID = 1 
  OPEN c_REDO 
    FETCH NEXT FROM c_REDO INTO @OLD_CONTROLID
    WHILE @@FETCH_STATUS = 0
	BEGIN
		
INSERT INTO [dbo].[TBPMO_CONTROL]
           ([CONTROL_TYPE_ID]
           ,[COL_NAME]
           ,[CONNECTION_ID_1]
           ,[CONNECTION_ID_2]
           ,[DEFAULT_VALUE]
           ,[FORM_ID]
           ,[FORMAT_TYPE]
           ,[NAME]
           ,[SHOW_COLUMN]
           ,[PARENT_CONTROL_ID]
           ,[REQUIRED]
           ,[SELECT_ONLY]
           ,[STATIC_LIST]
           ,[MASTER_DATA_ID]
           ,[MULTILINE]
           ,[READ_ONLY]
           ,[SQL_COMMAND_1]
           ,[SQL_COMMAND_2]
           ,[TREE_VIEW]
           ,[VALIDATION]
           ,[VISIBLE]
           ,[ADDED_WHO]
           ,[AUTO_SUGGEST])

     select [CONTROL_TYPE_ID]
           ,COALESCE([COL_NAME],'')
           ,[CONNECTION_ID_1]
           ,[CONNECTION_ID_2]
           ,COALESCE([DEFAULT_VALUE],'')
           ,11
           ,[FORMAT_TYPE]
           ,[NAME]
           ,[SHOW_COLUMN]
           ,[PARENT_CONTROL_ID]
           ,[REQUIRED]
           ,[SELECT_ONLY]
           ,COALESCE([STATIC_LIST],'')
           ,[MASTER_DATA_ID]
           ,[MULTILINE]
           ,[READ_ONLY]
           ,COALESCE([SQL_COMMAND_1],'')
           ,COALESCE([SQL_COMMAND_2],'')
           ,[TREE_VIEW]
           ,[VALIDATION]
           ,[VISIBLE]
           ,'DD TEMPLATE Import'
           ,[AUTO_SUGGEST]
		   FROM TBPMO_CONTROL_IMPORT where GUID = @OLD_CONTROLID
		   SELECT @NEW_CONTROLID = MAX(GUID) FROM TBPMO_CONTROL

		   SELECT @OLD_CONTROL_SCREEN_ID = GUID FROM TBPMO_CONTROL_SCREEN_IMPORT WHERE CONTROL_ID = @OLD_CONTROLID
		   Print '@OLD_CONTROL_SCREEN_ID: ' + convert(varchar(50),@OLD_CONTROL_SCREEN_ID)
		   --Now Insert of ControlScreen
			INSERT INTO [dbo].[TBPMO_CONTROL_SCREEN]
           ([CONTROL_ID]
           ,[SCREEN_ID]
           ,[CONTROL_TEXT]
           ,[X_LOC]
           ,[Y_LOC]
           ,[HEIGHT]
           ,[WIDTH]
           ,[FONT_FAMILY]
           ,[FONT_COLOR]
           ,[FONT_SIZE]
           ,[FONT_STYLE]
           ,[BACK_COLOR]
           ,[TAB_INDEX]
           ,[TAB_STOP]
           ,[ADDED_WHO])
     SELECT @NEW_CONTROLID
           ,1
           ,COALESCE([CONTROL_TEXT],'')
           ,[X_LOC]
           ,[Y_LOC]
           ,[HEIGHT]
           ,[WIDTH]
           ,[FONT_FAMILY]
           ,[FONT_COLOR]
           ,[FONT_SIZE]
           ,[FONT_STYLE]
           ,[BACK_COLOR]
           ,[TAB_INDEX]
           ,[TAB_STOP]
           ,'DD TEMPLATE Import'
		   FROM TBPMO_CONTROL_SCREEN_IMPORT WHERE CONTROL_ID = @OLD_CONTROLID

		    SELECT @NEW_CONTROL_SCREEN_ID = MAX(GUID) FROM TBPMO_CONTROL_SCREEN
			Print '@@NEW_CONTROL_SCREEN_ID: ' + convert(varchar(50),@NEW_CONTROL_SCREEN_ID)
		INSERT INTO [dbo].[TBPMO_CONTROL_LANGUAGE]
           ([LANGUAGE_TYPE]
           ,[CONTROL_SCREEN_ID]
           ,[CAPTION]
           ,[HINT]
           ,[ADDED_WHO])
    SELECT [LANGUAGE_TYPE]
           ,@NEW_CONTROL_SCREEN_ID
           ,COALESCE([CAPTION],'')
           ,[HINT]
           ,[ADDED_WHO] FROM TBPMO_CONTROL_LANGUAGE_IMPORT where CONTROL_SCREEN_ID = @OLD_CONTROL_SCREEN_ID


		FETCH NEXT FROM c_REDO INTO @OLD_CONTROLID
	END
CLOSE c_REDO
DEALLOCATE c_REDO