I want to throw an exception if cardescription is in table in row with specific @idCar
cardescription is not PK, and any change in design is not allowed
insert into carsdescription
(description)
value
(@description,@idCar)
Examples:
IDCar | Description
---------------------
1 | nice - this record was in table before.
1 | nice - should throw exception
2 | nice - it is allowed
Carsdescription table:
- ID (PK, int, autoincrement)
- idcar(FK, int)
- Description(varchar)
Cars table:
- Id(int, PK, autoincrement)
- Name(nvarchar(255)
You should create a unique index over IDCar and Description. Then your Database will prevent inserting duplicate data. Your CommandObject will then throw an exception.
But to be nice to your users you should first lookup in your table if a duplicate entry would be insterted.
EDIT:
Fore some reasons schema changes are not allowed. This means, your Database is unable to protected you against duplicate entries. So, as Matthew pointed out correctly, you have to: