sql - duplicates
I'm putting data from website (json) to sql base. In db i have these rows.
ID | PostId | Name | Message
Id开发者_运维知识库 is auto-increment primary key. PostId also has a unique values. Name and Message are nothing special.
When I run my script / click on the button in form / ... , the program saves all the values into database (lets say there are 25). Next time I'm going to press the button there will be added 25 more records (all duplicates), and so on...
Is there a way that the program can check through 'PostIds' if the value already exists before adding it to the db?
Thanks
Another way is to use merge statement, this can also update the duplicate rows if you like and is easier than useing an if statement
if not exists(select * from table where PostId =@PostId)
Begin
//add
End
You have many option
Simple one is that you ask Your data base about that PostId
SELECT count(PostId) FROM Table where PostId = @PostId;
精彩评论