SQL Query比较2行并存储变量C#的差异?

时间:2012-06-15 07:36:47

标签: c# sql variables compare

我目前正在开发一个项目,该项目正在寻找一个只存储2行数据的数据库。此数据库将始终添加新行,并且在输入新行时将删除顶行,确保始终有2行。

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

我可以在C#端完成所有这些但是我想知道是否有办法在SQL方面做大部分事情?感谢您的帮助: - )

2 个答案:

答案 0 :(得分:0)

感谢清除,让我们假设这个表:

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

答案 1 :(得分:0)

可以在C#和SQL两端使用

SQL端你需要编写SP或者某些东西,它会以列名和更改列的形式返回你的diff。

在这里查看更多信息。 SQL Rows to Columns