我目前正在研究一个项目,该项目正在考虑拥有一个仅在其中存储 2 行数据的数据库。该数据库将一直添加新行,并且在输入新行时将删除顶行,以确保始终有 2 行。
我要做的是比较顶行数据和底行数据,然后将不同的数据存储在变量中。我只想存储与底行不同的数据,因为顶行将始终是最旧的数据集,我不再需要处理它。
我可以在 C# 端完成这一切,但我想知道是否有办法在 SQL 端完成大部分工作?谢谢您的帮助 :-)
感谢您的澄清,让我们假设这张表:
create table temp_monitor(id int, stamp datetime, @newTemp1 float, @newTemp2 float)
那么这个存储过程应该可以解决问题
create proc updateTempMonitor(temp_oven_1 float, temp_oven_2 float)
as
-- save the values where about to replace
declare @id int, @t1 float, @t2 float
select top 1 @id = id, @t1 = temp_oven_1, @t2 = temp_oven_2 from temp_monitor order by stamp
-- update the oldest
update updateTempMonitor set temp_oven_1 = @newTemp1, temp_oven_2 = @newTemp2 where id = @id
-- return data to caller (c#)
select @t1, @t2