sql 2000如何做到同一条码最多只能插入2条记录?

freesp 2011-04-13 09:50:51
不管产品是否合格,只允许同一条码最多插入两条记录。
怎么做可以解决?

NUMBER RESULT
123456 FAIL
123456 PASS


NUMBER RESULT
123456 FAIL
123456 FAIL

虽然以下两种情况不可能发生,但是万一做了,也得防止

NUMBER RESULT
123456 PASS
123456 PASS



NUMBER RESULT
123456 PASS
123456 FAIL


谢谢!

...全文
102 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
O爱咋咋地O 2011-04-13
  • 打赏
  • 举报
回复
插入先select
freesp 2011-04-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gogodiy 的回复:]
插入前判断下,根据你的需求,只有表中没有该条码才能插入,因为每次插入就是两条,也就是要么没有,要么只有2条。

SQL code

if not exists (select 1 from table where number=123456)
begin
insert语句
end
else
begin
return
end
[/Quote]

条码不是每次同时插入2条,而是只允许插2条,1条PASS后一般不会再测试(有时候多测一次也有可能的),但是FAIL后,肯定得再测一次,再次PASS或FAIL后都不允许插入。
gogodiy 2011-04-13
  • 打赏
  • 举报
回复
插入前判断下,根据你的需求,只有表中没有该条码才能插入,因为每次插入就是两条,也就是要么没有,要么只有2条。

if not exists (select 1 from table where number=123456)
begin
insert语句
end
else
begin
return
end
  • 打赏
  • 举报
回复
这个,用触发器来实现吧。或者在前台做也行
freesp 2011-04-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ssp2009 的回复:]
SQL code
if(select count(1) from tb where [NUMBER]=@NUMBER)<3
insert into tb select @NUMBER,'PASS'
[/Quote]

这个失败的记录无法插入了吗?
快溜 2011-04-13
  • 打赏
  • 举报
回复
if(select count(1) from tb where [NUMBER]=@NUMBER)<3
insert into tb select @NUMBER,'PASS'
AcHerat 元老 2011-04-13
  • 打赏
  • 举报
回复
做一个插入触发器,只允许插入出现次数小于两次的产品信息!
freesp 2011-04-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 gogodiy 的回复:]
那就改成这样:

SQL code

if (select count(*) from table where number=123456)>1
begin
return
end
else
begin
insert语句
end
[/Quote]

我的原先语句是
insert into table(number,result) values('%s','%s')
是在LABVIEW里面使用,如果加上你的这个,还是会不停的写入

但是在查询分析器里面是执行成功的。
gogodiy 2011-04-13
  • 打赏
  • 举报
回复
那就改成这样:

if (select count(*) from table where number=123456)>1
begin
return
end
else
begin
insert语句
end

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧