IF NOT EXISTS ( SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'CONCILIACAO_CARTOES_FINAL')AND TYPE IN (N'U'))
BEGIN
CREATE TABLE [DBO].[CONCILIACAO_CARTOES_FINAL](
	[CD_CTR] 			BIGINT IDENTITY(1,1) NOT NULL,
	[CD_EMP] 			[INT] NOT NULL,
	[CD_FILIAL] 		[INT] NOT NULL,
	[CD_CX] 			[INT] NOT NULL,
	[NR_ECF] 			[INT] NOT NULL,
	[DT_VD] 			[DATETIME] NOT NULL,
	[VLR_VENDA] 		[MONEY] NULL,
	[COD_SIT] 			[INT] NULL,
	[IDT_REDE] 			[INT] NULL,
	[DESCR_REDE] 		[VARCHAR](50) COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS  NULL,
	[IDT_PRODUTO] 		[DECIMAL](5, 0)   NULL,
	[DESCR_PRODUTO] 	[VARCHAR](50) COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS  NULL,
	[DESCR_TRN] 		[DECIMAL](5, 0) NULL,
	[IDT_BANDEIRA] 		[DECIMAL](4, 0) NULL,
	[DESCR_BANDEIRA] 	[VARCHAR](50) COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS  NULL,
	[NUM_PARCELAS]		[INT] NULL,
	[CODIGO_RESP] 		[VARCHAR](3) COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS  NULL,
	[ESTADO_TRN]  		[DECIMAL](3, 0) NULL,
	[DATA_LANC] 		[VARCHAR](8) COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS  NULL,
	[DATAFISCAL] 		[VARCHAR](8) COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS  NULL,
	[COD_AUTORIZ] 		[VARCHAR](50)COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS   NOT NULL,
	[ST_VENDA] 			[INT] NOT NULL,
	[NSU_SITEF] 		[VARCHAR](9) NULL,
	[TP_OP_AUTOMATICO] 	[INT] NULL,
	[CD_CTR_PDV_VD_TEF] [BIGINT] NULL,
	[CD_VD] 			[BIGINT] NULL,
	[IDT_TRANSACAO] 	[BIGINT] NULL,
	[CD_CTR_CONCILIADOR][BIGINT] ,
 CONSTRAINT [PK_CONCILIACAO_CARTOES_FINAL] PRIMARY KEY CLUSTERED 
(
	[CD_CTR] 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 OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_CD_EMP')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_CD_EMP DEFAULT ((0)) FOR [CD_EMP]
END 
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_CD_FILIAL')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_CD_FILIAL DEFAULT ((0)) FOR [CD_FILIAL]
END
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_CD_CX')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_CD_CX DEFAULT ((0)) FOR [CD_CX]
END
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_NR_ECF')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_NR_ECF  DEFAULT ((0)) FOR [NR_ECF]
END
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_VLR_VENDA')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_VLR_VENDA DEFAULT ((0)) FOR [VLR_VENDA]
END
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_COD_AUTORIZ')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_COD_AUTORIZ DEFAULT ('') FOR [COD_AUTORIZ]
END
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_ST_VENDA')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_ST_VENDA DEFAULT ((0)) FOR [ST_VENDA]
END
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_TP_OP_AUTOMATICO')IS NULL 
BEGIN
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_TP_OP_AUTOMATICO DEFAULT ((1)) FOR [TP_OP_AUTOMATICO]
END
GO
IF NOT EXISTS (SELECT TOP 1 * FROM CONCILIACAO_CARTOES_FINAL ) 
 BEGIN 
	DROP TABLE CONCILIACAO_CARTOES_FINAL
 END 
 GO 
IF NOT EXISTS ( SELECT *
				  FROM SYS.OBJECTS
				 WHERE OBJECT_ID = OBJECT_ID(N'CONCILIACAO_CARTOES_FINAL')
				   AND TYPE IN (N'U')
		)
BEGIN
CREATE TABLE [DBO].[CONCILIACAO_CARTOES_FINAL](
	[CD_CTR] 			BIGINT IDENTITY(1,1) NOT NULL,
	[CD_EMP] 			[INT] NOT NULL,
	[CD_FILIAL] 		[INT] NOT NULL,
	[CD_CX] 			[INT] NOT NULL,
	[NR_ECF] 			[INT] NOT NULL,
	[DT_VD] 			[DATETIME] NOT NULL,
	[VLR_VENDA] 		[MONEY] NULL,
	[COD_SIT] 			[INT] NULL,
	[IDT_REDE] 			[INT] NULL,
	[DESCR_REDE] 		[VARCHAR](50) COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS  NULL,
	[IDT_PRODUTO] 		[DECIMAL](5, 0)   NULL,
	[DESCR_PRODUTO] 	[VARCHAR](50) COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS  NULL,
	[DESCR_TRN] 		[DECIMAL](5, 0) NULL,
	[IDT_BANDEIRA] 		[DECIMAL](4, 0) NULL,
	[DESCR_BANDEIRA] 	[VARCHAR](50) COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS  NULL,
	[NUM_PARCELAS]		[INT] NULL,
	[CODIGO_RESP] 		[VARCHAR](3) COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS  NULL,
	[ESTADO_TRN]  		[DECIMAL](3, 0) NULL,
	[DATA_LANC] 		[VARCHAR](8) COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS  NULL,
	[DATAFISCAL] 		[VARCHAR](8) COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS  NULL,
	[COD_AUTORIZ] 		[VARCHAR](50)COLLATE SQL_LATIN1_GENERAL_CP1_CI_AS   NOT NULL,
	[ST_VENDA] 			[INT] NOT NULL,
	[NSU_SITEF] 		[VARCHAR](9) NULL,
	[TP_OP_AUTOMATICO] 	[INT] NULL,
	[CD_CTR_PDV_VD_TEF] [BIGINT] NULL,
	[CD_VD] 			[BIGINT] NULL,
	[IDT_TRANSACAO] 	[BIGINT] NULL,
	[CD_CTR_CONCILIADOR][BIGINT] ,
 CONSTRAINT [PK_CONCILIACAO_CARTOES_FINAL] PRIMARY KEY CLUSTERED 
(
	[CD_CTR] 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 OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_CD_EMP')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_CD_EMP DEFAULT ((0)) FOR [CD_EMP]
END 
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_CD_FILIAL')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_CD_FILIAL DEFAULT ((0)) FOR [CD_FILIAL]
END
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_CD_CX')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_CD_CX DEFAULT ((0)) FOR [CD_CX]
END
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_NR_ECF')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_NR_ECF  DEFAULT ((0)) FOR [NR_ECF]
END
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_VLR_VENDA')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_VLR_VENDA DEFAULT ((0)) FOR [VLR_VENDA]
END
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_COD_AUTORIZ')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_COD_AUTORIZ DEFAULT ('') FOR [COD_AUTORIZ]
END
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_ST_VENDA')IS NULL 
BEGIN 
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_ST_VENDA DEFAULT ((0)) FOR [ST_VENDA]
END
GO
IF OBJECT_ID('DF_CONCILIACAO_CARTOES_FINAL_TP_OP_AUTOMATICO')IS NULL 
BEGIN
ALTER TABLE [DBO].[CONCILIACAO_CARTOES_FINAL] ADD  CONSTRAINT DF_CONCILIACAO_CARTOES_FINAL_TP_OP_AUTOMATICO DEFAULT ((1)) FOR [TP_OP_AUTOMATICO]
END
GO