IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[CustomerExternalRefXtab]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
  CREATE TABLE [dbo].[CustomerExternalRefXtab]
  (
    [FK_CustomerNumber] [int] NOT NULL,
    [FK_ExternalRefId] [int] NOT NULL,
    [CreatedDateTime] [datetime] NULL,
    [UpdatedDateTime] [datetime] NULL,
    [FK_UserId] [int] NULL,
    CONSTRAINT [PK_CustomerExternalRefXtab] PRIMARY KEY CLUSTERED 
    (
      [FK_CustomerNumber] ASC,
      [FK_ExternalRefId] ASC
    )
  ) ON [PRIMARY]

  ALTER TABLE [dbo].[CustomerExternalRefXtab]  WITH CHECK ADD  CONSTRAINT [FK_CustomerExternalRefXtab_Customer] FOREIGN KEY([FK_CustomerNumber])
  REFERENCES [dbo].[Customer] ([CustomerNumber])

  ALTER TABLE [dbo].[CustomerExternalRefXtab] CHECK CONSTRAINT [FK_CustomerExternalRefXtab_Customer]

  ALTER TABLE [dbo].[CustomerExternalRefXtab]  WITH CHECK ADD  CONSTRAINT [FK_CustomerExternalRefXtab_ExternalReference] FOREIGN KEY([FK_ExternalRefId])
  REFERENCES [dbo].[ExternalReference] ([ID])

  ALTER TABLE [dbo].[CustomerExternalRefXtab] CHECK CONSTRAINT [FK_CustomerExternalRefXtab_ExternalReference]

  ALTER TABLE [dbo].[CustomerExternalRefXtab]  WITH CHECK ADD  CONSTRAINT [FK_CustomerExternalRefXtab_Personel] FOREIGN KEY([FK_UserId])
  REFERENCES [dbo].[Personel] ([UserIDNumber])

  ALTER TABLE [dbo].[CustomerExternalRefXtab] CHECK CONSTRAINT [FK_CustomerExternalRefXtab_Personel]
END