IF OBJECT_ID(N'LowTempForJohnsonSterilizerRecord', 'U') IS NOT  NULL
	BEGIN
		declare @sql nvarchar(2000);
		declare @fkName nvarchar(255);
		IF OBJECT_ID(N'LowTempForJohnsonRecord', 'U') IS NOT  NULL
			BEGIN
				-- 表数据迁移到新表
				set @sql =  N'insert into LowTempForJohnsonRecord select cycleCounter,endDate,machineNumber,programName,stageTotalMin,stageTotalSec,startDate,SterilizationRecordId,totalCycleCounter from LowTempForJohnsonSterilizerRecord;';
			    -- 删除旧字段
			   IF (select count(*) from syscolumns where id=object_id(N'LowTempForJohnsonStageDetails') and name ='lowTempForJohnsonSterilizerRecord_id') >0
				   BEGIN
						set @fkName = (select fk.name from sysforeignkeys 
							join sysobjects fk on sysforeignkeys.constid = fk.id 
							join sysobjects ftable on sysforeignkeys.fkeyid = ftable.id 
							join sysobjects rtable on sysforeignkeys.rkeyid = rtable.id 
							join syscolumns cn on sysforeignkeys.fkeyid = cn.id and sysforeignkeys.fkey = cn.colid
							where ftable.name='LowTempForJohnsonStageDetails' and cn.name='lowTempForJohnsonSterilizerRecord_id');
						if @fkName is not null
							begin 
								set @sql = @sql + N'alter table LowTempForJohnsonStageDetails drop constraint ' + @fkName +';';
							end;
						set @sql = @sql + N'update LowTempForJohnsonStageDetails set lowTempForJohnsonRecord_id=lowTempForJohnsonSterilizerRecord_id;
											alter table LowTempForJohnsonStageDetails drop column lowTempForJohnsonSterilizerRecord_id;';
				   END;
				    -- 删除旧表
				set @sql = @sql + N'drop table LowTempForJohnsonSterilizerRecord;';
			END;
		ELSE
			BEGIN		
				-- 表数据迁移到新表
				set @sql =  N'select cycleCounter,endDate,machineNumber,programName,stageTotalMin,stageTotalSec,startDate,SterilizationRecordId,totalCycleCounter into LowTempForJohnsonRecord from LowTempForJohnsonSterilizerRecord;';
			     -- 删除旧字段
				IF (select count(*) from syscolumns where id=object_id(N'LowTempForJohnsonStageDetails') and name ='lowTempForJohnsonSterilizerRecord_id') >0
					BEGIN
					set @fkName = (select fk.name from sysforeignkeys 
							join sysobjects fk on sysforeignkeys.constid = fk.id 
							join sysobjects ftable on sysforeignkeys.fkeyid = ftable.id 
							join sysobjects rtable on sysforeignkeys.rkeyid = rtable.id 
							join syscolumns cn on sysforeignkeys.fkeyid = cn.id and sysforeignkeys.fkey = cn.colid
							where ftable.name='LowTempForJohnsonStageDetails' and cn.name='lowTempForJohnsonSterilizerRecord_id');
						if @fkName is not null
							begin 
								set @sql = @sql + N'alter table LowTempForJohnsonStageDetails drop constraint ' + @fkName +';';
							end;
					set @sql = @sql + N'update LowTempForJohnsonStageDetails set lowTempForJohnsonRecord_id=lowTempForJohnsonSterilizerRecord_id;
										alter table LowTempForJohnsonStageDetails drop column lowTempForJohnsonSterilizerRecord_id;';
					END;
				-- 删除旧表
				set @sql = @sql + N'drop table LowTempForJohnsonSterilizerRecord;';
			END;
			Exec sp_executesql @sql;
	END;
commit;