以后添加新值的时候,如果表中最后一条记录对应的A B C 的任意字段的值和上一条记录的差值大于等于其阀值的时候,则用新值新增一条记录。
否则用新值改写最后一条记录。
...全文
1599打赏收藏
这个存储过程怎么写?~!《在线急等,解决立刻給分》
表tbl_A中有三个字段 A B C 对A B C三个字段各有一个阀值 DA DB DC 添加新记录时,当表为空的时候,先添一条(0,0,0)的记录,直接添加新记录。 以后添加新值的时候,如果表中最后一条记录对应的A B C 的任意字段的值和上一条记录的差值大于等于其阀值的时候,则用新值新增一条记录。 否则用新值改写最后一条记录。
if not exists (select * from tbl_a)
begin
insert tbl_a(time,a,b,c) values('1900-1-1',0,0,0)
insert tbl_a(time,a,b,c) values(getdate(),@a,@b,@c)
return 0
end
if (select count(*) from tbl_a)=1 --楼主没说怎么处理,我胡乱猜
begin
insert tbl_a(time,a,b,c) values(getdate(),@a,@b,@c)
return 1
end
if exists (
select 1 from tbl_a a,tbl_a b,tbl_d c
where a.time=(select max(time) from tbl_a)
and b.time=(select max(time) from tbl_a where time<a.time)
and (a.a-b.a>=c.da or a.b-b.v>=c.db or a.c-b.c>=c.dc)
)
insert table_a(time,a,b,c) values(getdate(),@a,@b,@c)
else
update tbl_a
set time=getdate(),a=@a,b=@b,c=@c
where time=(select max(time) from tbl_a)
return 2