1

我目前正在研究一个项目,该项目正在考虑拥有一个仅在其中存储 2 行数据的数据库。该数据库将一直添加新行,并且在输入新行时将删除顶行,以确保始终有 2 行。

我要做的是比较顶行数据和底行数据,然后将不同的数据存储在变量中。我只想存储与底行不同的数据,因为顶行将始终是最旧的数据集,我不再需要处理它。

我可以在 C# 端完成这一切,但我想知道是否有办法在 SQL 端完成大部分工作?谢谢您的帮助 :-)

4

2 回答 2

1

感谢您的澄清,让我们假设这张表:

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
于 2012-06-15T08:29:17.430 回答
0

在 C# 和 SQL 中都是可能的,

SQL 方面,您需要编写 SP 或其他东西,它将以列名和更改的列的形式返回您的差异。

在这里寻找更多。SQL 行到列

于 2012-06-15T08:45:53.207 回答