2017-01-18 94 views
0

目前,我正在插入到表具有以下前缀也manaully设置我想要什么,在过去2列(1,GETDATE())选择*在INSERT语句中

Insert into [Table1]   
select col1,col2,col3, 1,getdate() 
from [table2] 

问题是存在的是列的负载,并导致在SP的混乱。

我试图重写语句...

Insert Into [table1] 
Select * from [Table2] 

但我也需要考虑到我想手动写入这2列..

是否有办法这样做?

非常感谢

+3

的可能的复制[?INSERT INTO SELECT \ *为SQL Server,不可能,我是正确](http://stackoverflow.com/questions/26886421/insert-into-select-for-sql-server-not-possible-am-i-correct) – mortb

+0

向我们展示这两个表的示例模式。 table1有两个比table2多的列吗? – GurV

+0

'... select table2。*,1,getdate()...' – jarlh

回答

0

这可能会帮助您解决问题:

Insert into [Table1]   
select col1, col2, col3, '1' as [col4] , getdate() as [col5] from [table2] 
+0

只是一个评论,不需要命名选定的列。 – jarlh

0

两点。首先,使用insert时,你应该始终处于命名列插入的习惯:

Insert into [Table1](col1, col2, col3, col4, col5)   
    select col1, col2, col3, 1, getdate() 
    from [table2]; 

其次,你不必把getdate()在插入。让数据库做的工作对你有一个默认值:

create table table1 . . . 
    col5 date default getdate() 
);