checking fo duplicates

hi
i am trying to insert multiple data into a table.i need to check duplicates before insertion.can anyone plse help me to chck this
[142 byte] By [getnitha] at [2007-11-20 9:19:58]
# 1 Re: checking fo duplicates
You did not mention what kind of database you are using. So, just assuming SQL Server is used.

There are three ways to check duplicate:
1. add constraints into the table to avoid duplicate;
2. use stored procedure to inset data. In SP, check if the record exists already, if not, insert it.
3. use SqlDataAdapter to populate data into dataset, check duplicate and insert new records into dataset, then update.

Hope one of them helps.
jasonli at 2007-11-10 3:30:05 >
# 2 Re: checking fo duplicates
thanks for ur help

i am using sql server 2005 as db.i need to check in my code itself for duplicates and insert data acccordingly.i am inserting multiple records at a time.can anyone plse help me


You did not mention what kind of database you are using. So, just assuming SQL Server is used.

There are three ways to check duplicate:
1. add constraints into the table to avoid duplicate;
2. use stored procedure to inset data. In SP, check if the record exists already, if not, insert it.
3. use SqlDataAdapter to populate data into dataset, check duplicate and insert new records into dataset, then update.

Hope one of them helps.
getnitha at 2007-11-10 3:30:58 >
# 3 Re: checking fo duplicates
Did you do what Jason suggested. The best way to avoid Duplicates is to add constraints to the Table itself. This way you don't need to write extra code for checking duplicates.

By the way, what do you mean by duplicate data?
Shuja Ali at 2007-11-10 3:31:58 >
# 4 Re: checking fo duplicates
hi
i will gve u more details abt my problem.on page load itself i need to insert data from one table into another table.some times same data will be present in both the tables then no need to insert that data.but in some cases First table will be hvng some records which is not present in secnd table.so in his situation i need to insert that particular record only.below i pasted my code.........plse get me some solution its very urgent



<code>

Try
Dim cmdDs As New DataSet
Dim countCmd As New SqlCommand
Dim cmdCount As SqlDataAdapter
Dim darold As DataRow

countCmd = New SqlCommand("Select USER_CREATION.User_ID,USER_CREATION.User_Name,T_TNA_LOG.N_EVENT,T_TNA_LOG.D_EVENT_DATE from T_TNA_LOG,USER_CREATION where T_TNA_LOG.S_USER_ID = USER_CREATION.User_ID ", Conn12)
cmdCount = New SqlDataAdapter(countCmd)
cmdCount.Fill(cmdDs)

For Each darold In cmdDs.Tables(0).Rows
Select Case (darold(2))

Case 16
TextBox5.Text = darold(3).ToString().Substring(10, 8)
Case 18
TextBox7.Text = darold(3).ToString().Substring(10, 8)
Case 19
TextBox8.Text = darold(3).ToString().Substring(10, 8)
Case 17
TextBox6.Text = darold(3).ToString().Substring(10, 8)

Dim cmdSql As New SqlCommand
Conn1.ConnectionString = connString
Conn1.Open()

sql1 = "insert into ATTENDENCE_REPORT(User_ID,User_Name,Punch_Date,Time_In,Break_Out,Break_In,Time_Out,Delay_Time,Over_Time,Actual_Hour,Total_Late_Hour,Total_Time,Remarks) values ('" + darold(0) + "','" + darold(1) + "','" + darold(3).ToString().Substring(0, 10) + "','" + TextBox5.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox6.Text + "','" + "" + "','" + "" + "','" + "" + "','" + "" + "','" + "" + "','" + "')"

cmdSql = New SqlCommand(sql1, Conn1)
cmdSql.ExecuteNonQuery()

Conn1.Close()
End Select
Next
Catch excp As Exception
End Try

</code>
getnitha at 2007-11-10 3:33:03 >
# 5 Re: checking fo duplicates
Show us the structure of two tables. Are they same. Inserting data can be done using a simple query.
Shuja Ali at 2007-11-10 3:34:05 >
# 6 Re: checking fo duplicates
table structures are different but in both tables i got a common field which is primary key of its own..............so i just want to check whether any duplicte records(i mean both tables hvng same record) then ignore it else insert new records.for eg i m trying to insert 4 records from first table into second table.say first field values as a,b,c,d

user_name

a
b
c
d

then i need to check whether "a" is present in second table.if "a" is not thre i need to insert it
then i need to check whether "b" i is present in second table.if "b" is not thre i need to insert it
like wise loop continues

hope u got my problem
getnitha at 2007-11-10 3:35:04 >
# 7 Re: checking fo duplicates
Here is a simple query that will do the insert. As you have not provided the actual table structure, I am just writing a general query that you can use Insert Into SECONDTABLE (COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5) Select COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5 From FIRSTTABLE Where PRIMARKEYFIELD Not In (Select PRIMARYKEYFEILD From SECONDTABLE)
Shuja Ali at 2007-11-10 3:36:01 >
# 8 Re: checking fo duplicates
thks for ur help.
i got it.now its working properly
getnitha at 2007-11-10 3:37:04 >