1

ユーザーがフィールドの値を手動で更新できるデータベース内のテーブルを参照する SQL Server ストアド プロシージャがありますrent( 'Rent1')。このプロシージャは、この賃貸料の値を別のテーブルの賃貸料フィールドと比較します ( 'Rent2')。If Rent1is different from Rent2the value for Rent2Is updated to the value of Rent1... または少なくともそれが起こるはずです。

このストアド プロシージャを実行すると、問題なく実行され、次の出力メッセージが表示されます。

(1 row(s) affected)


(1 row(s) affected)

Rent1テストの手段として、2つの値を と の間で異なるように変更したため、これは私が期待する結果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
4

2 に答える 2

1

トラブル解決の世界:

Update の前に 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))

また

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

編集 - - - - - - - - - - - - - - - - - - - -

何が起こっているかを本当に知りたい場合は、「出力」監査をコード化してください...... そうすれば、INSERT/UPDATE ステートメントで何が起こっているかを把握できます。

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

サンプルコードは次のとおりです。

SqlServer2005/2008 // INSERT/UPDATE/DELETE ステートメントの OUTPUT 句

これらのタイプのサンプルは Web 上のいたるところにありますが、ここに私の元の例を示します。

元の例: 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 –&lt;< 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;–&lt;<will be empty at this point
select * from dbo.AuditTable;

drop table dbo.EmployeeTable, dbo.AuditTable;
go
于 2013-04-24T17:18:13.787 に答える
1

私の答えが誰かにどれだけ役立つかはわかりませんが、可能であれば、ここに含めます..

したがって、unit_rentSPで参照されているテーブルは、私が作成し、unittypeテーブルのデータが入力されています(SPでも参照されています)。テーブルにデータを入力したときunit_rent、テーブルからすべての行を取得しましたunittype。ここで私は間違いを犯しました。テーブルにはunittype、特定の単位タイプに関連付けられた複数の単位が含まれていたため、ストアド プロシージャで 1 つの行を更新すると、その単位タイプに関連付けられた他のすべての単位が!=、変更​​した家賃の額になります。unit_rentそのため、テーブルに個別のユニットタイプのみを再入力すると、問題は解決しました。

かなりばかげた間違いですが、他の誰かを助ける可能性があるという可能性を考えて、未回答のままにしたくありません。

@granadaCoder - ありがとうございます。2回目ですが、大変お世話になりました。

于 2013-04-30T15:46:09.323 に答える