Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
MariaM
Starting Member
17 Posts |
Posted - 2008-02-08 : 03:32:30
|
Hi !I need some help again. It is probably a simple problem, but I can't find the solution right now.I want to insert values into a table by using select from another table. The table I want to insert into contains a few more columns than the original table though that I want to fill with null values.How do I do this ? Thank you in advance !/M |
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-02-08 : 03:48:07
|
I hope this is what you are looking for--Prepare table with more columns than seconddeclare @Table1 table( id int identity(1,1), EMPID int, EMPName varchar(50) )--Insert sample dataInsert into @Table1select 1,'sk' union allselect 2, 'pk' union allselect 3,'mk' union allselect 4 ,'lk' union allselect 5,'ok' select * from @Table1--create table with less number of columnsdeclare @Table2 table( id int identity(1,1), EMPID int )--insert sample dataInsert into @Table2select 3 union allselect 7 union allselect 8 union allselect 10 union allselect 11--Insert data from table2 to table 1Insert into @Table1(EMPID) select EMPID from @Table2--View data from table1 after insertion from table2select * from @Table1 |
 |
|
cognos79
Posting Yak Master
241 Posts |
Posted - 2008-02-08 : 10:29:18
|
Insert into Table2Select table1.col1, table1.col2, table1.col3 ,null,null,nullfrom table1 |
 |
|
|
|
|
|
|