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 2005 Forums
 Transact-SQL (2005)
 Insert Into Record with Record Count

Author  Topic 

hwood125
Starting Member

12 Posts

Posted - 2008-08-25 : 10:07:01
I am wondering what the best way would be to count the number of records that gets inserted into a table A from a variable table B that are not already in table A? The whole point is to avoid duplicates. I know primary keys would help but the primary keys get created based off of the date of the text file that gets inserted. So there is always a possibility of having duplicated records going through the text files. There are other steps that I would like to implement after this step depending wether the count is <> 0 is the ticket for now.

Thanks!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-25 : 10:11:07
didnt get the scenario fully. Will be inserting from text file or anothertable? first sentence says insert from table b then you say its from text files. Can you clarify?
Go to Top of Page

hwood125
Starting Member

12 Posts

Posted - 2008-08-25 : 10:14:39
I will be inserting from a temp variable table into a table.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-25 : 10:14:53
Make use of @@ROWCOUNT

Also, this forum is to post workable script and not to ask questions

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

hwood125
Starting Member

12 Posts

Posted - 2008-08-25 : 10:18:43
I thought forums were to help people. The whole point of me posting my topic is to get help because I DIDN'T KNOW a workable script to use in this situation.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-08-25 : 10:25:15
true. but this specific forum is for posting scripts that work and are helpfull to other people.
that's why i'm moving this from script library.

_______________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.0 out!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-25 : 10:27:42
quote:
Originally posted by hwood125

I thought forums were to help people. The whole point of me posting my topic is to get help because I DIDN'T KNOW a workable script to use in this situation.


You can post it no problem but not HERE...What Madhi told was not to post in this forum which is for scripts. you've other specific forums here like sql 2005,sql 2000,... where you can post. this forum is for posting scripts only.
Now your questions answer
If you want to avoid duplicates just check if combination already exists before insertion like below

INSERT INTO TableA
SELECT fields
FROM @TableVariable t
LEFT JOIN TableA a
ON a.uniquefieldcombination=t.uniquefieldcombination
WHERE a.uniquefieldcombination IS NULL
Go to Top of Page
   

- Advertisement -