if object_id(N'dbo.tr_Department') is null
begin
    exec ('create trigger dbo.tr_Department
on dbo.Department
for insert, update
as 
begin
    set nocount on

    insert into dbo.DepartmentHistory (HistoryType, DepartmentID, Name, Description, ModifiedDate, ModifiedUserName, ModifiedLoggedUserName)
    select case when d.ID is null then ''I'' else ''U'' end, i.ID, i.Name, i.Description, case when i.ModifiedDate = d.ModifiedDate then getdate() else isnull(i.ModifiedDate, getdate()) end, i.ModifiedUserName, i.ModifiedLoggedUserName
    from inserted i
        left outer join deleted d on i.ID = d.ID
end');
end