I have a list of Car IDs in a temp #CarIDs
CREATE TABLE #CarIDs(
[CARID] [nvarchar] (60) NULL,
[Type] [nvarchar] (20) NULL,
[Flag] [nvarchar] (30) NULL) GO
INSERT INTO #CarIDs (CARID, Type, Flag) VALUES ('1111','',''), ('2222','',''), ('3333','',''), ('4444','',''), ('5555','',''), ('6666','','')
Which gives me SELECT * FROM #CarIDs
+-------+-------+--------+
| CARID | Type | Flag |
+-------+-------+--------+
| 1111 | | |
| 2222 | | |
| 3333 | | |
| 4444 | | |
| 5555 | | |
| 6666 | | |
+-------+-------+--------+
How do I loop through the below table (CarHierarchy) to find out what model type is each CARID then insert into the temp table?
+-------+-------+-------+
| Jeep |Holden | Ford |
+-------+-------+-------+
| 1111 |2222 | 3333 |
| 4444 |6666 | 5555 |
+-------+-------+-------+
I expect the results to be #CarIDs:
+-------+-------+
| CARID | Type |
+-------+-------+
| 1111 |Jeep |
| 2222 |Holden |
| 3333 |Ford |
| 4444 |Jeep |
| 5555 |Ford |
| 6666 |Holden |
+------+--------+
CarHierarchyseems like a deeply misguided design for a table. Data in a single row ought to be facts that are related to each other. data shouldn't be embedded in metadata as column names. What happens when there aren't equal numbers of each type of car?