【发布时间】:2017-03-28 15:37:23
【问题描述】:
我有一个表 table1,列名为 a、b、c、d、e、f。 现在的任务是获取每列的值,该值肯定是单行值,并将其插入到其他 table2 - columns(x, y, z) 中。所以我的查询是这样的:
insert into table2 (x, y, z)
select a, '', '' from table1
union all
select b, '', '' from table1
union all
select c, '', '' from table1
union all
select d, '', '' from table1
union all
select e, '', '' from table1
.
.
.
union all
select f, '', '' from table1
现在,如果在 table1 中添加了一个新列,那么我必须在其中添加一个 select 语句。只是想避免这种情况,我怎样才能编写一个自动考虑所有列并使其更短的动态查询。
【问题讨论】:
标签: sql sql-server dynamic