です。これにはいくつかの方法があります。
まず、通常のアップデートを行うだけではない理由がわかりましたか?
update emp
set salary = emp.salary + (bonus * 2)
from employee emp
join bonuses bon on bon.employee_id = emp.employee_id
where bon.deserved = 1
もう一つの方法は、カーソルを使用することですが、人々は、そのパフォーマンスの問題を実際にそれら好きではない:https://docs.microsoft.com/en-us/sql/t-sql/language-elements/declare-cursor-transact-sql
もう一つの方法は、しばらくそれから、従業員の数を保持するコンテナを作成することです現在の反復はカウントと等しくなく、各ループで必要な従業員を取得します。
declare @employeecount int = (select count(*) from employee)
declare @current int = 1
while @current <= @employeecount
begin
declare @employeeid uniqueidentifier = (select top 1 employee_id from (select top (@current) * from employee order by employee_id asc) tmp order by employee_id desc)
-- Grab Employee and run logic
set @current = @current + 1;
end
ボトムはCURSORSの機能とほとんど同じですが、カーソルはそれより優れています。
私が最近取ったことは次のとおりです。私はそれに従うだけで簡単だとわかります。
declare @ids table (id uniqueidentifier)
insert into @ids
select employee_id
from employee
while (select count(*) from @ids) > 0
begin
declare @employeeid uniqueidentifier = (select top 1 employee_id from @ids)
-- Grab Employee and run logic
delete from @ids where id = @employeeid
end
私は最後のものが好きですが、いくつかの問題があるようです。イテレートは各IDにはなく、いくつかのボーナスはIDの間に混在することがあります。 – jou
私は似たようなことをしています。(上から1つのemployee_idを選択してください(上から(*現在)*をemployee_idから順に選択してください)。私はそれをやり直す別の方法を作りました。大規模なコードで読むのが好きかもしれません。 – Monofuse
あなたのロジックを試してみます。ありがとうございます。 – jou