To insert it into a temp table, just insert it into a temp table instead of into dbo.details. once that is done, to put the values from the temp table into details excluding duplicates, just have a where clause on your insert.
CREATE TABLE #tmpTable (DetailID INT, DetailName VARCHAR(MAX))
insert into #tmpTable (DetailID, DetailName)
<your code from above>
INSERT INTO dbo.details (DetailID, DetailName)
SELECT DetailID, DetailName FROM #tmpTable
WHERE #tmpTable.DetailID not in (select detailID from dbo.details)
With your code, I would recommend removing the "DISTINCT" as it is not needed. It is already going to be a distinct list.