,不使用特定Teacher_ID
參數:
相關子查詢版本:
create proc spUpdateTeachingHoursAndWages as
begin;
set nocount, xact_abort on;
update p
set HoursPerWeek = (
select count(*)
from Assignment.dbo.PupilDetails pd
where pd.Teacher_id = p.id
)
from Assignment.dbo.Payments p;
end;
go
inner join
版本:
create proc spUpdateTeachingHoursAndWages as
begin;
set nocount, xact_abort on;
update p
set HoursPerWeek = pd.HoursPerWeek
from Assignment.dbo.Payments p
inner join (
select
pd.id
, HoursPerWeek = count(*)
from Assignment.dbo.PupilDetails pd
group by pd.id
) pd
on p.id = pd.Teacher_id;
end;
go
cross apply()
版本:
create proc spUpdateTeachingHoursAndWages as
begin;
set nocount, xact_abort on;
update p
set HoursPerWeek = pd.HoursPerWeek
from Assignment.dbo.Payments p
cross apply(
select HoursPerWeek = count(*)
from Assignment.dbo.PupilDetails pd
where p.id = pd.Teacher_id;
) pd
end;
go
注:Why you should always include set xact_abort, nocount on;
- Erland Sommarskog
謝謝你,但我得到一個'無效的對象name'錯誤HoursPerWeek。將嘗試查看造成衝突的原因。 –
對不起,我以爲付款是模式名稱,但它的表名稱,刪除.HoursPerWeek – McNets
感謝您的答覆,我其實已經想通了:)感謝您的幫助! –