[英]sql - duplicates
I'm putting data from website (json) to sql base. 我将数据从网站(json)到sql base。 In db i have these rows.
在数据库中,我有这些行。
ID | PostId | Name | Message
Id is auto-increment primary key. Id是自动递增的主键。 PostId also has a unique values.
PostId也具有唯一值。 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). 当我运行脚本/单击表单...中的按钮时,该程序会将所有值保存到数据库中(假设有25个)。 Next time I'm going to press the button there will be added 25 more records (all duplicates), and so on...
下次我要按按钮时,将再添加25条记录(所有重复项),依此类推...
Is there a way that the program can check through 'PostIds' if the value already exists before adding it to the db? 程序有没有办法在将值添加到数据库之前检查“ PostIds”是否已经存在?
Thanks 谢谢
另一种方法是使用merge语句,如果愿意,它也可以更新重复的行,并且比使用if语句更容易
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
简单的一
PostId
是,您向数据库查询有关该PostId
SELECT count(PostId) FROM Table where PostId = @PostId;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.