IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EST_PROD_PRECO_ALT_PRC_FILIAL]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[EST_PROD_PRECO_ALT_PRC_FILIAL](
	[CD_EMP] [int] NOT NULL,
	[CD_ALT_PRECO] [int] NOT NULL,
	[CD_FILIAL] [int] NOT NULL,
 CONSTRAINT [PK_EST_PROD_PRECO_ALT_PRC_FILIAL] PRIMARY KEY CLUSTERED 
(
	[CD_EMP] ASC,
	[CD_ALT_PRECO] ASC,
	[CD_FILIAL] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END


IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DF_EST_PROD_PRECO_ALT_PRC_FILIAL_CD_EMP]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[EST_PROD_PRECO_ALT_PRC_FILIAL] ADD  CONSTRAINT [DF_EST_PROD_PRECO_ALT_PRC_FILIAL_CD_EMP]  DEFAULT ((0)) FOR [CD_EMP]
END

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DF_EST_PROD_PRECO_ALT_PRC_FILIAL_CD_ALT_PRECO]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[EST_PROD_PRECO_ALT_PRC_FILIAL] ADD  CONSTRAINT [DF_EST_PROD_PRECO_ALT_PRC_FILIAL_CD_ALT_PRECO]  DEFAULT ((0)) FOR [CD_ALT_PRECO]
END

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[DF_EST_PROD_PRECO_ALT_PRC_FILIAL_CD_FILIAL]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[EST_PROD_PRECO_ALT_PRC_FILIAL] ADD  CONSTRAINT [DF_EST_PROD_PRECO_ALT_PRC_FILIAL_CD_FILIAL]  DEFAULT ((0)) FOR [CD_FILIAL]
END

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_EST_PROD_PRECO_ALT_PRC_FILIAL_EST_PROD_PRECO_ALT]') AND parent_object_id = OBJECT_ID(N'[dbo].[EST_PROD_PRECO_ALT_PRC_FILIAL]'))
ALTER TABLE [dbo].[EST_PROD_PRECO_ALT_PRC_FILIAL]  WITH CHECK ADD  CONSTRAINT [FK_EST_PROD_PRECO_ALT_PRC_FILIAL_EST_PROD_PRECO_ALT] FOREIGN KEY([CD_EMP], [CD_ALT_PRECO])
REFERENCES [dbo].[EST_PROD_PRECO_ALT] ([CD_EMP], [CD_ALT_PRECO])

IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_EST_PROD_PRECO_ALT_PRC_FILIAL_EST_PROD_PRECO_ALT]') AND parent_object_id = OBJECT_ID(N'[dbo].[EST_PROD_PRECO_ALT_PRC_FILIAL]'))
ALTER TABLE [dbo].[EST_PROD_PRECO_ALT_PRC_FILIAL] CHECK CONSTRAINT [FK_EST_PROD_PRECO_ALT_PRC_FILIAL_EST_PROD_PRECO_ALT]
