这是我用来更新电表记录的存储过程
/*根据电表编号更新抄表记录*/
CREATE PROCEDURE MyProc_UpDateAmmeterRecord
(
@AmmeterNo varchar(20),
@PreCount numeric(15,2),
@Count numeric(15,2),
@Date DateTime
) AS
Declare @AmmeterID int,
@Month smallint,
@Year smallint
Select @Year= Case Month(@Date)
when 1 then
Year(@Date)-1
else
Year(@Date)
end,
@Month=Case Month(@Date)
When 1 then
12
else
Month(@Date)-1
end
Select @AmmeterID=ID from Ammeter where IsReplace=0 and No=@AmmeterNo
if @AmmeterID IS NULL
begin
Delete from AmmeterRecord Where AmmeterID=@AmmeterID and [Year]=@Year and [Month]=@Month
insert into AmmeterRecord (AmmeterID,PreCount,[Count],[Date],[Year],[Month]) Values (@AmmeterID,@PreCount,@Count,@Date,@Year,@Month)
if not Exists(Select ID from AmmeterRecord where AmmeterID=@AmmeterID and [Year]=Year(@Date) and [Month]=Month(@Date))
Update Ammeter Set PreCount = @Count where ID=@AmmeterID
Return(0)
end
else
Return(1)
GO