Information
This document helps you recover missing asset and all of its children assets in the hierarchy tree. You need to restore databases and run the various SQL scripts below in a tool like SQL Server Management Studio (SSMS). You need to replace entCustomer database name with <Your current database name> and entOldDatabase name with <your old restored database name> before running the SQL statements in this document.
Requirement
- Old database (
entOldDatabase) - Current database (
entCustomer) - Lost
AssetIDorAssetName
Restore Databases and find AssetPK
Step1: Restore Old database in same SQL Server where your current database is.
Our Old database: entOldDatabase
Current database: entCustomer
Step2: Find AssetPK of lost asset
If you know AssetID of lost asset you can use below SQL to get the AssetPK of that missing Asset.
SELECT AssetPK
FROM Asset
WHERE AssetID='MainBuilding'Output: AssetPK = 1234
This guide will help you restore Asset (AssetPK=1234) and all its children assets in AssetHierarchy and PMAsset tables.
If you don't you will have to use some other information to find the Asset and from there find the AssetPK
SQL statements
The SQL statements should be run in the order given below as the AssetHierarchy table has a dependency on the Asset table, so the restore to the Asset table has to happen first, then the AssetHierachy needs to be repaired.
Step 1: Insert Parent Asset in Asset table (AssetPK=1234).
USE entCustomer
SET IDENTITY_INSERT Asset ON;
INSERT INTO entCustomer.dbo.ASSET ([AssetPK],[AssetID],[AssetName],[InternalAssetNum],[IsLocation],[IsClone],[RequesterCanView],[IsUp],[Status],[StatusDesc],[Type],[TypeDesc],[ClassificationPK],[ClassificationID],[ClassificationName],[Model],[Serial],[System],[SystemDesc],[OperatorPK],[OperatorID],[OperatorName],[DepartmentPK],[DepartmentID],[DepartmentName],[TenantPK],[TenantID],[TenantName],[Vicinity],[AccountPK],[AccountID],[AccountName],[GenLedger],[RepairCenterPK],[RepairCenterID],[RepairCenterName],[ShopPK],[ShopID],[ShopName],[Priority],[PriorityDesc],[VendorPK],[VendorID],[VendorName],[ManufacturerPK],[ManufacturerID],[ManufacturerName],[WarrantyExpire],[ContactPK],[ContactID],[ContactName],[Address],[City],[State],[Zip],[Country],[DisplayMapOnWO],[PurchasedDate],[PurchaseOrder],[PurchaseCost],[CurrentValue],[InstallDate],[StartupDate],[ReplaceDate],[ReplacementCost],[Life],[DisposalDate],[LicenseNumber],[Instructions],[InstructionsToWO],[InsuranceCarrier],[InsurancePolicy],[LeaseNumber],[RegistrationDate],[xcoordinate],[ycoordinate],[zcoordinate],[parcelid],[IsPredictive],[Technology],[TechnologyDesc],[IsMeter],[Meter1Reading],[Meter1Units],[Meter1UnitsDesc],[Meter1TrackHistory],[Meter2Reading],[Meter2Units],[Meter2UnitsDesc],[Meter2TrackHistory],[LastMaintained],[LastMaintained_WOPK],[Icon],[Photo],[UDFChar1],[UDFChar2],[UDFChar3],[UDFChar4],[UDFChar5],[UDFChar6],[UDFChar7],[UDFChar8],[UDFChar9],[UDFChar10],[UDFDate1],[UDFDate2],[UDFBit1],[UDFBit2],[DemoLaborPK],[RowVersionIPAddress],[RowVersionUserPK],[RowVersionInitials],[RowVersionAction],[RowVersionDate],[PurchaseType],[PurchaseTypeDesc],[DrawingUpdatesNeeded],[DisplayOrder],[ClassIndustry],[ClassIndustryDesc],[RiskAssessmentRequired],[AssessedByPK],[AssessedByID],[AssessedByName],[LastAssessed],[RiskAssessmentGroup],[RiskAssessmentGroupDesc],[RiskFactor1],[RiskFactor1Score],[RiskFactor2],[RiskFactor2Score],[RiskFactor3],[RiskFactor3Score],[RiskFactor4],[RiskFactor4Score],[RiskFactor5],[RiskFactor5Score],[RiskScore],[RiskLevel],[PMRequired],[PlanForImprovement],[HIPPARelated],[StatementOfConditions],[StatementOfConditionsCompliant],[ZonePK],[ZoneID],[ZoneName],[County],[YearBuilt],[MajorRenovations],[SquareFootage],[ConstructionCode],[ConstructionCodeDesc],[NumberOfStories],[ISOProtection],[ISOProtectionDesc],[AutoSprinkler],[AutoSprinklerDesc],[SmokeAlarm],[SmokeAlarmDesc],[HeatAlarm],[HeatAlarmDesc],[FloodZone],[QuakeZone],[Ext100Feet],[OperatingUnits],[EstimatedValue],[ResponsibilityRepairPK],[ResponsibilityRepairID],[ResponsibilityRepairName],[ResponsibilityPMPK],[ResponsibilityPMID],[ResponsibilityPMName],[ResponsibilitySafetyPK],[ResponsibilitySafetyID],[ResponsibilitySafetyName],[ServiceRepairPK],[ServiceRepairID],[ServiceRepairName],[ServicePMPK],[ServicePMID],[ServicePMName],[Meter1RollDown],[Meter2RollDown],[Meter1RollDownMethod],[Meter2RollDownMethod],[Meter1ReadingLife],[Meter2ReadingLife],[IsLinear],[UDFDate3],[UDFDate4],[UDFDate5],[UDFBit3],[UDFBit4],[UDFBit5],[OrigPKforCloning],[PMCycleStartBy],[PMCycleStartByDesc],[PMCycleStartDate],[PMCounter],[ModelNumber],[ModelNumberMFG],[ModelLine],[ModelLineDesc],[ModelSeries],[ModelSeriesDesc],[SystemPlatform],[SystemPlatformDesc],[ResourceForScheduling],[RotatingPartPK],[RotatingPartID],[RotatingPartName],[RotatingRepairCenterPK],[RotatingLocationPK],[RotatingLaborPK],[GISLayer],[GISKeyValue],[GISTable],[ReceiveDate],[Latitude],[Longitude],[AssetFromPK],[AssetFromID],[AssetFromName],[AssetToPK],[AssetToID],[AssetToName],[FormName],[MaintainableToolPK],[MaintainableToolID],[MaintainableToolName],[MaintainableToolLocationPK],[RiskLevelAutoCalc],[PMRequiredAutoCalc],[RotatingBin],[AssetUUID],[RequireFinalReading],[apsSyncGUID],[RequireInitialReading],[apsRequirePhotos],[apsIconDefinitionPK],[apsRiskFactor1PK],[apsRiskFactor2PK],[apsRiskFactor3PK],[apsRiskFactor4PK],[apsRiskFactor5PK])
SELECT [AssetPK],[AssetID],[AssetName],[InternalAssetNum],[IsLocation],[IsClone],[RequesterCanView],[IsUp],[Status],[StatusDesc],[Type],[TypeDesc],[ClassificationPK],[ClassificationID],[ClassificationName],[Model],[Serial],[System],[SystemDesc],[OperatorPK],[OperatorID],[OperatorName],[DepartmentPK],[DepartmentID],[DepartmentName],[TenantPK],[TenantID],[TenantName],[Vicinity],[AccountPK],[AccountID],[AccountName],[GenLedger],[RepairCenterPK],[RepairCenterID],[RepairCenterName],[ShopPK],[ShopID],[ShopName],[Priority],[PriorityDesc],[VendorPK],[VendorID],[VendorName],[ManufacturerPK],[ManufacturerID],[ManufacturerName],[WarrantyExpire],[ContactPK],[ContactID],[ContactName],[Address],[City],[State],[Zip],[Country],[DisplayMapOnWO],[PurchasedDate],[PurchaseOrder],[PurchaseCost],[CurrentValue],[InstallDate],[StartupDate],[ReplaceDate],[ReplacementCost],[Life],[DisposalDate],[LicenseNumber],[Instructions],[InstructionsToWO],[InsuranceCarrier],[InsurancePolicy],[LeaseNumber],[RegistrationDate],[xcoordinate],[ycoordinate],[zcoordinate],[parcelid],[IsPredictive],[Technology],[TechnologyDesc],[IsMeter],[Meter1Reading],[Meter1Units],[Meter1UnitsDesc],[Meter1TrackHistory],[Meter2Reading],[Meter2Units],[Meter2UnitsDesc],[Meter2TrackHistory],[LastMaintained],[LastMaintained_WOPK],[Icon],[Photo],[UDFChar1],[UDFChar2],[UDFChar3],[UDFChar4],[UDFChar5],[UDFChar6],[UDFChar7],[UDFChar8],[UDFChar9],[UDFChar10],[UDFDate1],[UDFDate2],[UDFBit1],[UDFBit2],[DemoLaborPK],[RowVersionIPAddress],[RowVersionUserPK],[RowVersionInitials],[RowVersionAction],[RowVersionDate],[PurchaseType],[PurchaseTypeDesc],[DrawingUpdatesNeeded],[DisplayOrder],[ClassIndustry],[ClassIndustryDesc],[RiskAssessmentRequired],[AssessedByPK],[AssessedByID],[AssessedByName],[LastAssessed],[RiskAssessmentGroup],[RiskAssessmentGroupDesc],[RiskFactor1],[RiskFactor1Score],[RiskFactor2],[RiskFactor2Score],[RiskFactor3],[RiskFactor3Score],[RiskFactor4],[RiskFactor4Score],[RiskFactor5],[RiskFactor5Score],[RiskScore],[RiskLevel],[PMRequired],[PlanForImprovement],[HIPPARelated],[StatementOfConditions],[StatementOfConditionsCompliant],[ZonePK],[ZoneID],[ZoneName],[County],[YearBuilt],[MajorRenovations],[SquareFootage],[ConstructionCode],[ConstructionCodeDesc],[NumberOfStories],[ISOProtection],[ISOProtectionDesc],[AutoSprinkler],[AutoSprinklerDesc],[SmokeAlarm],[SmokeAlarmDesc],[HeatAlarm],[HeatAlarmDesc],[FloodZone],[QuakeZone],[Ext100Feet],[OperatingUnits],[EstimatedValue],[ResponsibilityRepairPK],[ResponsibilityRepairID],[ResponsibilityRepairName],[ResponsibilityPMPK],[ResponsibilityPMID],[ResponsibilityPMName],[ResponsibilitySafetyPK],[ResponsibilitySafetyID],[ResponsibilitySafetyName],[ServiceRepairPK],[ServiceRepairID],[ServiceRepairName],[ServicePMPK],[ServicePMID],[ServicePMName],[Meter1RollDown],[Meter2RollDown],[Meter1RollDownMethod],[Meter2RollDownMethod],[Meter1ReadingLife],[Meter2ReadingLife],[IsLinear],[UDFDate3],[UDFDate4],[UDFDate5],[UDFBit3],[UDFBit4],[UDFBit5],[OrigPKforCloning],[PMCycleStartBy],[PMCycleStartByDesc],[PMCycleStartDate],[PMCounter],[ModelNumber],[ModelNumberMFG],[ModelLine],[ModelLineDesc],[ModelSeries],[ModelSeriesDesc],[SystemPlatform],[SystemPlatformDesc],[ResourceForScheduling],[RotatingPartPK],[RotatingPartID],[RotatingPartName],[RotatingRepairCenterPK],[RotatingLocationPK],[RotatingLaborPK],[GISLayer],[GISKeyValue],[GISTable],[ReceiveDate],[Latitude],[Longitude],[AssetFromPK],[AssetFromID],[AssetFromName],[AssetToPK],[AssetToID],[AssetToName],[FormName],[MaintainableToolPK],[MaintainableToolID],[MaintainableToolName],[MaintainableToolLocationPK],[RiskLevelAutoCalc],[PMRequiredAutoCalc],[RotatingBin],[AssetUUID],[RequireFinalReading],[apsSyncGUID],[RequireInitialReading],[apsRequirePhotos],[apsIconDefinitionPK],[apsRiskFactor1PK],[apsRiskFactor2PK],[apsRiskFactor3PK],[apsRiskFactor4PK],[apsRiskFactor5PK]
FROM entOldDatabase.dbo.Asset
WHERE AssetPK =1234;
SET IDENTITY_INSERT Asset OFF;Step 2: Insert Children Assets in Asset table
USE entCustomer
SET IDENTITY_INSERT Asset ON;
WITH AssetTree AS
(
SELECT ah.*
FROM entOldDatabase.dbo.AssetHierarchy ah
WHERE ah.ParentPK = 1234
UNION ALL
SELECT ah.*
FROM entOldDatabase.dbo.AssetHierarchy ah
JOIN AssetTree at ON ah.ParentPK = at.AssetPK
)
INSERT INTO entCustomer.dbo.ASSET ([AssetPK],[AssetID],[AssetName],[InternalAssetNum],[IsLocation],[IsClone],[RequesterCanView],[IsUp],[Status],[StatusDesc],[Type],[TypeDesc],[ClassificationPK],[ClassificationID],[ClassificationName],[Model],[Serial],[System],[SystemDesc],[OperatorPK],[OperatorID],[OperatorName],[DepartmentPK],[DepartmentID],[DepartmentName],[TenantPK],[TenantID],[TenantName],[Vicinity],[AccountPK],[AccountID],[AccountName],[GenLedger],[RepairCenterPK],[RepairCenterID],[RepairCenterName],[ShopPK],[ShopID],[ShopName],[Priority],[PriorityDesc],[VendorPK],[VendorID],[VendorName],[ManufacturerPK],[ManufacturerID],[ManufacturerName],[WarrantyExpire],[ContactPK],[ContactID],[ContactName],[Address],[City],[State],[Zip],[Country],[DisplayMapOnWO],[PurchasedDate],[PurchaseOrder],[PurchaseCost],[CurrentValue],[InstallDate],[StartupDate],[ReplaceDate],[ReplacementCost],[Life],[DisposalDate],[LicenseNumber],[Instructions],[InstructionsToWO],[InsuranceCarrier],[InsurancePolicy],[LeaseNumber],[RegistrationDate],[xcoordinate],[ycoordinate],[zcoordinate],[parcelid],[IsPredictive],[Technology],[TechnologyDesc],[IsMeter],[Meter1Reading],[Meter1Units],[Meter1UnitsDesc],[Meter1TrackHistory],[Meter2Reading],[Meter2Units],[Meter2UnitsDesc],[Meter2TrackHistory],[LastMaintained],[LastMaintained_WOPK],[Icon],[Photo],[UDFChar1],[UDFChar2],[UDFChar3],[UDFChar4],[UDFChar5],[UDFChar6],[UDFChar7],[UDFChar8],[UDFChar9],[UDFChar10],[UDFDate1],[UDFDate2],[UDFBit1],[UDFBit2],[DemoLaborPK],[RowVersionIPAddress],[RowVersionUserPK],[RowVersionInitials],[RowVersionAction],[RowVersionDate],[PurchaseType],[PurchaseTypeDesc],[DrawingUpdatesNeeded],[DisplayOrder],[ClassIndustry],[ClassIndustryDesc],[RiskAssessmentRequired],[AssessedByPK],[AssessedByID],[AssessedByName],[LastAssessed],[RiskAssessmentGroup],[RiskAssessmentGroupDesc],[RiskFactor1],[RiskFactor1Score],[RiskFactor2],[RiskFactor2Score],[RiskFactor3],[RiskFactor3Score],[RiskFactor4],[RiskFactor4Score],[RiskFactor5],[RiskFactor5Score],[RiskScore],[RiskLevel],[PMRequired],[PlanForImprovement],[HIPPARelated],[StatementOfConditions],[StatementOfConditionsCompliant],[ZonePK],[ZoneID],[ZoneName],[County],[YearBuilt],[MajorRenovations],[SquareFootage],[ConstructionCode],[ConstructionCodeDesc],[NumberOfStories],[ISOProtection],[ISOProtectionDesc],[AutoSprinkler],[AutoSprinklerDesc],[SmokeAlarm],[SmokeAlarmDesc],[HeatAlarm],[HeatAlarmDesc],[FloodZone],[QuakeZone],[Ext100Feet],[OperatingUnits],[EstimatedValue],[ResponsibilityRepairPK],[ResponsibilityRepairID],[ResponsibilityRepairName],[ResponsibilityPMPK],[ResponsibilityPMID],[ResponsibilityPMName],[ResponsibilitySafetyPK],[ResponsibilitySafetyID],[ResponsibilitySafetyName],[ServiceRepairPK],[ServiceRepairID],[ServiceRepairName],[ServicePMPK],[ServicePMID],[ServicePMName],[Meter1RollDown],[Meter2RollDown],[Meter1RollDownMethod],[Meter2RollDownMethod],[Meter1ReadingLife],[Meter2ReadingLife],[IsLinear],[UDFDate3],[UDFDate4],[UDFDate5],[UDFBit3],[UDFBit4],[UDFBit5],[OrigPKforCloning],[PMCycleStartBy],[PMCycleStartByDesc],[PMCycleStartDate],[PMCounter],[ModelNumber],[ModelNumberMFG],[ModelLine],[ModelLineDesc],[ModelSeries],[ModelSeriesDesc],[SystemPlatform],[SystemPlatformDesc],[ResourceForScheduling],[RotatingPartPK],[RotatingPartID],[RotatingPartName],[RotatingRepairCenterPK],[RotatingLocationPK],[RotatingLaborPK],[GISLayer],[GISKeyValue],[GISTable],[ReceiveDate],[Latitude],[Longitude],[AssetFromPK],[AssetFromID],[AssetFromName],[AssetToPK],[AssetToID],[AssetToName],[FormName],[MaintainableToolPK],[MaintainableToolID],[MaintainableToolName],[MaintainableToolLocationPK],[RiskLevelAutoCalc],[PMRequiredAutoCalc],[RotatingBin],[AssetUUID],[RequireFinalReading],[apsSyncGUID],[RequireInitialReading],[apsRequirePhotos],[apsIconDefinitionPK],[apsRiskFactor1PK],[apsRiskFactor2PK],[apsRiskFactor3PK],[apsRiskFactor4PK],[apsRiskFactor5PK])
SELECT DISTINCT [AssetPK],[AssetID],[AssetName],[InternalAssetNum],[IsLocation],[IsClone],[RequesterCanView],[IsUp],[Status],[StatusDesc],[Type],[TypeDesc],[ClassificationPK],[ClassificationID],[ClassificationName],[Model],[Serial],[System],[SystemDesc],[OperatorPK],[OperatorID],[OperatorName],[DepartmentPK],[DepartmentID],[DepartmentName],[TenantPK],[TenantID],[TenantName],[Vicinity],[AccountPK],[AccountID],[AccountName],[GenLedger],[RepairCenterPK],[RepairCenterID],[RepairCenterName],[ShopPK],[ShopID],[ShopName],[Priority],[PriorityDesc],[VendorPK],[VendorID],[VendorName],[ManufacturerPK],[ManufacturerID],[ManufacturerName],[WarrantyExpire],[ContactPK],[ContactID],[ContactName],[Address],[City],[State],[Zip],[Country],[DisplayMapOnWO],[PurchasedDate],[PurchaseOrder],[PurchaseCost],[CurrentValue],[InstallDate],[StartupDate],[ReplaceDate],[ReplacementCost],[Life],[DisposalDate],[LicenseNumber],[Instructions],[InstructionsToWO],[InsuranceCarrier],[InsurancePolicy],[LeaseNumber],[RegistrationDate],[xcoordinate],[ycoordinate],[zcoordinate],[parcelid],[IsPredictive],[Technology],[TechnologyDesc],[IsMeter],[Meter1Reading],[Meter1Units],[Meter1UnitsDesc],[Meter1TrackHistory],[Meter2Reading],[Meter2Units],[Meter2UnitsDesc],[Meter2TrackHistory],[LastMaintained],[LastMaintained_WOPK],[Icon],[Photo],[UDFChar1],[UDFChar2],[UDFChar3],[UDFChar4],[UDFChar5],[UDFChar6],[UDFChar7],[UDFChar8],[UDFChar9],[UDFChar10],[UDFDate1],[UDFDate2],[UDFBit1],[UDFBit2],[DemoLaborPK],[RowVersionIPAddress],[RowVersionUserPK],[RowVersionInitials],[RowVersionAction],[RowVersionDate],[PurchaseType],[PurchaseTypeDesc],[DrawingUpdatesNeeded],[DisplayOrder],[ClassIndustry],[ClassIndustryDesc],[RiskAssessmentRequired],[AssessedByPK],[AssessedByID],[AssessedByName],[LastAssessed],[RiskAssessmentGroup],[RiskAssessmentGroupDesc],[RiskFactor1],[RiskFactor1Score],[RiskFactor2],[RiskFactor2Score],[RiskFactor3],[RiskFactor3Score],[RiskFactor4],[RiskFactor4Score],[RiskFactor5],[RiskFactor5Score],[RiskScore],[RiskLevel],[PMRequired],[PlanForImprovement],[HIPPARelated],[StatementOfConditions],[StatementOfConditionsCompliant],[ZonePK],[ZoneID],[ZoneName],[County],[YearBuilt],[MajorRenovations],[SquareFootage],[ConstructionCode],[ConstructionCodeDesc],[NumberOfStories],[ISOProtection],[ISOProtectionDesc],[AutoSprinkler],[AutoSprinklerDesc],[SmokeAlarm],[SmokeAlarmDesc],[HeatAlarm],[HeatAlarmDesc],[FloodZone],[QuakeZone],[Ext100Feet],[OperatingUnits],[EstimatedValue],[ResponsibilityRepairPK],[ResponsibilityRepairID],[ResponsibilityRepairName],[ResponsibilityPMPK],[ResponsibilityPMID],[ResponsibilityPMName],[ResponsibilitySafetyPK],[ResponsibilitySafetyID],[ResponsibilitySafetyName],[ServiceRepairPK],[ServiceRepairID],[ServiceRepairName],[ServicePMPK],[ServicePMID],[ServicePMName],[Meter1RollDown],[Meter2RollDown],[Meter1RollDownMethod],[Meter2RollDownMethod],[Meter1ReadingLife],[Meter2ReadingLife],[IsLinear],[UDFDate3],[UDFDate4],[UDFDate5],[UDFBit3],[UDFBit4],[UDFBit5],[OrigPKforCloning],[PMCycleStartBy],[PMCycleStartByDesc],[PMCycleStartDate],[PMCounter],[ModelNumber],[ModelNumberMFG],[ModelLine],[ModelLineDesc],[ModelSeries],[ModelSeriesDesc],[SystemPlatform],[SystemPlatformDesc],[ResourceForScheduling],[RotatingPartPK],[RotatingPartID],[RotatingPartName],[RotatingRepairCenterPK],[RotatingLocationPK],[RotatingLaborPK],[GISLayer],[GISKeyValue],[GISTable],[ReceiveDate],[Latitude],[Longitude],[AssetFromPK],[AssetFromID],[AssetFromName],[AssetToPK],[AssetToID],[AssetToName],[FormName],[MaintainableToolPK],[MaintainableToolID],[MaintainableToolName],[MaintainableToolLocationPK],[RiskLevelAutoCalc],[PMRequiredAutoCalc],[RotatingBin],[AssetUUID],[RequireFinalReading],[apsSyncGUID],[RequireInitialReading],[apsRequirePhotos],[apsIconDefinitionPK],[apsRiskFactor1PK],[apsRiskFactor2PK],[apsRiskFactor3PK],[apsRiskFactor4PK],[apsRiskFactor5PK]
FROM entOldDatabase.dbo.Asset
WHERE AssetPK IN (SELECT AssetPK FROM AssetTree);
SET IDENTITY_INSERT Asset OFF;Step 3: Insert Parent/Children Assets in AssetHierarchy table
WITH AssetTree AS
(
-- Anchor: include the root asset itself (1234)
SELECT ah.*
FROM entOldDatabase.dbo.AssetHierarchy ah
WHERE ah.AssetPK = 1234
UNION ALL
-- Recursive: children, grandchildren, etc.
SELECT ah.*
FROM entOldDatabase.dbo.AssetHierarchy ah
JOIN AssetTree at ON ah.ParentPK = at.AssetPK
)
INSERT INTO entCustomer.dbo.AssetHierarchy ([System],[AssetPK],[ParentPK],[AssetLevel],[HasChildren],[OrderAlpha],[OrderType],[Location1PK],[Location1ID],[Location1Name],[Location2PK],[Location2ID],[Location2Name],[Location3PK],[Location3ID],[Location3Name],[Location4PK],[Location4ID],[Location4Name],[Location5PK],[Location5ID],[Location5Name],[Location6PK],[Location6ID],[Location6Name],[Location7PK],[Location7ID],[Location7Name],[Location8PK],[Location8ID],[Location8Name],[ParentLocation],[ParentLocationAll],[ParentEquipment],[ParentEquipmentAll],[IsOpenWO],[IsOpenWOChild],[IsOpenPD],[IsOpenPDChild],[Dirty],[DirtyBelow],[MCeDeterminedBad],[apsSyncGUID])
SELECT [System],[AssetPK],[ParentPK],[AssetLevel],[HasChildren],[OrderAlpha],[OrderType],[Location1PK],[Location1ID],[Location1Name],[Location2PK],[Location2ID],[Location2Name],[Location3PK],[Location3ID],[Location3Name],[Location4PK],[Location4ID],[Location4Name],[Location5PK],[Location5ID],[Location5Name],[Location6PK],[Location6ID],[Location6Name],[Location7PK],[Location7ID],[Location7Name],[Location8PK],[Location8ID],[Location8Name],[ParentLocation],[ParentLocationAll],[ParentEquipment],[ParentEquipmentAll],[IsOpenWO],[IsOpenWOChild],[IsOpenPD],[IsOpenPDChild],[Dirty],[DirtyBelow],[MCeDeterminedBad],[apsSyncGUID]
FROM AssetTreeStep 4: Add missing records in PMAsset table
USE entCustomer
SET IDENTITY_INSERT PMAsset ON;
WITH AssetTree AS
(
-- Anchor: include the root asset itself (1234)
SELECT ah.*
FROM entOldDatabase.dbo.AssetHierarchy ah
WHERE ah.AssetPK = 1234
UNION ALL
-- Recursive: children, grandchildren, etc.
SELECT ah.*
FROM entOldDatabase.dbo.AssetHierarchy ah
JOIN AssetTree at ON ah.ParentPK = at.AssetPK
)
INSERT INTO entCustomer.dbo.PMAsset ([PK],[PMPK],[AssetPK],[RouteOrder],[PMCounter],[TimesCounter],[PMEnded],[TimesDone],[LastGeneratedDate],[LastCompletedDate],[LastProcedurePK],[NextScheduledDate],[NextProcedurePK],[Meter1ReadingLastInterval],[Meter1NextInterval],[Meter2ReadingLastInterval],[Meter2NextInterval],[RepairCenterPK],[RepairCenterID],[RepairCenterName],[ShopPK],[ShopID],[ShopName],[StockRoomPK],[StockRoomID],[StockRoomName],[ToolRoomPK],[ToolRoomID],[ToolRoomName],[SupervisorPK],[SupervisorID],[SupervisorName],[AccountPK],[AccountID],[AccountName],[DepartmentPK],[DepartmentID],[DepartmentName],[TenantPK],[TenantID],[TenantName],[ProjectPK],[ProjectID],[ProjectName],[GenerateMarker],[DemoLaborPK],[RowVersionIPAddress],[RowVersionUserPK],[RowVersionInitials],[RowVersionDate],[WOPK],[IsOpenWO],[ScheduleDisabled],[PMCycleStartDate],[PMCycleStartDateCalc],[ShiftPK],[ShiftID],[ShiftName],[AutomationDaysPrior],[apsSyncGUID])
SELECT p.[PK], p.[PMPK], p.[AssetPK], p.[RouteOrder], p.[PMCounter], p.[TimesCounter], p.[PMEnded], p.[TimesDone], p.[LastGeneratedDate], p.[LastCompletedDate], p.[LastProcedurePK], p.[NextScheduledDate], p.[NextProcedurePK], p.[Meter1ReadingLastInterval], p.[Meter1NextInterval], p.[Meter2ReadingLastInterval], p.[Meter2NextInterval], p.[RepairCenterPK], p.[RepairCenterID], p.[RepairCenterName], p.[ShopPK], p.[ShopID], p.[ShopName], p.[StockRoomPK], p.[StockRoomID], p.[StockRoomName], p.[ToolRoomPK], p.[ToolRoomID], p.[ToolRoomName], p.[SupervisorPK], p.[SupervisorID], p.[SupervisorName], p.[AccountPK], p.[AccountID], p.[AccountName], p.[DepartmentPK], p.[DepartmentID], p.[DepartmentName], p.[TenantPK], p.[TenantID], p.[TenantName], p.[ProjectPK], p.[ProjectID], p.[ProjectName], p.[GenerateMarker], p.[DemoLaborPK], p.[RowVersionIPAddress], p.[RowVersionUserPK], p.[RowVersionInitials], p.[RowVersionDate], p.[WOPK], p.[IsOpenWO], p.[ScheduleDisabled], p.[PMCycleStartDate], p.[PMCycleStartDateCalc], p.[ShiftPK], p.[ShiftID], p.[ShiftName], p.[AutomationDaysPrior], p.[apsSyncGUID]
FROM entOldDatabase.dbo.PMAsset p
WHERE p.AssetPK IN (SELECT AssetPK FROM AssetTree)
AND NOT EXISTS (SELECT 1 FROM entCustomer.dbo.PMAsset n WHERE n.PK = p.PK );
SET IDENTITY_INSERT PMAsset OFF;