IF NOT EXISTS (SELECT * FROM SYS.TABLES WHERE NAME = 'PDV_VD_CHQS_TROCO_RELACIONADA') 
begin 
CREATE TABLE [dbo].[PDV_VD_CHQS_TROCO_RELACIONADA](
	[CD_EMP] [int] NOT NULL,
	[CD_FILIAL] [int] NOT NULL,
	[CD_VD] [int] NOT NULL,
	[CD_VD_RELACIONADA] [int] NOT NULL,
 CONSTRAINT [PK_PDV_VD_CHQS_TROCO_RELACIONADA] PRIMARY KEY CLUSTERED 
(
	[CD_EMP] ASC,
	[CD_FILIAL] ASC,
	[CD_VD] ASC,
	[CD_VD_RELACIONADA] 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].[PDV_VD_CHQS_TROCO_RELACIONADA]  WITH CHECK ADD  CONSTRAINT [FK_PDV_VD_CHQS_TROCO_RELACIONADA_PDV_VD] FOREIGN KEY([CD_VD], [CD_FILIAL], [CD_EMP])
REFERENCES [dbo].[PDV_VD] ([CD_VD], [CD_FILIAL], [CD_EMP])

ALTER TABLE [dbo].[PDV_VD_CHQS_TROCO_RELACIONADA] CHECK CONSTRAINT [FK_PDV_VD_CHQS_TROCO_RELACIONADA_PDV_VD]

ALTER TABLE [dbo].[PDV_VD_CHQS_TROCO_RELACIONADA]  WITH CHECK ADD  CONSTRAINT [FK_PDV_VD_CHQS_TROCO_RELACIONADA_PDV_VD_VD_RELACIONADA] FOREIGN KEY([CD_VD_RELACIONADA], [CD_FILIAL], [CD_EMP])
REFERENCES [dbo].[PDV_VD] ([CD_VD], [CD_FILIAL], [CD_EMP])

ALTER TABLE [dbo].[PDV_VD_CHQS_TROCO_RELACIONADA] CHECK CONSTRAINT [FK_PDV_VD_CHQS_TROCO_RELACIONADA_PDV_VD_VD_RELACIONADA]
end

