Click here to Skip to main content
14,242,862 members
Rate this:
Please Sign up or sign in to vote.
See more:

Iam using C# and Sql Server 2005 .
I have temp table from which all the data will be saved to master table.

I have to check whether all the data has copied from temp to master.
if not i have delete that particular data and reinsert again in master

i have written for one time checking.
But i don't think that's right way becoz for first time if data not properly inserted then i will compare and reinsert second time but after that also i need check for integrity

Pls help me how to solve this

Nirmala Saravanan
ZurdoDev 21-Apr-14 8:38am
I don't understand where you are stuck?
King Fisher 21-Apr-14 9:14am
What you have tried?
ALLAMI Said 21-Apr-14 12:24pm
can you add a peace of code to understand more where you're stuck
Rate this:
Please Sign up or sign in to vote.

Solution 2

Once you have completed your insert, but before disposing of the temporary table you could try using something like
SELECT COUNT(*) FROM [temp_table] WHERE ComboKey NOT IN [master_table]

Where the ComboKey is some combination of key values that will uniquely identify the record you have tried to insert.

You are expecting a value of 0 to be returned. Any non-zero return indicates that you didn't insert everything.

Overall you might be better off using transactions ... have a look at SQL Server Transactions and Error Handling[^]
Rate this:
Please Sign up or sign in to vote.

Solution 1

before inserting in master table get the count of rows and keep it in a variable.

then in another variable keep the count of temparary table rows.

after inserting check the count of master table

before count+ temptablecount

you can verify ,
if didnt match delete the rows..
CHill60 23-Apr-14 7:44am
And what if this is a multi-user database?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100