if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ProductsCategoryGroupXtab]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

CREATE TABLE [dbo].[ProductsCategoryGroupXtab](
	[FK_ProductNumber] [int] NOT NULL,
	[FK_CategoryGroupNumber] [int] NOT NULL,
        [CategoryType] [int] NOT NULL
) ON [PRIMARY]

GO

IF NOT EXISTS (SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME ='FK_ProductsCategoryGroupXtab_CategoryGroup')
  ALTER TABLE [dbo].[ProductsCategoryGroupXtab]  WITH CHECK ADD  CONSTRAINT [FK_ProductsCategoryGroupXtab_CategoryGroup] FOREIGN KEY([FK_CategoryGroupNumber])
  REFERENCES [dbo].[CategoryGroup] ([GroupNumber])
GO

IF EXISTS (SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME ='FK_ProductsCategoryGroupXtab_CategoryGroup')
  ALTER TABLE [dbo].[ProductsCategoryGroupXtab] CHECK CONSTRAINT [FK_ProductsCategoryGroupXtab_CategoryGroup]
GO

IF NOT EXISTS (SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME ='FK_ProductsCategoryGroupXtab_Products')
  ALTER TABLE [dbo].[ProductsCategoryGroupXtab]  WITH CHECK ADD  CONSTRAINT [FK_ProductsCategoryGroupXtab_Products] FOREIGN KEY([FK_ProductNumber])
  REFERENCES [dbo].[Products] ([ProductNumber])
GO

IF EXISTS (SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME ='FK_ProductsCategoryGroupXtab_Products')
  ALTER TABLE [dbo].[ProductsCategoryGroupXtab] CHECK CONSTRAINT [FK_ProductsCategoryGroupXtab_Products]
GO


