How to prevent adding a duplicate value into a table
-
OK, here is the deal. I've got a table with 2 columns: "ID"(int), "Name"(string). "ID" - is a unique key field used in relations with other tables, but the values of the "Name" should be unique as well. I need to add more data into this table, but not to create some duplicate information. I know about INSERT DISTINCT, but I am not using an SQL command, instead I use a VS generated TableAdapter (in my case it is named "companiesTableAdapter"). Here is a small code snippet: this.companiesTableAdapter.Insert("XYZ Company"); this code does insert the string into a table, but it doesn't care if "XYZ Company" is already there. (the DB itself is created in Access2003, if this may be helpful) If someone has done something similar, I would really appreciate their help.
-
OK, here is the deal. I've got a table with 2 columns: "ID"(int), "Name"(string). "ID" - is a unique key field used in relations with other tables, but the values of the "Name" should be unique as well. I need to add more data into this table, but not to create some duplicate information. I know about INSERT DISTINCT, but I am not using an SQL command, instead I use a VS generated TableAdapter (in my case it is named "companiesTableAdapter"). Here is a small code snippet: this.companiesTableAdapter.Insert("XYZ Company"); this code does insert the string into a table, but it doesn't care if "XYZ Company" is already there. (the DB itself is created in Access2003, if this may be helpful) If someone has done something similar, I would really appreciate their help.
First check if that name exists, in case if exists then don't insert otherwise do insert. example:
if(select count(name) from tablename where name='value')=0 begin insert into tablename values ('idvalue','namevalue') end
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you then save my time by voting my post.
-
First check if that name exists, in case if exists then don't insert otherwise do insert. example:
if(select count(name) from tablename where name='value')=0 begin insert into tablename values ('idvalue','namevalue') end
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you then save my time by voting my post.
Worked great! Thanks a lot
-
Worked great! Thanks a lot
-
OK, here is the deal. I've got a table with 2 columns: "ID"(int), "Name"(string). "ID" - is a unique key field used in relations with other tables, but the values of the "Name" should be unique as well. I need to add more data into this table, but not to create some duplicate information. I know about INSERT DISTINCT, but I am not using an SQL command, instead I use a VS generated TableAdapter (in my case it is named "companiesTableAdapter"). Here is a small code snippet: this.companiesTableAdapter.Insert("XYZ Company"); this code does insert the string into a table, but it doesn't care if "XYZ Company" is already there. (the DB itself is created in Access2003, if this may be helpful) If someone has done something similar, I would really appreciate their help.
-
How can I do that? (Add a unique numerical index to a column)
-
How can I do that? (Add a unique numerical index to a column)