我有一个有 5 列的 sql 视图。我希望将数据写入一个表但只有 2 列,
看法
id
car1
car2
car3
car4
桌子
id
cars
INSERT INTO table
SELECT * from view
insert cars (id, car)
select id, cars
from view
unpivot
(cars for car in (car1, car2,car3, car4)) as u
蛮力和无知(但可靠):
INSERT INTO table
SELECT id, car1 FROM view WHERE car1 IS NOT NULL
UNION
SELECT id, car2 FROM view WHERE car2 IS NOT NULL
UNION
SELECT id, car3 FROM view WHERE car3 IS NOT NULL
UNION
SELECT id, car4 FROM view WHERE car4 IS NOT NULL
UNION 具有消除任何重复项的优点。
你试过这个吗?
INSERT INTO table SELECT ID, Car1 + ',' + Car2 + ',' + Car3 + ',' + Car4 as Cars from view