我有一个 SQL Server 存储过程,它引用了我的数据库中的一个表,用户可以在其中手动更新rent
字段的值 ( 'Rent1'
)。该过程将此租金值与不同表 ( 'Rent2'
) 中的租金字段进行比较。如果与 Is的值Rent1
不同,则更新为... 的值,或者至少这是应该发生的。Rent2
Rent2
Rent1
当我执行这个存储过程时,它运行良好并且我收到这些输出消息:
(1 row(s) affected)
(1 row(s) affected)
这是我所期望的结果,因为作为一种测试手段,我已经将两个值更改为在Rent1
和之间不同Rent2
。但是当我查询更新后的表时,值保持不变。
这是我的存储过程:
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER PROCEDURE update_rent
AS
DECLARE @flag INT
SET @flag = (select COUNT(*) from unit_rent left outer join unittype on unittype = scode where rent <> srent)
WHILE (@flag > 0)
BEGIN
IF (select min(rent) from unit_rent
left outer join unittype on unittype = scode
left outer join property on property.scode = unit_rent.pscode
where rent <> srent) <>
(select min(srent) from unit_rent
left outer join unittype on unittype = scode
left outer join property on property.scode = unit_rent.pscode
where rent <> srent
and rent in (select min(rent) from unit_rent
left outer join unittype on unittype = scode
left outer join property on property.scode = unit_rent.pscode
where rent <> srent))
BEGIN
UPDATE unittype
SET srent = (select min(rent) from unit_rent
left outer join unittype on unittype = scode
left outer join property on property.scode = unit_rent.pscode
where rent <> srent)
WHERE unittype.hmy = (select min(hmy) from unittype left outer join unit_rent on unittype = scode where rent <> srent
and rent = (select min(rent) from unit_rent left outer join unittype on unittype = scode where rent <> srent))
SET @flag = @flag-1;
END
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
谁能看到我可能出错的地方或告诉我为什么我的输出消息在骗我?或者也许我可以采取不同的方法?我将不胜感激任何形式的帮助,谢谢!
更新:刚刚尝试了一种不同的方法,结果相同,只有 3 条(1 row(s) addected)
消息:
ALTER PROCEDURE update_rent
AS
DECLARE @tmprent TABLE (hmy INT, rent decimal(11,2));
DECLARE @flag INT
SET @flag = (select COUNT(*) from unit_rent left outer join unittype on unittype = scode where rent <> srent)
INSERT INTO @tmprent (hmy, rent) values (1, 0.00);
WHILE (@flag > 0)
BEGIN
IF (select min(rent) from unit_rent
left outer join unittype on unittype = scode
left outer join property on property.scode = unit_rent.pscode
where rent <> srent) <>
(select min(srent) from unit_rent
left outer join unittype on unittype = scode
left outer join property on property.scode = unit_rent.pscode
where rent <> srent
and rent in (select min(rent) from unit_rent
left outer join unittype on unittype = scode
left outer join property on property.scode = unit_rent.pscode
where rent <> srent))
BEGIN
UPDATE @tmprent
SET rent = (select min(rent) from unit_rent
left outer join unittype on unittype = scode
left outer join property on property.scode = unit_rent.pscode
where rent <> srent)
WHERE hmy = 1
UPDATE unittype
SET srent = (select rent from @tmprent where hmy = 1)
WHERE unittype.hmy = (select min(hmy) from unittype left outer join unit_rent on unittype = scode where rent <> srent
and rent = (select min(rent) from unit_rent left outer join unittype on unittype = scode where rent <> srent))
SET @flag = @flag-1;
END
END