CREATE TRIGGER TEPO_UPDATE_PROVIDER -- runs every time there's an insert or update to the calendar table
ON CALENDAR
FOR INSERT--, UPDATE breaks the world if update added
NOT FOR REPLICATION AS
SET NOCOUNT ON
SET ARITHABORT ON
IF EXISTS (select * from inserted i) -- makes sure we altered the calendar table before proceeding
BEGIN
UPDATE contacts
set contacts.physicianid =
(select CASE inserted.physicianid
WHEN '181' THEN '9' -- First is calendar doc, second is profile doc, from physician and provider tables
WHEN '15' THEN '8' -- another doctor
WHEN '16' THEN '7' -- another doctor
WHEN '655' THEN '14' -- another doctor
ELSE contacts.physicianid -- leaves the doctor alone if it not one listed above
END
from inserted, contacts
WHERE inserted.subjectid in (249,250) -- from otherinventory table
and contacts.contactid = inserted.patientid) -- makes sure it's just the patient we want
END
GO