IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EST_PROD_NCM_CADASTRO]') AND type in (N'U'))
begin
CREATE TABLE dbo.EST_PROD_NCM_CADASTRO (
	NR_NCM varchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	DS_NCM varchar(MAX) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	PERC_ALIQ money NOT NULL,
	TIPO_TRIBUTACAO_PIS_COFINS int NOT NULL,
	CD_USU int NOT NULL,
	DT_CAD datetime NOT NULL,
	DT_ULT_ALT datetime NULL,
	PIS_COFINS int NOT NULL,
	TIPO_TRIBUTACAO_PIS_COFINS_ENTRADA int NOT NULL,
	PERC_ALIQ_IMPORTADO money NULL,
	NR_CEST varchar(20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	CD_CST_SAIDA_PIS int NULL,
	CD_CST_SAIDA_COFINS int NULL,
	CD_CST_ENTRADA_PIS int NULL,
	CD_CST_ENTRADA_COFINS int NULL,
	STS_NCM int NOT NULL,
	CONSTRAINT PK_EST_PROD_NCM_CADASTRO PRIMARY KEY (NR_NCM)
);
END
ELSE

IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'NR_NCM')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD NR_NCM varchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL  DEFAULT '';
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'DS_NCM')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD DS_NCM varchar(500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL  DEFAULT '';
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'PERC_ALIQ')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD PERC_ALIQ money NOT NULL  DEFAULT 0;
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'TIPO_TRIBUTACAO_PIS_COFINS')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD TIPO_TRIBUTACAO_PIS_COFINS int NOT NULL DEFAULT 0;
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'CD_USU')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD CD_USU int NOT NULL  DEFAULT 0;
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'DT_CAD')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD DT_CAD datetime NOT NULL DEFAULT 0;
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'DT_ULT_ALT')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD DT_ULT_ALT datetime NULL;
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'PIS_COFINS')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD PIS_COFINS int NOT NULL DEFAULT 0;
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'TIPO_TRIBUTACAO_PIS_COFINS_ENTRADA')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD TIPO_TRIBUTACAO_PIS_COFINS_ENTRADA int NOT NULL DEFAULT 0;
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'PERC_ALIQ_IMPORTADO')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD PERC_ALIQ_IMPORTADO money NULL;
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'NR_CEST')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD NR_CEST varchar(20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL;
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'CD_CST_SAIDA_PIS')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD CD_CST_SAIDA_PIS int NULL;
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'CD_CST_SAIDA_COFINS')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD CD_CST_SAIDA_COFINS int NULL;
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'CD_CST_ENTRADA_PIS')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD CD_CST_ENTRADA_PIS int NULL;
	END
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'CD_CST_ENTRADA_COFINS')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD CD_CST_ENTRADA_COFINS int NULL;
	END	
GO
IF NOT EXISTS(SELECT 1 FROM syscolumns WHERE ID = OBJECT_ID('EST_PROD_NCM_CADASTRO') AND NAME = 'STS_NCM')
	BEGIN
		ALTER TABLE dbo.EST_PROD_NCM_CADASTRO ADD STS_NCM int NOT NULL;
	END	

