IF EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LCF_LX_PAIS' AND COLUMN_NAME = 'LX_STATUS_REGISTRO')
	BEGIN
	IF EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LCF_LX_PAIS' AND COLUMN_NAME = 'LX_STATUS_REGISTRO' and COLUMN_DEFAULT is null)
		BEGIN
			EXEC('ALTER TABLE [dbo].[LCF_LX_PAIS] ADD  CONSTRAINT [DF_LX_STATUS_REGISTRO]  DEFAULT ((1)) FOR [LX_STATUS_REGISTRO]')
		END
	END
GO

IF EXISTS(SELECT *  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SERIES_NF' AND COLUMN_NAME = 'SEQUENCIAL' and CHARACTER_MAXIMUM_LENGTH <15)
	BEGIN
		EXEC('ALTER TABLE [dbo].[SERIES_NF] ALTER COLUMN [SEQUENCIAL] [varchar](15) NULL')
	END
	
GO

IF EXISTS(SELECT *  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'SERIES_NF' AND COLUMN_NAME = 'COD_SERIE_SINTEGRA' and CHARACTER_MAXIMUM_LENGTH <6)
	BEGIN
		EXEC('ALTER TABLE [dbo].[SERIES_NF] ALTER COLUMN [COD_SERIE_SINTEGRA] [varchar](6) NULL')
	END
	
GO

IF EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LCF_LX_UF' AND COLUMN_NAME = 'LX_STATUS_REGISTRO')
	BEGIN
	IF EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LCF_LX_UF' AND COLUMN_NAME = 'LX_STATUS_REGISTRO' and COLUMN_DEFAULT is null)
		BEGIN
			EXEC('ALTER TABLE [dbo].[LCF_LX_UF] ADD  CONSTRAINT [DF_UF_LX_STATUS_REGISTRO]  DEFAULT ((1)) FOR [LX_STATUS_REGISTRO]')
		END
	END
GO

IF OBJECT_ID('UNICO_VITRINE_PEDIDO') IS NULL
	BEGIN
		CREATE TABLE UNICO_VITRINE_PEDIDO
			(
				ID_PEDIDO INT IDENTITY(1, 1) NOT NULL, 
				CODIGO_FILIAL CHAR(6) NOT NULL, 
				TICKET CHAR(8) NOT NULL, 
				DATA_VENDA DATETIME NOT NULL, 
				PEDIDO VARCHAR(50) NOT NULL, 
				CODIGO_CLIENTE VARCHAR(14) NULL, 
				NSU VARCHAR(16) NULL, 
				VALOR_ORIGINAL_PEDIDO NUMERIC(9, 2) NOT NULL, 
				DESCONTO_LOJA NUMERIC(9, 2) NOT NULL, 
				CONFIRMADO BIT NOT NULL,
				CANCELADO BIT NOT NULL, 
				FRETE NUMERIC(9, 2) NULL,
			    CONSTRAINT PK_UNICO_VITRINE_PEDIDO PRIMARY KEY CLUSTERED (ID_PEDIDO)
			)
	END
GO
		
IF OBJECT_ID('UNICO_VITRINE_PEDIDO_ITEM') IS NULL
	BEGIN	
		CREATE TABLE UNICO_VITRINE_PEDIDO_ITEM
				(	ID_PEDIDO_ITEM INT IDENTITY(1, 1)NOT NULL, 
					ID_PEDIDO INT NOT NULL, 
					CODIGO_BARRA VARCHAR(25) NOT NULL, 
					QTDE INT NOT NULL, 
					PRECO_UNITARIO NUMERIC(9, 2) NOT NULL, 
					VENDEDOR CHAR(4) NULL,
					DESCONTO NUMERIC(9, 2) NULL,
					CONSTRAINT PK_UNICO_VITRINE_PEDIDO_ITEM PRIMARY KEY CLUSTERED (ID_PEDIDO_ITEM), 
					FOREIGN KEY (ID_PEDIDO) REFERENCES UNICO_VITRINE_PEDIDO (ID_PEDIDO) ON DELETE CASCADE 			
		)
	END
GO

IF NOT EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'UNICO_VITRINE_PEDIDO_ITEM' AND COLUMN_NAME = 'VENDEDOR')
	BEGIN
		ALTER TABLE [dbo].[UNICO_VITRINE_PEDIDO_ITEM] ADD [VENDEDOR] CHAR(4) NULL
	END
GO

IF NOT EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'UNICO_VITRINE_PEDIDO_ITEM' AND COLUMN_NAME = 'DESCONTO')
	BEGIN
			ALTER TABLE [dbo].[UNICO_VITRINE_PEDIDO_ITEM] ADD [DESCONTO] NUMERIC(9, 2) NULL
	END
GO

IF NOT EXISTS(SELECT TOP 1 1  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'UNICO_VITRINE_PEDIDO' AND COLUMN_NAME = 'FRETE')
	BEGIN
		ALTER TABLE [dbo].[UNICO_VITRINE_PEDIDO] ADD [FRETE] NUMERIC(9, 2) NULL
	END
GO

IF NOT EXISTS(SELECT TOP 1 1 FROM sys.tables A INNER JOIN sys.columns B ON A.Object_ID = B.Object_ID AND A.Name = 'TABELA_ALIQUOTA_IMPOSTO_ITEM' AND COLUMNPROPERTY(A.Object_ID, B.Name, 'IsIdentity') = 1)
BEGIN
begin try 

	BEGIN TRANSACTION
	CREATE TABLE dbo.Tmp_TABELA_ALIQUOTA_IMPOSTO_ITEM
					(
					ID_IMPOSTO_ITEM int NOT NULL IDENTITY (1, 1),
					UF char(2) NOT NULL,
					NCM_NBS char(10) NOT NULL,
					EXCESSAO_FISCAL_TABELA smallint NOT NULL,
					TABELA smallint NOT NULL,
					ALIQNAC_FEDERAL numeric(5, 2) NOT NULL,
					ALIQIMP_FEDERAL numeric(5, 2) NOT NULL,
					ALIQ_MUNICIPAL numeric(5, 2) NOT NULL,
					ALIQ_ESTADUAL numeric(5, 2) NOT NULL,
					INICIO_VIGENCIA smalldatetime NULL,
					FIM_VIGENCIA smalldatetime NULL,
					CHAVE varchar(6) NULL,
					VERSAO varchar(6) NOT NULL,
					FONTE varchar(34) NULL,
					DATA_PARA_TRANSFERENCIA datetime NULL,
					CONSTRAINT XPKTmp_TABELA_ALIQUOTA_IMPOSTO_ITEM PRIMARY KEY NONCLUSTERED (NCM_NBS, UF,VERSAO,EXCESSAO_FISCAL_TABELA),
					CONSTRAINT XAKTmp_TABELA_ALIQUOTA_IMPOSTO_ITEM UNIQUE CLUSTERED (ID_IMPOSTO_ITEM))  


	SET IDENTITY_INSERT dbo.Tmp_TABELA_ALIQUOTA_IMPOSTO_ITEM ON
	IF EXISTS(SELECT * FROM dbo.TABELA_ALIQUOTA_IMPOSTO_ITEM)
		EXEC('	INSERT INTO dbo.Tmp_TABELA_ALIQUOTA_IMPOSTO_ITEM (ID_IMPOSTO_ITEM, UF, NCM_NBS, EXCESSAO_FISCAL_TABELA, TABELA, ALIQNAC_FEDERAL, ALIQIMP_FEDERAL, ALIQ_MUNICIPAL, ALIQ_ESTADUAL, INICIO_VIGENCIA, FIM_VIGENCIA, CHAVE, VERSAO, FONTE, DATA_PARA_TRANSFERENCIA)
				SELECT ID_IMPOSTO_ITEM, UF, NCM_NBS, EXCESSAO_FISCAL_TABELA, TABELA, ALIQNAC_FEDERAL, ALIQIMP_FEDERAL, ALIQ_MUNICIPAL, ALIQ_ESTADUAL, INICIO_VIGENCIA, FIM_VIGENCIA, CHAVE, VERSAO, FONTE, DATA_PARA_TRANSFERENCIA FROM dbo.TABELA_ALIQUOTA_IMPOSTO_ITEM WITH (HOLDLOCK TABLOCKX)')
	SET IDENTITY_INSERT dbo.Tmp_TABELA_ALIQUOTA_IMPOSTO_ITEM OFF


	DROP TABLE dbo.TABELA_ALIQUOTA_IMPOSTO_ITEM

	EXECUTE sp_rename N'dbo.Tmp_TABELA_ALIQUOTA_IMPOSTO_ITEM', N'TABELA_ALIQUOTA_IMPOSTO_ITEM', 'OBJECT'
	EXECUTE sp_rename N'XPKTmp_TABELA_ALIQUOTA_IMPOSTO_ITEM', N'XPKTABELA_ALIQUOTA_IMPOSTO_ITEM', 'OBJECT'
	EXECUTE sp_rename N'dbo.XAKTmp_TABELA_ALIQUOTA_IMPOSTO_ITEM', N'XAKTABELA_ALIQUOTA_IMPOSTO_ITEM', 'OBJECT'

	commit tran

end try
begin catch

	declare @ErrorMessage  nvarchar(4000), @ErrorSeverity int, @ErrorState int

	select	@ErrorMessage = error_message(), @ErrorSeverity = error_severity(),	@ErrorState = error_state()
		
	if @@trancount > 0 rollback tran
		
	raiserror (@ErrorMessage, @ErrorSeverity, @ErrorState)

end catch

END
GO

IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LOJA_VENDA_PGTO' AND COLUMN_NAME = 'NUMERO_NF_ORDEM')
begin
	ALTER TABLE DBO.LOJA_VENDA_PGTO ADD NUMERO_NF_ORDEM CHAR(15) NULL
end
go

IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LOJA_VENDA_PGTO' AND COLUMN_NAME = 'SERIE_NF_ORDEM')
begin
	ALTER TABLE DBO.LOJA_VENDA_PGTO ADD SERIE_NF_ORDEM CHAR(6) NULL
END
go


/*
-----------------------------------------------------------------------------------------------------------------------------------------------------
--	ALTERAÇÕES DE BANCO PARA A VERSÃO 3.1.10.6
-----------------------------------------------------------------------------------------------------------------------------------------------------
*/

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS 
   WHERE TABLE_NAME = 'LOJA_PEDIDO' AND COLUMN_NAME = 'CHANNEL_ID' )
BEGIN
	ALTER TABLE [dbo].[LOJA_PEDIDO] ADD CHANNEL_ID VARCHAR(100)
END
go

IF NOT EXISTS (SELECT 1 FROM   INFORMATION_SCHEMA.COLUMNS
     WHERE  TABLE_NAME = 'LOJA_VENDA_TROCA_OMS' AND COLUMN_NAME = 'QTDE_DEVOLVIDA')
BEGIN
  ALTER TABLE [dbo].[LOJA_VENDA_TROCA_OMS]  ADD QTDE_DEVOLVIDA INT NULL
END
go

IF NOT EXISTS (SELECT 1 FROM   INFORMATION_SCHEMA.COLUMNS
     WHERE  TABLE_NAME = 'LOJA_VENDA_TROCA_OMS' AND COLUMN_NAME = 'QTDE_AVARIA')
BEGIN
  ALTER TABLE [dbo].[LOJA_VENDA_TROCA_OMS] ADD QTDE_AVARIA INT NULL
END
go
       
IF NOT EXISTS (SELECT 1 FROM   INFORMATION_SCHEMA.COLUMNS
    WHERE  TABLE_NAME = 'LOJA_VENDA_TROCA_OMS' AND COLUMN_NAME = 'QTDE_RECUSADA')
BEGIN
  ALTER TABLE [dbo].[LOJA_VENDA_TROCA_OMS] ADD QTDE_RECUSADA INT NULL
END
go
       
IF NOT EXISTS (SELECT 1 FROM   INFORMATION_SCHEMA.COLUMNS
      WHERE  TABLE_NAME = 'LOJA_VENDA_TROCA_OMS' AND COLUMN_NAME = 'QTDE_NAO_DEVOLVIDA')
BEGIN
  ALTER TABLE [dbo].[LOJA_VENDA_TROCA_OMS] ADD QTDE_NAO_DEVOLVIDA INT NULL
END
go
       
 IF NOT EXISTS (SELECT 1 FROM   INFORMATION_SCHEMA.COLUMNS
      WHERE  TABLE_NAME = 'LOJA_VENDA_TROCA_OMS' AND COLUMN_NAME = 'ID_EXCEPTION')
BEGIN
  ALTER TABLE [dbo].[LOJA_VENDA_TROCA_OMS] ADD ID_EXCEPTION VARCHAR(20) NULL
END
go
       
IF NOT EXISTS (SELECT 1  FROM   INFORMATION_SCHEMA.COLUMNS
      WHERE  TABLE_NAME = 'LOJA_VENDA_TROCA_OMS' AND COLUMN_NAME = 'INDICA_DEVOLUCAO')
BEGIN
  ALTER TABLE [dbo].[LOJA_VENDA_TROCA_OMS] ADD INDICA_DEVOLUCAO BIT NULL
END
go
       
/*
-----------------------------------------------------------------------------------------------------------------------------------------------------
--	ALTERAÇÕES DE BANCO PARA A VERSÃO 3.1.10.12
-----------------------------------------------------------------------------------------------------------------------------------------------------
*/

IF EXISTS (SELECT TOP 1 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'AREZZO_XML_COMPRAS_LP' )
	AND NOT EXISTS (SELECT TOP 1 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'AREZZO_XML_COMPRAS_LP' AND COLUMN_NAME = 'CODIGO_FILIAL')
BEGIN
	ALTER TABLE AREZZO_XML_COMPRAS_LP ADD CODIGO_FILIAL CHAR(06) NULL
END
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CLASSIF_FISCAL_IMPOSTO' AND COLUMN_NAME = 'INATIVO') 
BEGIN
	ALTER TABLE [dbo].[CLASSIF_FISCAL_IMPOSTO] ADD INATIVO BIT NULL DEFAULT (0)
END
GO

/*
-----------------------------------------------------------------------------------------------------------------------------------------------------
--	ALTERAÇÕES DE BANCO PARA A VERSÃO 3.1.10.14
-----------------------------------------------------------------------------------------------------------------------------------------------------
*/
IF NOT EXISTS(SELECT TOP 1 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'LOJA_INTERMEDIADORES_TRANSACAO')
BEGIN
	CREATE TABLE [dbo].[LOJA_INTERMEDIADORES_TRANSACAO](
		   [ID_INTERMEDIADOR] [int] NOT NULL,
		   [CNPJ] char(14) NOT NULL,
		   [NOME_FANTASIA] VARCHAR(40) NOT NULL,
		   [INATIVO] BIT DEFAULT 0 NOT NULl ,
		   [ENVIA_LOJA_VAREJO] [bit] DEFAULT 0 NOT NULL,
		   [DATA_PARA_TRANSFERENCIA]  DATETIME,
	CONSTRAINT [XPKLOJA_INTERMEDIADORES_TRANSACAO] PRIMARY KEY CLUSTERED 
	(
		   [ID_INTERMEDIADOR] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY],
    CONSTRAINT [XAKLOJA_INTERMEDIADORES_TRANSACAO] UNIQUE NONCLUSTERED 
    (
		        [CNPJ] 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
GO

IF NOT EXISTS( SELECT 1 FROM SYS.sysobjects WHERE NAME = 'LOJA_INTERMEDIADORES_TRANSACAO_FILIAL' )
BEGIN
	CREATE TABLE [dbo].[LOJA_INTERMEDIADORES_TRANSACAO_FILIAL](
				[ID_INTERMEDIADOR] [int] NOT NULL,
				[COD_FILIAL] [char](6) NOT NULL,
				[IDCADINTTRAN] [varchar](60) NOT NULL,
				[INATIVO] [bit] DEFAULT 0 NOT NULL,
				[CODIGO_FILIAL] [char](6) NOT NULL,
	CONSTRAINT [XPKLOJA_INTERMEDIADORES_TRANSACAO_FILIAL] PRIMARY KEY CLUSTERED 
    (
                [ID_INTERMEDIADOR] ASC, [COD_FILIAL] ASC, [IDCADINTTRAN] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
    CONSTRAINT [XAKLOJA_INTERMEDIADORES_TRANSACAO_FILIAL] UNIQUE NONCLUSTERED 
	(
			[IDCADINTTRAN] ASC,
			[COD_FILIAL] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
	CONSTRAINT [XFK10001_LOJA_INTERMEDIADORES_TRANSACAO_FILIAL] FOREIGN KEY
	(
		[ID_INTERMEDIADOR]
	)
	REFERENCES [dbo].[LOJA_INTERMEDIADORES_TRANSACAO] ([ID_INTERMEDIADOR]) ON DELETE CASCADE	
	) ON [PRIMARY]
END
GO

/*
-----------------------------------------------------------------------------------------------------------------------------------------------------
--	ALTERAÇÕES DE BANCO PARA A VERSÃO 3.1.10.16
-----------------------------------------------------------------------------------------------------------------------------------------------------
*/

IF NOT EXISTS( SELECT 1 FROM SYS.sysobjects WHERE NAME = 'LGPD_FINALIDADE' )
BEGIN
	CREATE TABLE [dbo].[LGPD_FINALIDADE](
			[ID_FINALIDADE] [int]  NOT NULL,
			[ID_AMBIENTE] [int] NOT NULL,
			[CODIGO] [varchar](40) NOT NULL,
			[DESCRICAO]	VARCHAR(256) NOT NULL,
			[ID_SERVER]	INT,
			[INATIVO]	BIT NOT NULL,	
			[DATA_PARA_TRANSFERENCIA]	DATETIME,
		CONSTRAINT [XPKRELGPD_FINALIDADE] PRIMARY KEY CLUSTERED 
		(
		[ID_FINALIDADE] ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
		) ON [PRIMARY]								
END				
GO

IF NOT EXISTS( SELECT 1 FROM SYS.sysobjects WHERE NAME = 'CLIENTE_VAR_FINALIDADE' )
BEGIN
	CREATE TABLE [dbo].[CLIENTE_VAR_FINALIDADE](
		[CODIGO_CLIENTE] [varchar](14) NOT NULL,
		[ID_FINALIDADE] [int]  NOT NULL,
		[CONSENTIDO] [BIT] NOT NULL,
		[CODIGO_FILIAL] [varchar](6) NOT NULL,
		[DATA_PARA_TRANSFERENCIA]	DATETIME,
	 CONSTRAINT [XPKCLIENTE_VAR_FINALIDADE] PRIMARY KEY CLUSTERED 
	(
		[CODIGO_CLIENTE]  ASC, [ID_FINALIDADE] ASC
	)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
	) ON [PRIMARY]

	ALTER TABLE [dbo].[CLIENTE_VAR_FINALIDADE]  WITH NOCHECK ADD  CONSTRAINT [XFK10001_CLIENTE_VAR_FINALIDADE] FOREIGN KEY([ID_FINALIDADE])
	REFERENCES [dbo].[LGPD_FINALIDADE] ([ID_FINALIDADE])

	ALTER TABLE [dbo].[CLIENTE_VAR_FINALIDADE] CHECK CONSTRAINT [XFK10001_CLIENTE_VAR_FINALIDADE]

	ALTER TABLE [dbo].[CLIENTE_VAR_FINALIDADE]  WITH NOCHECK ADD  CONSTRAINT [XFK10002_CLIENTE_VAR_FINALIDADE] FOREIGN KEY([CODIGO_CLIENTE])
	REFERENCES [dbo].[CLIENTES_VAREJO] ([CODIGO_CLIENTE])
	
	ALTER TABLE [dbo].[CLIENTE_VAR_FINALIDADE] CHECK CONSTRAINT [XFK10002_CLIENTE_VAR_FINALIDADE]			
END
GO

IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS 
	WHERE TABLE_NAME = 'CLIENTE_VAR_FINALIDADE' AND COLUMN_NAME = 'CODIGO_FILIAL')
BEGIN
	EXEC('ALTER TABLE CLIENTE_VAR_FINALIDADE ADD CODIGO_FILIAL varchar(6) NULL')
	EXEC('UPDATE CLIENTE_VAR_FINALIDADE SET
			CODIGO_FILIAL = (SELECT TOP 1 CODIGO_FILIAL FROM LOJA_VENDA ORDER BY DATA_DIGITACAO DESC)
		  WHERE CODIGO_FILIAL IS NULL')
	EXEC('ALTER TABLE CLIENTE_VAR_FINALIDADE ALTER COLUMN CODIGO_FILIAL varchar(6) not NULL')
END

--POSSP-8030 - Erro na execução do Script 10
if EXISTS(SELECT * FROM SYS.sysobjects WHERE NAME = 'LXI_LGPD_SOLICITACAO_RECEBIDA')
BEGIN
	DROP Trigger dbo.LXI_LGPD_SOLICITACAO_RECEBIDA
END

EXEC('
	IF NOT EXISTS(SELECT TOP 1 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ''FILIAIS'' AND COLUMN_NAME = ''EXCLUI_ICMS_PIS_COFINS_XML'')
	BEGIN
			ALTER TABLE FILIAIS ADD EXCLUI_ICMS_PIS_COFINS_XML BIT NULL
	END
')

EXEC('
	IF NOT EXISTS(SELECT TOP 1 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ''FILIAIS'' AND COLUMN_NAME = ''DATA_INICIO_EXCLUSAO_ICMS_PIS_COFINS_XML'')
	BEGIN
			ALTER TABLE FILIAIS ADD DATA_INICIO_EXCLUSAO_ICMS_PIS_COFINS_XML DATETIME NULL
	END
')

exec('
	IF NOT EXISTS(SELECT TOP 1 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ''FILIAIS_MODELOS'')
	BEGIN
		CREATE TABLE [dbo].[FILIAIS_MODELOS](
			[FILIAL] [varchar](25) NOT NULL,
			[NUMERO_MODELO_FISCAL] [varchar](3) NOT NULL,
		 CONSTRAINT [XFKFILIAIS_MODELOS] FOREIGN KEY([FILIAL]) REFERENCES [dbo].[FILIAIS] ([FILIAL]) ON DELETE CASCADE,
		 CONSTRAINT [XPKFILIAIS_MODELOS] PRIMARY KEY NONCLUSTERED 
		(
			[FILIAL] ASC,
			[NUMERO_MODELO_FISCAL] ASC
		)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
		) ON [PRIMARY]
	END
')

EXEC('
	IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ''PRODUTOS_BARRA'' AND COLUMN_NAME = ''CODIGO_BARRA_PADRAO'')
	BEGIN
		ALTER TABLE [dbo].[PRODUTOS_BARRA] ADD [CODIGO_BARRA_PADRAO] BIT
	END
')
