select ID = row_number() over (partition by TABLENAME order by COLNAME), TABLENAME, COLNAMEfrom #temp;