varun
varun

Reputation: 3

insert data in physical table from temp table with less column

I have one issue, I'm not too sure is it possible by anyway or not.

suppose- I have a physical table with 4 columns but I have to insert data into it from temp tables with 1000's of records but with less column (i.e. 2 columns) Is it possible by anyway.

I have added a temporary script to describe my problem-

create table A (id int,sal int,name varchar(50),data varchar(50))
create table #B (id int,sal int)
insert into #B values(1,10)
insert into #B values(2,20)
insert into #B values(3,30)
insert into #B values(4,40)

---This will Not work
insert into A select * from #B

Is there any other way we can do this, I have added just a scenario but i have a lot of columns in my physical table

Upvotes: 0

Views: 1795

Answers (2)

Ranjana Ghimire
Ranjana Ghimire

Reputation: 1815

    create table A (id int,sal int,name varchar(50),data varchar(50))
    create table #B (id int,sal int)
    insert into #B values(1,10)
    insert into #B values(2,20)
    insert into #B values(3,30)
    insert into #B values(4,40)

    -This will  work
    insert into A select *,null,null from #B    
    -- or    
    insert into A (id,sal) select * from #B

Upvotes: 2

shree.pat18
shree.pat18

Reputation: 21757

How about specifying the columns during insert i.e.

insert into A(id,sal) select * from #B

Upvotes: 1

Related Questions