/****** Object:  Table [dbo].[EST_SETORIZACAO_END]    Script Date: 11/26/2013 15:36:08 ******/
IF  NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EST_SETORIZACAO_END]') AND type in (N'U'))
begin
	CREATE TABLE [dbo].[EST_SETORIZACAO_END](
		[CD_EMP] [int] NOT NULL,
		[CD_END] [int] NOT NULL,
		[CD_SETOR] [int] NOT NULL,
		[CD_CTR] [int] NOT NULL,
		[ENDERECO] [varchar](50) NULL,
		[BAIRRO] [varchar](50) NULL,
		[CIDADE] [varchar](50) NULL,
		[UF] [varchar](2) NULL,
		[DDD] [varchar](10) NULL,
		[TELEFONE] [varchar](20) NULL,
		[CEP] [varchar](20) NULL,
		[CD_USU] [int] NULL,
		[DT_CAD] [datetime] NULL,
		[UF_DOC] [varchar](2) NULL,
	 CONSTRAINT [PK_EST_SETORIZACAO_END] PRIMARY KEY CLUSTERED 
	(
		[CD_EMP] ASC,
		[CD_END] 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_SETORIZACAO_END]  WITH CHECK ADD  CONSTRAINT [FK_EST_SETORIZACAO_END_EST_SETORIZACAO_SETOR] FOREIGN KEY([CD_EMP], [CD_SETOR])
	REFERENCES [dbo].[EST_SETORIZACAO_SETOR] ([CD_EMP], [CD_SETOR])

	ALTER TABLE [dbo].[EST_SETORIZACAO_END] CHECK CONSTRAINT [FK_EST_SETORIZACAO_END_EST_SETORIZACAO_SETOR]

	ALTER TABLE [dbo].[EST_SETORIZACAO_END]  WITH CHECK ADD  CONSTRAINT [FK_EST_SETORIZACAO_END_GLB_MEDICOS1] FOREIGN KEY([CD_EMP], [CD_CTR])
	REFERENCES [dbo].[GLB_MEDICOS] ([CD_EMP], [CD_CTR])

	ALTER TABLE [dbo].[EST_SETORIZACAO_END] CHECK CONSTRAINT [FK_EST_SETORIZACAO_END_GLB_MEDICOS1]
end

/****** Object:  Table [dbo].[EST_SETORIZACAO_END_EST_PROD_FABRIC]    Script Date: 11/26/2013 15:36:08 ******/
IF  NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EST_SETORIZACAO_END_EST_PROD_FABRIC]') AND type in (N'U'))
begin
	CREATE TABLE [dbo].[EST_SETORIZACAO_END_EST_PROD_FABRIC](
		[CD_EMP] [int] NOT NULL,
		[CD_END] [int] NOT NULL,
		[CD_FABRIC] [int] NOT NULL,
	 CONSTRAINT [PK_EST_SETORIZACAO_END_EST_PROD_FABRIC] PRIMARY KEY CLUSTERED 
	(
		[CD_EMP] ASC,
		[CD_END] ASC,
		[CD_FABRIC] 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_SETORIZACAO_END_EST_PROD_FABRIC]  WITH CHECK ADD  CONSTRAINT [FK_EST_SETORIZACAO_END_EST_PROD_FABRIC_EST_PROD_FABRIC] FOREIGN KEY([CD_FABRIC])
REFERENCES [dbo].[EST_PROD_FABRIC] ([CD_FABRIC])

ALTER TABLE [dbo].[EST_SETORIZACAO_END_EST_PROD_FABRIC] CHECK CONSTRAINT [FK_EST_SETORIZACAO_END_EST_PROD_FABRIC_EST_PROD_FABRIC]

ALTER TABLE [dbo].[EST_SETORIZACAO_END_EST_PROD_FABRIC]  WITH CHECK ADD  CONSTRAINT [FK_EST_SETORIZACAO_END_EST_PROD_FABRIC_EST_SETORIZACAO_END] FOREIGN KEY([CD_EMP], [CD_END])
REFERENCES [dbo].[EST_SETORIZACAO_END] ([CD_EMP], [CD_END])

ALTER TABLE [dbo].[EST_SETORIZACAO_END_EST_PROD_FABRIC] CHECK CONSTRAINT [FK_EST_SETORIZACAO_END_EST_PROD_FABRIC_EST_SETORIZACAO_END]

end

/****** Object:  Table [dbo].[EST_SETORIZACAO_END_PDV_VD_IT]    Script Date: 11/26/2013 15:36:08 ******/
IF  NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EST_SETORIZACAO_END_PDV_VD_IT]') AND type in (N'U'))
begin
	CREATE TABLE [dbo].[EST_SETORIZACAO_END_PDV_VD_IT](
	[CD_EMP] [int] NOT NULL,
	[CD_FILIAL] [int] NOT NULL,
	[CD_VD] [int] NOT NULL,
	[CD_IT] [int] NOT NULL,
	[CD_END] [int] NOT NULL,
	CONSTRAINT [PK_EST_SETORIZACAO_END_PDV_VD_IT] PRIMARY KEY CLUSTERED 
	(
	[CD_EMP] ASC,
	[CD_FILIAL] ASC,
	[CD_VD] ASC,
	[CD_IT] ASC,
	[CD_END] 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_SETORIZACAO_END_PDV_VD_IT]  WITH CHECK ADD  CONSTRAINT [FK_EST_SETORIZACAO_END_PDV_VD_IT_EST_SETORIZACAO_END] FOREIGN KEY([CD_EMP], [CD_END])
	REFERENCES [dbo].[EST_SETORIZACAO_END] ([CD_EMP], [CD_END])

	ALTER TABLE [dbo].[EST_SETORIZACAO_END_PDV_VD_IT] CHECK CONSTRAINT [FK_EST_SETORIZACAO_END_PDV_VD_IT_EST_SETORIZACAO_END]
end


/****** Object:  Table [dbo].[EST_SETORIZACAO_SETOR]    Script Date: 11/26/2013 15:36:08 ******/
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EST_SETORIZACAO_SETOR]') AND type in (N'U'))
begin
	CREATE TABLE [dbo].[EST_SETORIZACAO_SETOR](
	[CD_EMP] [int] NOT NULL,
	[CD_SETOR] [int] NOT NULL,
	[DS_SETOR] [varchar](50) NULL,
	[CD_USU] [int] NULL,
	[DT_CAD] [datetime] NULL,
	CONSTRAINT [PK_EST_SETORIZACAO_SETOR] PRIMARY KEY CLUSTERED 
	(
	[CD_EMP] ASC,
	[CD_SETOR] 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