
IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL') 
BEGIN

CREATE TABLE [dbo].[EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL](
	[CD_EMP] [int] NOT NULL,
	[CD_FILIAL] [int] NOT NULL,
	[CD_PED] [int] NOT NULL,
	[CD_FORN] [int] NOT NULL,
	[CD_COND_COMPRA] [int] NOT NULL,
	[CD_PROD] [int] NOT NULL,
	[VLR_PROD] [money] NOT NULL,
	[PERC_DESC] [money] NOT NULL,
 CONSTRAINT [PK_EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL] PRIMARY KEY CLUSTERED 
(
	[CD_EMP],
	[CD_FILIAL],
	[CD_PED],
	[CD_FORN],
	[CD_COND_COMPRA],
	[CD_PROD] )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY];

ALTER TABLE [dbo].[EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL]  WITH CHECK ADD  
			CONSTRAINT [FK_EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL_EST_PROD_PRECO_COND_COMPRA] FOREIGN KEY(  [CD_EMP],[CD_COND_COMPRA])
REFERENCES [dbo].[EST_PROD_PRECO_COND_COMPRA] ( [CD_EMP],[CD_COND_COMPRA] )

ALTER TABLE [dbo].[EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL]  WITH CHECK ADD  
			CONSTRAINT [FK_EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL_EST_PED_ELTR_IT] FOREIGN KEY(  [CD_EMP],[CD_PROD])
REFERENCES [dbo].[EST_PED_CP_IT] ( [CD_EMP],[CD_PROD] )

ALTER TABLE [dbo].[EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL] ADD  CONSTRAINT [DF_EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL_CD_COND_COMPRA]  DEFAULT ((0)) FOR [CD_COND_COMPRA]
 
ALTER TABLE [dbo].[EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL] ADD  CONSTRAINT [DF_EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL_CD_PROD]  DEFAULT ((0)) FOR [CD_PROD]

ALTER TABLE [dbo].[EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL] ADD  CONSTRAINT [DF_EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL_VLR_PROD]  DEFAULT ((0)) FOR [VLR_PROD]

ALTER TABLE [dbo].[EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL] ADD  CONSTRAINT [DF_EST_PED_CP_PG_FORN_FORN_VINCULADO_COMPRA_CPL_PERC_DESC]  DEFAULT ((0)) FOR [PERC_DESC]

END

