SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Copy data from one table to another table


Copy data from one table to another table

Author
Message
cmrhema
cmrhema
Right there with Babe
Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)

Group: General Forum Members
Points: 760 Visits: 247
Hi,

I have two tables emp and empcheck1 in the same database

emp consits 50 records
and empcheck1 consists of 25 records

Both the tables have the same design, here empid is the unique key

I want to insert all the datas from emp1 to empcheck1 in such a way that it should not rewrite or duplicate the original 25 records

Whatever 25 records are there in empcheck1 already exists in emp

So i want the rest of the 25 odd records to be copied to empcheck1 from emp


Can anyone let me know how to do that
i tried out
insert into empcheck1 select * from emp where emp.empid<>empcheck1.empid

But it gave me an error
Zahran
Zahran
Right there with Babe
Right there with Babe (722 reputation)Right there with Babe (722 reputation)Right there with Babe (722 reputation)Right there with Babe (722 reputation)Right there with Babe (722 reputation)Right there with Babe (722 reputation)Right there with Babe (722 reputation)Right there with Babe (722 reputation)

Group: General Forum Members
Points: 722 Visits: 381
Try this

insert into empcheck1
select * from emp
where emp.empid not in(select empid from empcheck1)



- Zahran -
cmrhema
cmrhema
Right there with Babe
Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)

Group: General Forum Members
Points: 760 Visits: 247
thank you, works
Sam-638661
Sam-638661
Old Hand
Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)

Group: General Forum Members
Points: 373 Visits: 120
This is another way of the same workaround with better performance:

insert into empcheck1
select emp.*
from emp
left join empcheck1
on emp.empid=empcheck1.empid
where empcheck1.empid is null
cmrhema
cmrhema
Right there with Babe
Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)Right there with Babe (760 reputation)

Group: General Forum Members
Points: 760 Visits: 247
thanks samanta
RBarryYoung
RBarryYoung
SSC Guru
SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)

Group: General Forum Members
Points: 68032 Visits: 9519
samata.putumbaka (5/20/2008)
This is another way of the same workaround with better performance:

Interestingly, on SQL 2005, the first query is marginally faster.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)

Group: General Forum Members
Points: 427258 Visits: 43428
Same goes in 2000... in fact, our standards recommend trying the WHERE NOT IN before trying the OUTER JOIN fore exclusions of this nature.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Sergiy
Sergiy
SSC-Forever
SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)

Group: General Forum Members
Points: 49735 Visits: 12750
What about NOT EXISTS?
RBarryYoung
RBarryYoung
SSC Guru
SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)

Group: General Forum Members
Points: 68032 Visits: 9519
Sergiy (5/20/2008)
What about NOT EXISTS?

Exactly the same as NOT IN for this case in SQL 2005. In general that is what I usually see for SQL 2005.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)

Group: General Forum Members
Points: 427258 Visits: 43428
If I remember correctly, NOT EXISTS can actually be a bit faster in the presence of the correct index. Still, it uses correlation which is hidden RBAR.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search