我可以将任意数量和类型的列添加到临时表中,而无需先定义它们:
select into #temp from table;
但如果我想稍后在我的脚本中向这个临时表添加列,我知道的唯一方法是:
alter #temp add column int;
insert into #table (column) select column from table;
如果我想添加多个列,这有点麻烦。有没有办法在不先定义它们的情况下将列添加到临时表?
最佳答案
我认为 insert
在添加一列后不合适。 Update
看起来更像你想要的操作。
一种选择是创建一个新的临时表:
select t.*, 'value' as col
into #temp1
from #temp t;
但是,对于现有表,无法同时添加和填充列 - 除非提供默认值。
但是,您可以同时添加多个列:
alter #temp add col1 int, col2 int, col3 int;
update #temp t
set col1 = 1, col2 = 2, col3 = 3;
关于sql - 如何轻松地将列添加到临时表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36132639/