USE BDENTER
GO

CREATE SCHEMA WEB
GO

CREATE TABLE [web].[ConfiguracaoFilial](
	[CodigoFilial] [nvarchar](50) NOT NULL,
	[Cor] [nvarchar](10) NULL,
	[ExtensaoLogo] [nvarchar](5) NULL,
 CONSTRAINT [PK_ConfiguracaoFilial] PRIMARY KEY CLUSTERED 
(
	[CodigoFilial] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [web].[TipoPesquisa](
	[Id] [int] NOT NULL,
	[Descricao] [nvarchar](100) NOT NULL 
 CONSTRAINT [PK_TipoPesquisa] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

insert [web].[TipoPesquisa] values (1,'Produto')
insert [web].[TipoPesquisa] values (2,'Cliente')

GO
GO

/****** Object:  Table [web].[HistoricoPesquisa]    Script Date: 04/14/2015 16:46:43 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [web].[HistoricoPesquisa](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[TipoPesquisa] [int] NOT NULL,
	[Usuario] [int] NOT NULL,
	[filtro] [nvarchar](4000) NOT NULL,
	[Data] [datetime] NULL,
	[Selecionado] [bit] NULL,
 CONSTRAINT [PK_PesquisaProduto] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [web].[HistoricoPesquisa]  WITH CHECK ADD  CONSTRAINT [FK_HistoricoPesquisa_TipoPesquisa] FOREIGN KEY([TipoPesquisa])
REFERENCES [web].[TipoPesquisa] ([Id])
GO

ALTER TABLE [web].[HistoricoPesquisa] CHECK CONSTRAINT [FK_HistoricoPesquisa_TipoPesquisa]
GO

update controlaoid set delta = 1

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='AREAPRECAD')) 
	ALTER TABLE AREAPRECAD ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_AREAPRECAD1')
	CREATE INDEX IDX_AREAPRECAD1 on AREAPRECAD(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='CONCORRCAD')) 
	ALTER TABLE CONCORRCAD ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_CONCORRCAD1')
	CREATE INDEX IDX_CONCORRCAD1 on CONCORRCAD(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='HISTATECAD')) 
	ALTER TABLE HISTATECAD ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_HISTATECAD1')
	CREATE INDEX IDX_HISTATECAD1 on HISTATECAD(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='HISTENTCAD')) 
	ALTER TABLE HISTENTCAD ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_HISTENTCAD3')
	CREATE INDEX IDX_HISTENTCAD3 on HISTENTCAD(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='ITEMCONHECIMENTOTRANSPORTE')) 
	ALTER TABLE ITEMCONHECIMENTOTRANSPORTE ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_ITEMCONHECIMENTOTRANSPORTE1')
	CREATE INDEX IDX_ITEMCONHECIMENTOTRANSPORTE1 on ITEMCONHECIMENTOTRANSPORTE(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='ITEMFATURADO')) 
	ALTER TABLE ITEMFATURADO ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_ITEMFATURADO1')
	CREATE INDEX IDX_ITEMFATURADO1 on ITEMFATURADO(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='ITNFSAICAD')) 
	ALTER TABLE ITNFSAICAD ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_ITNFSAICAD5')
	CREATE INDEX IDX_ITNFSAICAD5 on ITNFSAICAD(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='LOTE')) 
	ALTER TABLE LOTE ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_LOTE1')
	CREATE INDEX IDX_LOTE1 on LOTE(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='LOTEPROMOCAO')) 
	ALTER TABLE LOTEPROMOCAO ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_LOTEPROMOCAO1')
	CREATE INDEX IDX_LOTEPROMOCAO1 on LOTEPROMOCAO(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='OFERTASEST')) 
	ALTER TABLE OFERTASEST ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_OFERTASEST1')
	CREATE INDEX IDX_OFERTASEST1 on OFERTASEST(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='OFERTASFILIAL')) 
	ALTER TABLE OFERTASFILIAL ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_OFERTASFILIAL1')
	CREATE INDEX IDX_OFERTASFILIAL1 on OFERTASFILIAL(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='PERDVENCAD')) 
	ALTER TABLE PERDVENCAD ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_PERDVENCAD1')
	CREATE INDEX IDX_PERDVENCAD1 on PERDVENCAD(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='PRODUTOFORMULA')) 
	ALTER TABLE PRODUTOFORMULA ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_PRODUTOFORMULA1')
	CREATE INDEX IDX_PRODUTOFORMULA1 on PRODUTOFORMULA(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='PRODREFCAD')) 
	ALTER TABLE PRODREFCAD ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_PRODREFCAD1')
	CREATE INDEX IDX_PRODREFCAD1 on PRODREFCAD(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='RESERVALOTE')) 
	ALTER TABLE RESERVALOTE ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_RESERVALOTE1')
	CREATE INDEX IDX_RESERVALOTE1 on RESERVALOTE(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='SAIDALOTE')) 
	ALTER TABLE SAIDALOTE ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_SAIDALOTE1')
	CREATE INDEX IDX_SAIDALOTE1 on SAIDALOTE(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='PRODCONEST')) 
	ALTER TABLE PRODCONEST ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_PRODCONEST1')
	CREATE INDEX IDX_PRODCONEST1 on PRODCONEST(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='ITEMFILEST')) 
	ALTER TABLE ITEMFILEST ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_ITEMFILEST3')
	CREATE INDEX IDX_ITEMFILEST3 on ITEMFILEST(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='ITEMDEPEST')) 
	ALTER TABLE ITEMDEPEST ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_ITEMDEPEST1')
	CREATE INDEX IDX_ITEMDEPEST1 on ITEMDEPEST(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='ITNFENTCAD')) 
	ALTER TABLE ITNFENTCAD ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_ITNFENTCAD4')
	CREATE INDEX IDX_ITNFENTCAD4 on ITNFENTCAD(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='SIMILARCAD')) 
	ALTER TABLE SIMILARCAD ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_SIMILARCAD1')
	CREATE INDEX IDX_SIMILARCAD1 on SIMILARCAD(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='PRODUTOAGREGADO')) 
	ALTER TABLE PRODUTOAGREGADO ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_PRODUTOAGREGADO1')
	CREATE INDEX IDX_PRODUTOAGREGADO1 on PRODUTOAGREGADO(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='ITEMCONHECIMENTOTRANSPORTE')) 
	ALTER TABLE ITEMCONHECIMENTOTRANSPORTE ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_ITEMCONHECIMENTOTRANSPORTE1')
	CREATE INDEX IDX_ITEMCONHECIMENTOTRANSPORTE1 on ITEMCONHECIMENTOTRANSPORTE(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='ITEMFORCAD')) 
	ALTER TABLE ITEMFORCAD ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_ITEMFORCAD1')
	CREATE INDEX IDX_ITEMFORCAD1 on ITEMFORCAD(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='VENDATEXTOPADRAO')) 
	ALTER TABLE VENDATEXTOPADRAO ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_VENDATEXTOPADRAO')
	CREATE INDEX IDX_VENDATEXTOPADRAO on VENDATEXTOPADRAO(ID)
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='PREVPARCELA')) 
	ALTER TABLE PREVPARCELA ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_PREVPARCELA2')
	CREATE INDEX IDX_PREVPARCELA2 on PREVPARCELA(ID)
GO

IF NOT EXISTS (SELECT 1 FROM SysColumns WHERE Name = 'DESCONTOENTREGA' AND SysColumns.ID IN (SELECT ID FROM SysObjects WHERE xType = 'U' AND Name = 'ITEMCLICOMPLEMENTO'))
	ALTER TABLE ITEMCLICOMPLEMENTO ADD DESCONTOENTREGA NUMERIC(15,2) NOT NULL DEFAULT(0)
GO

IF NOT EXISTS (SELECT 1 FROM SysColumns WHERE Name = 'DescontoPorPercentual' AND SysColumns.ID IN (SELECT ID FROM SysObjects WHERE xType = 'U' AND Name = 'PediCliComplemento'))
	ALTER TABLE PediCliComplemento ADD DescontoPorPercentual NUMERIC(1)
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'ITEMCLICOMPLEMENTO' AND COLUMN_NAME = 'NFCI') 
	ALTER TABLE dbo.ITEMCLICOMPLEMENTO add NFCI VARCHAR(100)
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'ITEMCLICOMPLEMENTO' AND COLUMN_NAME = 'FCI_NUMORDORIGEM') 
	ALTER TABLE dbo.ITEMCLICOMPLEMENTO add FCI_NUMORDORIGEM INT
GO

IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'ITEMCLICOMPLEMENTO' AND COLUMN_NAME = 'FCI_RITEMORIGEM') 
	ALTER TABLE dbo.ITEMCLICOMPLEMENTO add FCI_RITEMORIGEM CHAR(5)
GO

IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'DESCICMSSUFRAMA' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMCLICOMPLEMENTO')) 
	ALTER TABLE ITEMCLICOMPLEMENTO ADD DESCICMSSUFRAMA NUMERIC(15,2) not null default 0
GO

IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'PercFrete' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMCLICOMPLEMENTO')) 
	ALTER TABLE ITEMCLICOMPLEMENTO ADD PercFrete numeric(5,2) default 0 NOT NULL
GO

IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'PrecoOferta' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMCLICAD')) 
	ALTER TABLE ITEMCLICAD ADD PrecoOferta numeric(15,2) default 0 NOT NULL
GO

IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'Origem' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMFILEST')) 
	ALTER TABLE ITEMFILEST ADD Origem CHAR(1) default '' NOT NULL
GO

IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'BLOQUEIARETIRADAFUT' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='ITEMFILEST'))
    ALTER TABLE ITEMFILEST ADD BLOQUEIARETIRADAFUT NUMERIC(1) NULL
GO

IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'BLOQUEIATRANSF' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='ITEMFILEST'))
    ALTER TABLE ITEMFILEST ADD BLOQUEIATRANSF NUMERIC(1) NULL
GO

IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'BLOQUEARVENDA' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='ITEMFILEST'))
    ALTER TABLE ITEMFILEST ADD BLOQUEARVENDA NUMERIC(1) NULL
GO

IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'BloqueiaAbastecimento' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='ITEMFILEST'))
    ALTER TABLE ITEMFILEST ADD BloqueiaAbastecimento NUMERIC(1) NULL
GO

IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'Reserva' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='HISTORICOPEDIDO'))
    ALTER TABLE HISTORICOPEDIDO ADD Reserva CHAR(1) NULL
GO

-- Criação de identificador único para o registro da tabela
IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='FRETPEDCAD')) 
	ALTER TABLE FRETPEDCAD ADD ID INT IDENTITY NOT NULL
GO
IF NOT EXISTS (SELECT 1 FROM SYSINDEXES WHERE name='IDX_FRETPEDCAD')
	CREATE INDEX IDX_FRETPEDCAD on FRETPEDCAD(ID)
GO

IF NOT EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE NAME = 'ID' AND SYSCOLUMNS.ID IN (SELECT ID FROM SYSOBJECTS WHERE XTYPE='U' AND NAME='IMAGEMPRODUTO'))  
BEGIN
	ALTER TABLE IMAGEMPRODUTO ADD ID INT IDENTITY NOT NULL
	ALTER TABLE IMAGEMPRODUTO ADD CONSTRAINT PK_IMAGEMPRODUTO PRIMARY KEY CLUSTERED(ID)
END
