0

我有一个有 5 列的 sql 视图。我希望将数据写入一个表但只有 2 列,

看法

id 
car1
car2
car3
car4

桌子

id
cars

INSERT INTO table 
SELECT * from view
4

3 回答 3

2
insert cars (id, car)
select id, cars
from view
unpivot
(cars for car in (car1, car2,car3, car4)) as u
于 2012-08-03T15:37:02.910 回答
2

蛮力和无知(但可靠):

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 具有消除任何重复项的优点。

于 2012-08-03T15:37:40.430 回答
1

你试过这个吗?

INSERT INTO table SELECT ID, Car1 + ',' + Car2 + ',' + Car3 + ',' + Car4 as Cars from view

于 2012-08-03T15:36:41.833 回答