I need to do the select based upon the content (equipmentID) in #TempTable1 and bring the result into #TempTable2. I am not sure how to do the join. Can anyone help?
CREATE TABLE #TempTable1 (equipmentID1 nvarchar(20))
Fill #TempTable1 here .......
Now use records in #TempTable1 as basis for my select..... Tried below code, but gives me error: Incorrect syntax near the keyword 'on'.
INSERT INTO #TempTable2
SELECT equipmentID1
FROM #TempTable1
INNER JOIN
(SELECT
substring(dbo.Job.CompanyJobId, 1, 10) as job,
substring(dbo.Item.CompanyItemId, 1, 10) as costcode,
dbo.Batch.ReportDate as jobdate,
dbo.Equipment.CompanyEquipmentID as equipmentID,
substring(dbo.Equipment.Name, 1, 20) as equipmentname,
dbo.EquipmentLaborEvent.Hours as hours,
substring(dbo.EventStatusType.Name, 1, 1) as event_code,
dbo.EventStatusType.Name as event_name,
substring (dbo.Batch.Name, 1, charindex('-',dbo.Batch.Name)-1) as foreman,
substring (e2.UserName, 1, 10) as approver
FROM dbo.Equipment)
on dbo.Equipment.CompanyEquipmentID = equipmentID1
inner join EquipmentLaborEvent on dbo.EquipmentLaborEvent.EquipmentGuid = dbo.Equipment.EquipmentGuid
inner join dbo.Region on dbo.Region.RegionGuid = Equipment.RegionGuid
inner join dbo.Event on dbo.Event.EventGuid = EquipmentLaborEvent.EventGuid
inner join dbo.EventStatusType on dbo.EventStatusType.EventStatusTypeGuid = dbo.Event.EventStatus
inner join dbo.Job on dbo.Job.JobGuid = dbo.Event.JobGuid
inner join dbo.Item on dbo.Item.ItemGuid = dbo.Event.ItemGuid
inner join dbo.Batch on dbo.Batch.BatchGuid = dbo.Event.BatchGuid
left join dbo.Account e2 on e2.AccountGuid = dbo.Event.ApprovedByAccountGuid
inner join dbo.Equipment e3 on dbo.Equipment.CompanyEquipmentID = #TempTable1.equipmentID
WHERE dbo.Region.CompanyRegionID = '00060' and
dbo.Job.CompanyJobID = @JobNumber and
dbo.Batch.Reportdate >= '2009-05-14' and
dbo.Batch.ReportDate <= '2009-05-29' and
CREATE TABLE #TempTable1 (equipmentID1 nvarchar(20))
Fill #TempTable1 here .......
Now use records in #TempTable1 as basis for my select..... Tried below code, but gives me error: Incorrect syntax near the keyword 'on'.
INSERT INTO #TempTable2
SELECT equipmentID1
FROM #TempTable1
INNER JOIN
(SELECT
substring(dbo.Job.CompanyJobId, 1, 10) as job,
substring(dbo.Item.CompanyItemId, 1, 10) as costcode,
dbo.Batch.ReportDate as jobdate,
dbo.Equipment.CompanyEquipmentID as equipmentID,
substring(dbo.Equipment.Name, 1, 20) as equipmentname,
dbo.EquipmentLaborEvent.Hours as hours,
substring(dbo.EventStatusType.Name, 1, 1) as event_code,
dbo.EventStatusType.Name as event_name,
substring (dbo.Batch.Name, 1, charindex('-',dbo.Batch.Name)-1) as foreman,
substring (e2.UserName, 1, 10) as approver
FROM dbo.Equipment)
on dbo.Equipment.CompanyEquipmentID = equipmentID1
inner join EquipmentLaborEvent on dbo.EquipmentLaborEvent.EquipmentGuid = dbo.Equipment.EquipmentGuid
inner join dbo.Region on dbo.Region.RegionGuid = Equipment.RegionGuid
inner join dbo.Event on dbo.Event.EventGuid = EquipmentLaborEvent.EventGuid
inner join dbo.EventStatusType on dbo.EventStatusType.EventStatusTypeGuid = dbo.Event.EventStatus
inner join dbo.Job on dbo.Job.JobGuid = dbo.Event.JobGuid
inner join dbo.Item on dbo.Item.ItemGuid = dbo.Event.ItemGuid
inner join dbo.Batch on dbo.Batch.BatchGuid = dbo.Event.BatchGuid
left join dbo.Account e2 on e2.AccountGuid = dbo.Event.ApprovedByAccountGuid
inner join dbo.Equipment e3 on dbo.Equipment.CompanyEquipmentID = #TempTable1.equipmentID
WHERE dbo.Region.CompanyRegionID = '00060' and
dbo.Job.CompanyJobID = @JobNumber and
dbo.Batch.Reportdate >= '2009-05-14' and
dbo.Batch.ReportDate <= '2009-05-29' and