alter table vclib.t_entities_web_alias_access drop constraint fk_t_entities_web_alias_t_entities_web_alias_access_entitieswebalias;
alter table vclib.t_entities_web_alias drop constraint fk_t_entities_web_alias_t_entities_web_alias_inheritsalias;
alter table vclib.t_entities_web_alias drop constraint pk_t_entities_web_alias;

ALTER TABLE [VCLib].[T_ENTITIES_WEB_ALIAS] ADD [EntitiesWebAliasID] uniqueidentifier NULL
GO
UPDATE [VCLib].[T_ENTITIES_WEB_ALIAS] SET [EntitiesWebAliasID] = NEWID()
GO
ALTER TABLE [VCLib].[T_ENTITIES_WEB_ALIAS] ALTER COLUMN [EntitiesWebAliasID] uniqueidentifier NOT NULL
GO
ALTER TABLE [VCLib].[T_ENTITIES_WEB_ALIAS] ADD CONSTRAINT [PK_T_ENTITIES_WEB_ALIAS]
PRIMARY KEY CLUSTERED ([EntitiesWebAliasID]) ON [PRIMARY]
GO



exec sp_rename N'VCLib.T_ENTITIES_WEB_ALIAS_ACCESS.EntitiesWebAlias','EntitiesWebAliasOld', 'COLUMN'
GO
ALTER TABLE [VCLib].[T_ENTITIES_WEB_ALIAS_ACCESS] ADD [EntitiesWebAlias] uniqueidentifier NULL
GO
UPDATE ewa
SET ewa.[EntitiesWebAlias] = (select EntitiesWebAliasID from [VCLib].[T_ENTITIES_WEB_ALIAS] where Alias = ewa.EntitiesWebAliasOld)
from [VCLib].[T_ENTITIES_WEB_ALIAS_ACCESS] ewa
GO
ALTER TABLE [VCLib].[T_ENTITIES_WEB_ALIAS_ACCESS] ALTER COLUMN [EntitiesWebAlias] uniqueidentifier NOT NULL
GO
ALTER TABLE [VCLib].[T_ENTITIES_WEB_ALIAS_ACCESS] ADD CONSTRAINT [FK_T_ENTITIES_WEB_ALIAS_T_ENTITIES_WEB_ALIAS_ACCESS_EntitiesWebAlias]
FOREIGN KEY([EntitiesWebAlias]) REFERENCES [VCLib].[T_ENTITIES_WEB_ALIAS] ([EntitiesWebAliasID])
GO
ALTER TABLE [VCLib].[T_ENTITIES_WEB_ALIAS_ACCESS] DROP COLUMN [EntitiesWebAliasOld]
GO

exec sp_rename N'VCLib.T_ENTITIES_WEB_ALIAS.InheritsAlias','InheritsAliasOld', 'COLUMN'
GO
ALTER TABLE [VCLib].[T_ENTITIES_WEB_ALIAS] ADD [InheritsAlias] uniqueidentifier NULL
GO
update ewa
set ewa.InheritsAlias = (select EntitiesWebAliasID from [VCLib].[T_ENTITIES_WEB_ALIAS] where Alias = ewa.InheritsAliasOld)
from [VCLib].[T_ENTITIES_WEB_ALIAS] ewa
GO
ALTER TABLE [VCLib].[T_ENTITIES_WEB_ALIAS] ADD CONSTRAINT [FK_T_ENTITIES_WEB_ALIAS_T_ENTITIES_WEB_ALIAS_InheritsAlias]
FOREIGN KEY([InheritsAlias]) REFERENCES [VCLib].[T_ENTITIES_WEB_ALIAS] ([EntitiesWebAliasID])
GO
ALTER TABLE [VCLib].[T_ENTITIES_WEB_ALIAS] DROP COLUMN [InheritsAliasOld]
GO
