May 20, 2008 at 1:53 am
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
May 20, 2008 at 2:06 am
Try this
insert into empcheck1
select * from emp
where emp.empid not in(select empid from empcheck1)
- Zahran -
May 20, 2008 at 3:06 am
thank you, works
May 20, 2008 at 7:15 am
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
May 20, 2008 at 7:19 am
thanks samanta
May 20, 2008 at 4:20 pm
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.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
May 20, 2008 at 5:31 pm
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
Change is inevitable... Change for the better is not.
May 20, 2008 at 5:39 pm
What about NOT EXISTS?
_____________
Code for TallyGenerator
May 20, 2008 at 9:43 pm
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.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
May 20, 2008 at 9:54 pm
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
Change is inevitable... Change for the better is not.
May 20, 2008 at 10:37 pm
Jeff Moden (5/20/2008)
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.
These 2 queries:
select * from Tally
where N NOT IN (select N from Tally)
select * from Tally T1
where NOT exists (select 1 from Tally T2
where T1.N = T2.N)
generate identical execution plans.
So, there is no any kind of hidden RBAR.
From my experience NOT EXISTS beats NOT IN when:
1) list IN is too big (does not fit into one page);
2) you need to match values in more than 1 column.
_____________
Code for TallyGenerator
June 20, 2008 at 3:35 pm
Hi,
I have same scenario, but with some addional option.
I have data in "temptable" and data in "originaltable".
What I want to do, I will insert all the new rows from "temptable" to "originaltable" which "originaltable" don't have. I can manager this by solution you provided.
BUT
addionally I want to update existing rows in "originaltable" table which "temptable" also have. update values with the values of "temptable". How can I achieve this part.
at the end, I will delete all from "temptable". So next time I can fill it again, and start new insert/update process.
Let me know, if you people need more details.
Waiting for response.
Regards,
Azam.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply