IF OBJECT_ID('EST_NF_SAI_LOTE_ENVIO_WMS_CPL') IS NULL 
begin
CREATE TABLE [dbo].[EST_NF_SAI_LOTE_ENVIO_WMS_CPL](
	[CD_CTR] [int] NOT NULL,
	[CD_EMP] [int] NOT NULL,
	[CD_FILIAL] [int] NOT NULL,
	[CD_NF] [int] NOT NULL,
 CONSTRAINT [PK_EST_NF_SAI_LOTE_ENVIO_WMS_CPL] PRIMARY KEY CLUSTERED 
(
	[CD_CTR] ASC,
	[CD_EMP] ASC,
	[CD_FILIAL] ASC,
	[CD_NF] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
 CONSTRAINT [IX_EST_NF_SAI_LOTE_ENVIO_WMS_CPL] UNIQUE NONCLUSTERED 
(
	[CD_EMP] ASC,
	[CD_FILIAL] ASC,
	[CD_NF] ASC
)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_NF_SAI_LOTE_ENVIO_WMS_CPL] ADD  CONSTRAINT [DF_EST_NF_SAI_LOTE_ENVIO_WMS_CPL_CD_CTR]  DEFAULT ((0)) FOR [CD_CTR]

ALTER TABLE [dbo].[EST_NF_SAI_LOTE_ENVIO_WMS_CPL] ADD  CONSTRAINT [DF_EST_NF_SAI_LOTE_ENVIO_WMS_CPL_CD_EMP]  DEFAULT ((0)) FOR [CD_EMP]

ALTER TABLE [dbo].[EST_NF_SAI_LOTE_ENVIO_WMS_CPL] ADD  CONSTRAINT [DF_EST_NF_SAI_LOTE_ENVIO_WMS_CPL_CD_FILIAL]  DEFAULT ((0)) FOR [CD_FILIAL]

ALTER TABLE [dbo].[EST_NF_SAI_LOTE_ENVIO_WMS_CPL] ADD  CONSTRAINT [DF_EST_NF_SAI_LOTE_ENVIO_WMS_CPL_CD_NF]  DEFAULT ((0)) FOR [CD_NF]

ALTER TABLE [dbo].[EST_NF_SAI_LOTE_ENVIO_WMS_CPL]  WITH CHECK ADD  CONSTRAINT [FK_EST_NF_SAI_LOTE_ENVIO_WMS_CPL_EST_NF_SAI] FOREIGN KEY([CD_EMP], [CD_FILIAL], [CD_NF])
REFERENCES [dbo].[EST_NF_SAI] ([CD_EMP], [CD_FILIAL], [CD_NF])

ALTER TABLE [dbo].[EST_NF_SAI_LOTE_ENVIO_WMS_CPL] CHECK CONSTRAINT [FK_EST_NF_SAI_LOTE_ENVIO_WMS_CPL_EST_NF_SAI]

ALTER TABLE [dbo].[EST_NF_SAI_LOTE_ENVIO_WMS_CPL]  WITH CHECK ADD  CONSTRAINT [FK_EST_NF_SAI_LOTE_ENVIO_WMS_CPL_EST_NF_SAI_LOTE_ENVIO_WMS] FOREIGN KEY([CD_CTR])
REFERENCES [dbo].[EST_NF_SAI_LOTE_ENVIO_WMS] ([CD_CTR])

ALTER TABLE [dbo].[EST_NF_SAI_LOTE_ENVIO_WMS_CPL] CHECK CONSTRAINT [FK_EST_NF_SAI_LOTE_ENVIO_WMS_CPL_EST_NF_SAI_LOTE_ENVIO_WMS]
end

