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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Insert problem

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 second
declare @Table1 table
(
id int identity(1,1),
EMPID int,
EMPName varchar(50)
)
--Insert sample data
Insert into @Table1
select 1,'sk' union all
select 2, 'pk' union all
select 3,'mk' union all
select 4 ,'lk' union all
select 5,'ok'
select * from @Table1

--create table with less number of columns
declare @Table2 table
(
id int identity(1,1),
EMPID int

)
--insert sample data
Insert into @Table2
select 3 union all
select 7 union all
select 8 union all
select 10 union all
select 11

--Insert data from table2 to table 1
Insert into @Table1(EMPID)
select EMPID from @Table2
--View data from table1 after insertion from table2
select * from @Table1
Go to Top of Page

cognos79
Posting Yak Master

241 Posts

Posted - 2008-02-08 : 10:29:18
Insert into Table2
Select table1.col1, table1.col2, table1.col3
,null,null,null
from table1
Go to Top of Page
   

- Advertisement -