use AnP

if object_id(N'dbo.users_tables_remove', N'P') is not null drop procedure dbo.users_tables_remove
go
create procedure dbo.users_tables_remove as begin set nocount on 
    if object_id(N'dbo._Users', N'U') is not null drop table dbo._Users
    if object_id(N'dbo.Users', N'U') is not null drop table dbo.Users
end
go

if object_id(N'dbo.users_tables_create', N'P') is not null drop procedure dbo.users_tables_create
go
create procedure dbo.users_tables_create as begin set nocount on 

    if object_id(N'dbo.Users', N'U') is null create table dbo.Users(
        id integer not null identity(1, 1), 
        [procedure] integer not null, 
        [session] integer not null, 
        nick varchar(32) not null, 
        [password] varbinary(64) not null, 
        accessible bit not null constraint df_users_accessible default 1, 
        date_in datetime2 not null constraint df_users_date_in default getdate(), 
        date_out datetime2, 
        constraint pk_users primary key clustered(id), 
        constraint fk_users_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint uq_users_nick unique(nick) with(fillfactor = 90), 
        constraint ck_users_nick check(nick != '' and nick not like '%[^a-zA-Z0-9_]%' and nick not like '[^a-zA-Z_]%'), 
        constraint ck_users_password check(datalength([password]) > 0)
    )

    if object_id(N'dbo._Users', N'U') is null create table dbo._Users(
        id integer not null identity(1, 1), 
        mode integer not null, 
        entity integer not null, 
        [procedure] integer not null, 
        [session] integer not null, 
        nick varchar(32) not null, 
        [password] varbinary(64) not null, 
        accessible bit not null, 
        date_in datetime2 not null, 
        date_out datetime2, 
        history_date datetime2 not null constraint df__users_history_in default sysutcdatetime(), 
        constraint pk__users primary key clustered(id), 
        constraint fk__users_mode foreign key(mode) references dbo.HistoryModes(id) 
            on update no action 
            on delete no action,
        constraint fk__users_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        index ix__users_history nonclustered(entity, history_date)
    )

end
go

execute dbo.users_tables_create

if object_id(N'dbo.users_tables_upgrade', N'P') is not null drop procedure dbo.users_tables_upgrade
go
create procedure dbo.users_tables_upgrade as begin set nocount on 

    if (select top 1 0 from information_schema.table_constraints where 
        table_catalog = db_name() and 
        table_name = 'Users' and 
        constraint_name = 'fk_users_session'
    ) is null alter table dbo.Users add constraint fk_users_session foreign key([session]) references dbo.Sessions(id) 
        on update no action 
        on delete no action
    
    if (select top 1 0 from information_schema.table_constraints where 
        table_catalog = db_name() and 
        table_name = '_Users' and 
        constraint_name = 'fk__users_session'
    ) is null alter table dbo._Users add constraint fk__users_session foreign key([session]) references dbo.Sessions(id) 
        on update no action 
        on delete no action

end
go

if object_id(N'dbo.users_trigger', N'TR') is not null drop trigger dbo.users_trigger
go
create trigger dbo.users_trigger on dbo.Users for insert, update, delete as begin set nocount on 

    declare @insert integer, @update integer, @delete integer, @unknown integer

    execute dbo.history_mode_get @insert output, @update output, @delete output, @unknown output

    -- update.
    insert into dbo._Users(mode, entity, [procedure], [session], nick, [password], accessible, date_in, date_out) 
    select @update, inserted.id, inserted.[procedure], inserted.[session], inserted.nick, inserted.[password], inserted.accessible, inserted.date_in, inserted.date_out 
    from inserted inserted 
    join deleted deleted on inserted.id = deleted.id 
    where 
        inserted.[procedure] != deleted.[procedure] or 
        inserted.[session] != deleted.[session] or 
        inserted.nick != deleted.nick or 
        inserted.[password] != deleted.[password] or 
        inserted.accessible != deleted.accessible or 
        inserted.date_in != deleted.date_in or 
        isnull(inserted.date_out, '0001-01-01') != isnull(deleted.date_out, '0001-01-01')

    -- Insert.
    insert into dbo._Users(mode, entity, [procedure], [session], nick, [password], accessible, date_in, date_out) 
    select @insert, inserted.id, inserted.[procedure], inserted.[session], inserted.nick, inserted.[password], inserted.accessible, inserted.date_in, inserted.date_out 
    from inserted inserted 
    left join deleted deleted on inserted.id = deleted.id 
    where deleted.id is null

    -- deleted.
    insert into dbo._Users(mode, entity, [procedure], [session], nick, [password], accessible, date_in, date_out) 
    select @delete, deleted.id, deleted.[procedure], deleted.[session], deleted.nick, deleted.[password], deleted.accessible, deleted.date_in, deleted.date_out 
    from deleted deleted 
    left join inserted inserted on deleted.id = inserted.id 
    where inserted.id is null

end
go