
alter schema vclib transfer dbo.T_USERS

-- Tables

/***** Object:  Table [FIES].[T_USERS] *****/
CREATE TABLE [FIES].[T_USERS]
(
    [UserID] uniqueidentifier NOT NULL,
    [Mo] varchar(6) NULL,
    [Smo] varchar(5) NULL,
    [TfomsBranch] uniqueidentifier NULL
)
ON [PRIMARY]
GO

-- Alters

ALTER TABLE [FIES].[T_USERS] WITH NOCHECK ADD CONSTRAINT [PK_T_USERS]
PRIMARY KEY CLUSTERED ([UserID]) ON [PRIMARY]
GO

ALTER TABLE [FIES].[T_USERS] WITH NOCHECK ADD CONSTRAINT [FK_T_USERS_T_USERS_UserID]
FOREIGN KEY([UserID]) REFERENCES [VCLib].[T_USERS] ([UserID])
GO

ALTER TABLE [FIES].[T_USERS] WITH NOCHECK ADD CONSTRAINT [FK_T_MO_T_USERS_Mo]
FOREIGN KEY([Mo]) REFERENCES [FIES].[T_MO] ([MCOD])
GO

ALTER TABLE [FIES].[T_USERS] WITH NOCHECK ADD CONSTRAINT [FK_T_SMO_T_USERS_Smo]
FOREIGN KEY([Smo]) REFERENCES [FIES].[T_SMO] ([SMOCOD])
GO

ALTER TABLE [FIES].[T_USERS] WITH NOCHECK ADD CONSTRAINT [FK_T_TFOMS_BRANCH_T_USERS_TfomsBranch]
FOREIGN KEY([TfomsBranch]) REFERENCES [FIES].[T_TFOMS_BRANCH] ([TfomsBranchID])
GO





insert into FIES.T_USERS ([UserID], [Mo], [Smo], [TfomsBranch])
select [UserID], [Mo], [Smo], [TfomsBranch] from VCLib.T_USERS where  type_ = (select top 1 entityid from vclib.t_entities where entname = 'USERS' and [schema] = 'FIES')


IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[VCLib].[FK_T_MO_T_USERS_Mo]') AND parent_object_id = OBJECT_ID(N'[VCLib].[T_USERS]'))
ALTER TABLE [VCLib].[T_USERS] DROP CONSTRAINT [FK_T_MO_T_USERS_Mo]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[VCLib].[FK_T_SMO_T_USERS_Smo]') AND parent_object_id = OBJECT_ID(N'[VCLib].[T_USERS]'))
ALTER TABLE [VCLib].[T_USERS] DROP CONSTRAINT [FK_T_SMO_T_USERS_Smo]
GO

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[VCLib].[FK_T_TFOMS_BRANCH_T_USERS_TfomsBranch]') AND parent_object_id = OBJECT_ID(N'[VCLib].[T_USERS]'))
ALTER TABLE [VCLib].[T_USERS] DROP CONSTRAINT [FK_T_TFOMS_BRANCH_T_USERS_TfomsBranch]
GO


alter table VCLib.T_USERS drop column [Mo]
alter table VCLib.T_USERS drop column [Smo]
alter table VCLib.T_USERS drop column [TfomsBranch]