if object_id(N'dbo.tr_UserInfo') is null and exists (select * from sys.tables where object_id = object_id(N'dbo.UserInfo'))
begin
    exec ('create trigger dbo.tr_UserInfo
on dbo.UserInfo
for insert, update
as 
begin
    set nocount on

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