
/*
	Add item to the paISVProducts table.
*/
use DYNAMICS
delete paISVProducts where PRODID=1755
insert into paISVProducts (PRODID,DSPLNAME,PRCDNAME,TransferCB,RemoveCB,MODULEID) values (1755,'Project Accounting - Core','pa_ArchiveProjectCore',0,0,0)
insert into paISVProducts (PRODID,DSPLNAME,PRCDNAME,TransferCB,RemoveCB,MODULEID) values (1755,'Project Accounting - Contracts','pa_ArchiveProjectContracts',0,0,1)

delete paISVPreview where PRODID=1755
insert into paISVPreview (PRODID,DSPLNAME,PRCDNAME,Count1,MODULEID) values (1755, 'Project Accounting - Core','pa_PreviewProjectCore',0,0)
insert into paISVPreview (PRODID,DSPLNAME,PRCDNAME,Count1,MODULEID) values (1755, 'Project Accounting - Contracts','pa_PreviewProjectContracts',0,1)

delete paISVMasterData where PRODID=1755
insert into paISVMasterData (PRODID,fileOSName,fileName) values (1755, 'PA00401','PA_Fee_MSTR')
insert into paISVMasterData (PRODID,fileOSName,fileName) values (1755, 'PA00402','PA_Project_Fee_Totals_MSTR')
insert into paISVMasterData (PRODID,fileOSName,fileName) values (1755, 'PA00501','PA_Customer_MSTR')
insert into paISVMasterData (PRODID,fileOSName,fileName) values (1755, 'PA00502','PA_Third_Party_Customer_MSTR')

/* This row tells the sizing window the proc that will calculate the amount of data per year */
delete paISVDBSizingLogic where PRODID=1755
insert into paISVDBSizingLogic (PRODID,MODULEID, PRCDNAME) values (1755, 0,'pa_DBSizingProjectCore')
insert into paISVDBSizingLogic (PRODID,MODULEID, PRCDNAME) values (1755, 1,'pa_DBSizingProjectContracts')

/* These rows tell the sizing window which tables make up transaction history for the product.  These appear as nodes under the product */
/* This table is also read to populate the archive report that prints afterwards, so you need a row in it for every transaction history */
/* table you archive.  So, any table in your archive proc that you call pa_AddSQLTableCounts for, you would have in this table */
delete paISVDBSizing where PRODID=1755
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA30100','PA_Timesheet_HDR_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA30101','PA_Timesheet_LINE_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA30103','PA_TS_Distribution_HIST')

insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA30200','PA_Equipment_Log_HDR_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA30201','PA_Equipment_Log_LINE_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA30203','PA_Equipt_Log_Distribution_HIST')

insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA30300','PA_Miscellaneous_Log_HDR_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA30301','PA_Miscellaneous_Log_LINE_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA30303','PA_Miscellaneous_Log_Dist_HIST')

insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA30900','PA_IV_Transfer_HIST_HDR')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA30901','PA_IV_Transfer_HIST_LINE')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA30903','PA_IV_TRX_HIST_Serial_Lot')

insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA33100','PA_Billing_HDR_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA33200','PA_Billing_DTL_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA33204','PA_Billing_Commissions_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA33206','PA_Billing_Subline_Tax_Detail_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA33205','PA_Billing_Tax_Detail_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA33601','PA_Applied_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA33602','PA_Applied_Sub_DTL_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA33203','PA_Billing_Sub_DTL_HIST_TM')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA33202','PA_Billing_Sub_DTL_HIST_FF')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 1,'PA33201','PA_Billing_Sub_DTL_HIST_Fee')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA11500','PA_Job_History_HDR')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA11300','PA_Job_History_LINE')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA11400','PA_Contract_History_HDR')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA30703','PA_Job_History_HDR')

insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA33301','PA_Rev_Recog_HDR_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA33304','PA_Rev_Recog_DTL_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA33302','PA_Rev_Recog_Budg_DTL_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA33303','PA_Rev_Recog_Fee_DTL_HIST')
insert into paISVDBSizing (PRODID,MODULEID, fileOSName,FILENAME) values (1755, 0,'PA33305','PA_RR_Distribution_HIST')

/* Create the pa_ArchiveProjectCore stored procedure */
drop procedure pa_ArchiveProjectCore
go
CREATE   procedure [dbo].[pa_ArchiveProjectCore] 
	@I_FailedTableName	varchar(80) = NULL,
	@I_RemoveTableName	varchar(80) = NULL,
 	@I_Transfer  		tinyint 	= NULL,  
	@I_Remove			tinyint 	= NULL,  
	@I_CutOffDate 		datetime	= NULL ,  
	@I_FromCompany 		varchar(5) 	= NULL,  
	@I_ToCompany 		varchar(5) 	= NULL,
	@I_WhichDate		smallint	= NULL,
	@O_Success			tinyint		= NULL output,
	@O_ErrorState 		int 		= NULL output 
with ENCRYPTION
as  
declare 
	@Loop 		tinyint,   
	@tTransaction 	tinyint,  
	@iError 	integer,
	@TRUE		tinyint,
	@FALSE		tinyint,
	@KeyInfo	varchar(132),
	@DocInfo	varchar(255),
	@ExecString	varchar(2000)
   
/*	Validate parameters 	*/
if 	(@I_Transfer 		is NULL) or  
	(@I_RemoveTableName		is NULL) or
	(@I_Remove 		is NULL) or  
	(@I_CutOffDate 	is NULL) or
	(@I_FromCompany 	is NULL) or  
	(@I_WhichDate 	is NULL) or  
	(@I_ToCompany 		is NULL) 
begin  
	select 	@O_ErrorState 		= 99992,  
		@O_Success		= 0
 	return 
end 

/*	Initialize parameteres and constants	*/
select	@O_Success 		= 0,  
	@O_ErrorState 		= 0,  
	@iError 		= 0,
	@TRUE			= 1,
	@FALSE			= 0

/* Count rows before archiving */
exec pa_AddSQLTableCounts 'PA30100', @I_ToCompany, 1
exec pa_AddSQLTableCounts 'PA30100', @I_FromCompany, 3

select @KeyInfo = @I_FromCompany+ ' To  '+@I_ToCompany + 'Transfer: '+convert(char,@I_Transfer)+' Remove:'+convert(char,@I_Remove)+' Which:'+convert(char,@I_WhichDate)
select @DocInfo = rtrim(convert(char(12),@I_CutOffDate,112))
select @ExecString = 'insert into '+@I_RemoveTableName+' values (1755,'''+@KeyInfo+''','''+@DocInfo+''')'
exec (@ExecString)

select @ExecString = 'insert into '+@I_FailedTableName+' values (1755,'''+@KeyInfo+''','''+@DocInfo+''')'
exec (@ExecString)
/* Count rows after archiving */
exec pa_AddSQLTableCounts 'PA30100', @I_ToCompany, 2
exec pa_AddSQLTableCounts 'PA30100', @I_FromCompany, 4

/*
while (@Loop is null) begin  
	select @Loop = 1
	if @@trancount = 0 begin  
		select @tTransaction = 1  
		begin transaction 
	end 

	/*	 insert batches up to cut off posted ! 	*/
	if @I_WhichDate = 1 begin
		select @ExecString = 'insert into '+@I_RemoveTableName+' '+ 
			     'select distinct DOCTYPE,INVCNMBR,BACHNUMB, BCHSOURC,CUSTNMBR, DOCDATE '+
			     ' from ' + @I_FromCompany + '..PA30100 where '+
			     ' GLPOSTDT <= ''' + rtrim(convert(char(12),@I_CutOffDate,112)) + ''''	
	end 
	else begin 
		select @ExecString = 'insert into '+@I_RemoveTableName+' '+ 
			     'select distinct DOCTYPE,INVCNMBR,BACHNUMB, BCHSOURC,CUSTNMBR, DOCDATE '+
			     ' from ' + @I_FromCompany + '..PA30100 where '+
			     ' DOCDATE <= ''' + rtrim(convert(char(12),@I_CutOffDate,112)) + ''''
	end

	/* Now insert into the FailedTable rows that didn't meet criteria.  For Project, there are none, but commented section
		below shows some made up criteria
	*/
	select @ExecString = ' insert into '+@I_FailedTableName+' '+
		'select DOCTYPE, ''Some made up reason these can not be archived'' from '+@I_RemoveTableName+'R where R.DOCTYPE in (select DOCTYPE from '+@I_FromCompany+'..PA00500)'
	exec (@ExecString)
	select @iError = @@error
	if @iError <> 0 break  

	select @ExecString = ' delete '+@I_RemoveTableName+' R'+
		'where R.DOCTYPE in (select DOCTYPE from '+@I_FromCompany+'..PA00500)'
	exec (@ExecString)
	select @iError = @@error
	if @iError <> 0 break  
	/* End sample criteria */
	
end  
select @O_ErrorState = @iError
if @O_ErrorState <> 0 begin  
	select @O_Success = @FALSE
	if @tTransaction = 1  
		rollback transaction 
end 
else 
begin  
	select @O_Success = @TRUE
	if @tTransaction = 1  
		commit transaction 
end 
*/

/* Create the pa_ArchiveProjectContracts stored procedure */
go
drop procedure pa_ArchiveProjectContracts
go
CREATE   procedure [dbo].[pa_ArchiveProjectContracts] 
	@I_FailedTableName	varchar(80) = NULL,
	@I_RemoveTableName	varchar(80) = NULL,
 	@I_Transfer  		tinyint 	= NULL,  
	@I_Remove			tinyint 	= NULL,  
	@I_CutOffDate 		datetime	= NULL ,  
	@I_FromCompany 		varchar(5) 	= NULL,  
	@I_ToCompany 		varchar(5) 	= NULL,
	@I_WhichDate		smallint	= NULL,
	@O_Success			tinyint		= NULL output,
	@O_ErrorState 		int 		= NULL output 
with ENCRYPTION
as  
declare 
	@Loop 		tinyint,   
	@tTransaction 	tinyint,  
	@iError 	integer,
	@TRUE		tinyint,
	@FALSE		tinyint,
	@KeyInfo	varchar(132),
	@DocInfo	varchar(255),
	@ExecString	varchar(2000)
   
/*	Validate parameters 	*/
if 	(@I_Transfer 		is NULL) or  
	(@I_RemoveTableName		is NULL) or
	(@I_Remove 		is NULL) or  
	(@I_CutOffDate 	is NULL) or
	(@I_FromCompany 	is NULL) or  
	(@I_WhichDate 	is NULL) or  
	(@I_ToCompany 		is NULL) 
begin  
	select 	@O_ErrorState 		= 99992,  
		@O_Success		= 0
 	return 
end 

/*	Initialize parameteres and constants	*/
select	@O_Success 		= 0,  
	@O_ErrorState 		= 0,  
	@iError 		= 0,
	@TRUE			= 1,
	@FALSE			= 0

/* Count rows before archiving */
exec pa_AddSQLTableCounts 'PA30200', @I_ToCompany, 1
exec pa_AddSQLTableCounts 'PA30200', @I_FromCompany, 3

select @KeyInfo = @I_FromCompany+ ' To  '+@I_ToCompany + 'Transfer: '+convert(char,@I_Transfer)+' Remove:'+convert(char,@I_Remove)+' Which:'+convert(char,@I_WhichDate)
select @DocInfo = rtrim(convert(char(12),@I_CutOffDate,112))
select @ExecString = 'insert into '+@I_RemoveTableName+' values (1755,'''+@KeyInfo+''','''+@DocInfo+''')'
exec (@ExecString)

select @ExecString = 'insert into '+@I_FailedTableName+' values (1755,'''+@KeyInfo+''','''+@DocInfo+''')'
exec (@ExecString)
/* Count rows after archiving */
exec pa_AddSQLTableCounts 'PA30200', @I_ToCompany, 2
exec pa_AddSQLTableCounts 'PA30200', @I_FromCompany, 4
go

/* Create the pa_PreviewProjectCore stored procedure */

drop procedure pa_PreviewProjectCore
go
CREATE  procedure [dbo].[pa_PreviewProjectCore] 
	@I_Company 		varchar(5) 	= NULL ,
	@I_CutOffDate 	datetime	= NULL ,  
	@I_WhichDate	int	= NULL
as  
	declare @ExecString varchar (2000),
			@t_Rows int
			
	select @t_Rows = 0
	create table #RowCountTable
	(
		NumberOfRows	int
	)
/*
	Project only has the one date, so we don't care
	if @I_WhichDate=0	
	else				
*/
	select @ExecString = 'insert into #RowCountTable select count(*) from '+@I_Company+'..PA30100 C where C.PADOCDT <= '''+ rtrim(convert(char(12),@I_CutOffDate,112)) + ''''
	exec (@ExecString)
	select @t_Rows = (select distinct NumberOfRows from #RowCountTable)
	delete #RowCountTable
	
	select @ExecString = 'insert into #RowCountTable select count(*) from '+@I_Company+'..PA30300 C where C.PADOCDT <= '''+ rtrim(convert(char(12),@I_CutOffDate,112)) + ''''
	exec (@ExecString)
	select @t_Rows = @t_Rows+(select distinct NumberOfRows from #RowCountTable)
	delete #RowCountTable
	
	select @ExecString = 'insert into #RowCountTable select count(*) from '+@I_Company+'..PA33301 C where C.PADOCDT <= '''+ rtrim(convert(char(12),@I_CutOffDate,112)) + ''''
	exec (@ExecString)
	select @t_Rows = @t_Rows+(select distinct NumberOfRows from #RowCountTable)
	delete #RowCountTable
	
	/* Now, insert total into paISVPreview table */
	select @ExecString = 'update DYNAMICS..paISVPreview set Count1 = '+convert(char,@t_Rows)+' where PRODID=1755 and MODULEID=0'
	exec (@ExecString)

go	

/* Create the pa_PreviewProjectContracts stored procedure */
go
drop procedure pa_PreviewProjectContracts
go
CREATE  procedure [dbo].[pa_PreviewProjectContracts] 
	@I_Company 		varchar(5) 	= NULL ,
	@I_CutOffDate 	datetime	= NULL ,  
	@I_WhichDate	int	= NULL
as  
	declare @ExecString varchar (2000),
			@t_Rows int
			
	select @t_Rows = 0
	create table #RowCountTable
	(
		NumberOfRows	int
	)
/*
	Project only has the one date, so we don't care
	if @I_WhichDate=0	
	else				
*/

	select @ExecString = 'insert into #RowCountTable select count(*) from '+@I_Company+'..PA30200 C where C.PADOCDT <= '''+ rtrim(convert(char(12),@I_CutOffDate,112)) + ''''
	exec (@ExecString)
	select @t_Rows = @t_Rows+(select distinct NumberOfRows from #RowCountTable)
	delete #RowCountTable

	select @ExecString = 'insert into #RowCountTable select count(*) from '+@I_Company+'..PA30900 C where C.PADOCDT <= '''+ rtrim(convert(char(12),@I_CutOffDate,112)) + ''''
	exec (@ExecString)
	select @t_Rows = @t_Rows+(select distinct NumberOfRows from #RowCountTable)
	delete #RowCountTable
	
	select @ExecString = 'insert into #RowCountTable select count(*) from '+@I_Company+'..PA33200 C where C.PADOCDT <= '''+ rtrim(convert(char(12),@I_CutOffDate,112)) + ''''
	exec (@ExecString)
	select @t_Rows = @t_Rows+(select distinct NumberOfRows from #RowCountTable)
	delete #RowCountTable
	
	/* Now, insert total into paISVPreview table */
	select @ExecString = 'update DYNAMICS..paISVPreview set Count1 = '+convert(char,@t_Rows)+' where PRODID=1755 andd MODULEID=1'
	exec (@ExecString)

go	

/* Create the pa_DBSizingProjectCore stored procedure */
drop proc pa_DBSizingProjectCore
go

CREATE  procedure [dbo].[pa_DBSizingProjectCore] 
	@I_Company 		varchar(5) 	= NULL ,
 	@I_Year  		int		 	= NULL,  
	@I_StartDate 	datetime	= NULL ,  
	@I_EndDate	 	datetime	= NULL 
as  
	declare @ExecString varchar (2000),
		@RCount int

	/* insert rows into @I_Company..paDBDist
		first fill temp table with docs from header table in date range.
		add count of that table to paDBDist.
		now count rows in child tables that have docs in the header table, adding a row to paDBDist for each child table
	*/
	create table #ProjectTemp
	(
		ProjectNo	CHAR(17)
	)
	create table #RowCountTable
	(
		NumberOfRows	int
	)
	select @ExecString = 'delete '+@I_Company+'..paDBDist where PRODID=1755 and MODULE1=0 and YEAR1= '+ convert(char,@I_Year)
	exec (@ExecString)
	
	select @ExecString = 'insert into #ProjectTemp select PATSNO from '+@I_Company+'..PA30100 S where S.PADOCDT between '''+ rtrim(convert(char(12),@I_StartDate,112)) + ''''+
		' and '''+ rtrim(convert(char(12),@I_EndDate,112)) + ''''
	exec (@ExecString)
	select @RCount = (select count(*) from #ProjectTemp)
	
	select @ExecString = 'insert into '+@I_Company+'..paDBDist values ('+convert(char,@I_Year) +',1755,0,''PA30100'','+ convert(char,@Rcount)+',0)'
	exec (@ExecString)

	exec( 'insert into #RowCountTable select count(*) from '+@I_Company+'..PA30101 C where C.PATSNO in (select ProjectNo from #ProjectTemp)')
	select @Rcount = (select distinct NumberOfRows from #RowCountTable)
	delete #RowCountTable
	
	select @ExecString = 'insert into '+@I_Company+'..paDBDist values ('+convert(char,@I_Year) +',1755,0,''PA30101'','+ convert(char,@Rcount)+',0)'
	exec (@ExecString)


/* Companion RM Tables sample */
insert into paISVModComp values (1755,'RM',1,'paProjectModuleCompanionFilter')	/* filter further */
insert into paISVModComp values (1755,'RM',2,'paProjectModuleCompanionArchive')	/* archive additional tables */
go

/* Create the pa_DBSizingProjectContracts stored procedure */
drop proc pa_DBSizingProjectContracts
go

CREATE  procedure [dbo].[pa_DBSizingProjectContracts]
	@I_Company 		varchar(5) 	= NULL ,
 	@I_Year  		int		 	= NULL,  
	@I_StartDate 	datetime	= NULL ,  
	@I_EndDate	 	datetime	= NULL 
as  
	declare @ExecString varchar (2000),
		@RCount int

	/* insert rows into @I_Company..paDBDist
		first fill temp table with docs from header table in date range.
		add count of that table to paDBDist.
		now count rows in child tables that have docs in the header table, adding a row to paDBDist for each child table
	*/
	create table #ProjectTemp
	(
		ProjectNo	CHAR(17)
	)
	create table #RowCountTable
	(
		NumberOfRows	int
	)
	select @ExecString = 'delete '+@I_Company+'..paDBDist where PRODID=1755 and MODULE1=1 and YEAR1= '+ convert(char,@I_Year)
	exec (@ExecString)
	
	exec( 'insert into #RowCountTable select count(*) from '+@I_Company+'..PA30103 C where C.PATSNO in (select ProjectNo from #ProjectTemp)')
	select @Rcount = (select distinct NumberOfRows from #RowCountTable)
	delete #RowCountTable
	
	select @ExecString = 'insert into '+@I_Company+'..paDBDist values ('+convert(char,@I_Year) +',1755,1,''PA30103'','+ convert(char,@Rcount)+',0)'
	exec (@ExecString)

/* Companion RM Tables sample */
insert into paISVModComp values (1755,'RM',1,'paProjectModuleCompanionFilter')	/* filter further */
insert into paISVModComp values (1755,'RM',2,'paProjectModuleCompanionArchive')	/* archive additional tables */
go

drop procedure [paProjectModuleCompanionFilter]
go
CREATE  procedure [dbo].[paProjectModuleCompanionFilter] 
	@RemoveTableName	varchar(40)	= NULL,
	@FailedTableName	varchar(40)	= NULL,
	@TempTable1Name		varchar(40)	= NULL,
	@TempTable2Name		varchar(40)	= NULL,
	@TempTable3Name		varchar(40)	= NULL,
 	@Transfer  		tinyint 		= NULL,  
	@Remove		tinyint 		= NULL,  
	@CutOffDate 		datetime	= NULL ,  
	@FromCompany 	varchar (5) 	= NULL,  
	@ToCompany 		varchar (5) 	= NULL,
	@WhichDate		smallint		= NULL
as  

declare @ExecString varchar (2000)
	
/* Remove rows from t@RemoveTableName that have an A in the customer name, and put them in the @FailedTableName table */
select @ExecString = 'insert into '+@FailedTableName+' select R.RMDTYPAL, R.DOCNUMBR,R.CUSTNMBR,R.DOCDATE, ''Starts with letter A'','+
	''''','''','''+	'01/01/1900'''+ ' from '+@RemoveTableName+' R where CUSTNMBR in (select CUSTNMBR from '+@FromCompany+'..RM00101 where CUSTNAME like ''A%'')'
exec (@ExecString)
select @ExecString = 'delete '+@RemoveTableName+' where CUSTNMBR in (select CUSTNMBR from '+@FromCompany+'..RM00101 where CUSTNAME like ''A%'')'

go
drop procedure paProjectModuleCompanionArchive
go
CREATE  procedure [dbo].[paProjectModuleCompanionArchive] 
	@RemoveTableName	varchar(40)	= NULL,
	@FailedTableName	varchar(40)	= NULL,
	@TempTable1Name		varchar(40)	= NULL,
	@TempTable2Name		varchar(40)	= NULL,
	@TempTable3Name		varchar(40)	= NULL,
 	@Transfer  		tinyint 		= NULL,  
	@Remove		tinyint 		= NULL,  
	@CutOffDate 		datetime	= NULL ,  
	@FromCompany 	varchar (5) 	= NULL,  
	@ToCompany 		varchar (5) 	= NULL,
	@WhichDate		smallint		= NULL
as  

declare @ExecString varchar (2000)
	
/* Remove rows from t@RemoveTableName that have an A in the customer name, and put them in the @FailedTableName table */
select @ExecString = 'insert into '+@FailedTableName+' select R.RMDTYPAL, R.DOCNUMBR,R.CUSTNMBR,R.DOCDATE, ''Starts with letter A'','+
	''''','''','''+	'01/01/1900'''+ ' from '+@RemoveTableName+' R where CUSTNMBR in (select CUSTNMBR from '+@FromCompany+'..RM00101 where CUSTNAME like ''A%'')'
exec (@ExecString)
select @ExecString = 'delete '+@RemoveTableName+' where CUSTNMBR in (select CUSTNMBR from '+@FromCompany+'..RM00101 where CUSTNAME like ''A%'')'
