更新存储过程不更新

时间:2013-04-24 16:59:08

标签: sql-server stored-procedures if-statement while-loop sql-update

我有一个SQL Server存储过程,它引用了我的数据库中的一个表,用户可以在其中手动更新rent字段('Rent1')的值。该过程将此租金值与另一个表('Rent2')中的租赁字段进行比较。如果Rent1Rent2不同,则Rent2的值会更新为Rent1的值...或至少是应该发生的值。

当我执行这个存储过程时,它运行正常,我收到这些输出消息:

(1 row(s) affected)


(1 row(s) affected)

我期望的结果是什么,因为作为一种测试手段,我在Rent1Rent2之间更改了两个值。但是当我查询更新的表时,值保持不变。

这是我的存储过程:

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

2 个答案:

答案 0 :(得分:1)

在解决问题的世界中:

在更新之前放置一个select语句,以查看是否匹配

/*
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)
*/
select * from unittype
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))

OR

declare @myCountCheck
select @myCountCheck =
(select count(*)
from unittype
    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))
)

if (@myCountCheck < 1)
BEGIN
    print 'No Row Match !!!'
END

EDIT ---------------------------------------

如果你真的想看看发生了什么,那么编写一些“输出”审核...... 这样你就可以捕获INSERT / UPDATE语句中发生的事情

http://granadacoder.wordpress.com/2008/12/10/sqlserver20052008-output-clause-in-insertupdatedelete-statements/

以下是示例代码:

SqlServer2005 / 2008 // INSERT / UPDATE / DELETE语句中的OUTPUT子句

这些类型的样本遍布网络上,但这是我最初的例子,我相信它更清晰。

原始示例: http://blogs.msdn.com/sqltips/archive/2005/06/13/OUTPUT-clause.aspx

create table PrimaryHolderTable ( i int identity (1001,2) not null primary key, j int not null unique )
create table #OutputResultsHolder ( i int not null, j int not null)

insert into PrimaryHolderTable (j)
output inserted.i, inserted.j into #OutputResultsHolder
select top 10 o.object_id from sys.objects as o order by o.object_id desc –<< from sys.objects is there just to provide some rows


select * from #OutputResultsHolder
drop table #OutputResultsHolder, PrimaryHolderTable;

go



create table dbo.EmployeeTable ( EmpKey int identity(1001,2) ,  EmpAge int not null );
create table dbo.AuditTable ( EntityKey int not null default -1  ,  OldValue int null, NewValue int null , Tag varchar(64)  );

insert into dbo.EmployeeTable (EmpAge)
output inserted.EmpKey , null , inserted.EmpAge , ‘Employee Inserted’ into dbo.AuditTable ( EntityKey , OldValue , NewValue , Tag)
 values( 18 );

insert into dbo.EmployeeTable (EmpAge)
output inserted.EmpKey , null , inserted.EmpAge , ‘Employee Inserted’ into dbo.AuditTable ( EntityKey , OldValue , NewValue , Tag) 
 values( 20 );

insert into dbo.EmployeeTable (EmpAge)
output inserted.EmpKey , null , inserted.EmpAge , ‘Employee Inserted’ into dbo.AuditTable ( EntityKey , OldValue , NewValue , Tag) 
 values( 22 );


update dbo.EmployeeTable
   set EmpAge  = EmpAge + 1
output inserted.EmpKey , deleted.EmpAge, inserted.EmpAge , ‘Employee Updated’ into dbo.AuditTable ( EntityKey , OldValue , NewValue , Tag)
 where EmpAge <=20;

delete from dbo.EmployeeTable
output deleted.EmpKey , deleted.EmpAge, NULL , ‘Employee Deleted’  into dbo.AuditTable (EntityKey , OldValue , NewValue , Tag)
 where EmpAge > 0;–Test multi rows

select * from dbo.EmployeeTable;–<<will be empty at this point
select * from dbo.AuditTable;

drop table dbo.EmployeeTable, dbo.AuditTable;
go

答案 1 :(得分:1)

我不知道我的答案可以帮助任何人多少,但是如果有可能的话,我会把它包括在这里..

所以我的SP中引用的unit_rent表是由我创建的,并填充了我unittype表中的数据(也在SP中引用)。当我填充unit_rent表时,我抓住了unittype表中的所有行。这是我犯错误的地方。 unittype表包含与特定单元类型相关联的多个单元,因此每当我使用存储过程更新一行时,与该单元类型关联的所有其他单元将变为租金金额!=我变了。所以我重新填充了我的unit_rent表,只有不同的单位类型,我的问题就解决了。

非常愚蠢的错误,但我不想让它没有得到答案,因为它可能会帮助别人。

@granadaCoder - 谢谢,再次为您提供帮助。你第二次非常彻底地帮助了我。