
/*------ ATD 214567 ------*/
/* Cria a tabela ItemFaturado */
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'ITEMFATURADO' AND type = 'U') AND
   EXISTS (SELECT COUNT(Item) TotItem FROM ItemFaturado HAVING COUNT(Item) = 0)
   DROP TABLE ITEMFATURADO
GO

IF NOT EXISTS (SELECT name FROM sysobjects WHERE name = 'ITEMFATURADO' AND type = 'U')
   CREATE TABLE [dbo].[ITEMFATURADO](
   	[Usuario] [varchar](254) NULL,
	[Estacao] [varchar](254) NULL,
	[Filial] [char](2) NULL,
	[Pedido] [int] NULL,
	[Codpro] [char](5) NULL,
	[Item] [int] NOT NULL,
	[Quant] [numeric](13, 3) NULL,
	[Reservado] [datetime] NOT NULL,
	[Faturado] [datetime] NULL,
	[Cancelado] [datetime] NULL,
	[NUMEROLISTA] [int] NULL,
	[ORDEMENTREGA] [int] NULL,
	[SITMANUT] [char](1) NULL,
	[VIAORDEMSEPARACAO] [numeric](3, 0) NULL,
	[NUMPEDTRANSFERENCIA] [int] NULL,
	[DATAMONTAGEM] [datetime] NULL,
	[NUMORD] [int] NULL,
	[DataOrdemSeparacao] [datetime] NULL,
 CONSTRAINT [PK_ITEMFATURADO] PRIMARY KEY CLUSTERED 
(
	[Item] ASC,
	[Reservado] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


IF EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'Usuario' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMFATURADO')) AND
   NOT EXISTS (SELECT name FROM sysobjects WHERE name = 'DF__ITEMFATUR__Usuar__75667A86' AND type = 'D')
   ALTER TABLE [dbo].[ITEMFATURADO] ADD  CONSTRAINT [DF__ITEMFATUR__Usuar__75667A86]  DEFAULT (' ') FOR [Usuario]
GO

IF EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'Estacao' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMFATURADO')) AND
   NOT EXISTS (SELECT name FROM sysobjects WHERE name = 'DF__ITEMFATUR__Estac__765A9EBF' AND type = 'D')
   ALTER TABLE [dbo].[ITEMFATURADO] ADD  CONSTRAINT [DF__ITEMFATUR__Estac__765A9EBF]  DEFAULT (' ') FOR [Estacao]
GO

IF EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'Filial' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMFATURADO')) AND
   NOT EXISTS (SELECT name FROM sysobjects WHERE name = 'DF__ITEMFATUR__Filia__774EC2F8' AND type = 'D')
   ALTER TABLE [dbo].[ITEMFATURADO] ADD  CONSTRAINT [DF__ITEMFATUR__Filia__774EC2F8]  DEFAULT (' ') FOR [Filial]
GO

IF EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'Pedido' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMFATURADO')) AND
   NOT EXISTS (SELECT name FROM sysobjects WHERE name = 'DF__ITEMFATUR__Pedid__7842E731' AND type = 'D')
   ALTER TABLE [dbo].[ITEMFATURADO] ADD  CONSTRAINT [DF__ITEMFATUR__Pedid__7842E731]  DEFAULT (7) FOR [Pedido]
GO

IF EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'Codpro' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMFATURADO')) AND
   NOT EXISTS (SELECT name FROM sysobjects WHERE name = 'DF__ITEMFATUR__Codpr__79370B6A' AND type = 'D')
   ALTER TABLE [dbo].[ITEMFATURADO] ADD  CONSTRAINT [DF__ITEMFATUR__Codpr__79370B6A]  DEFAULT (' ') FOR [Codpro]
GO

IF EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'Item' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMFATURADO')) AND
   NOT EXISTS (SELECT name FROM sysobjects WHERE name = 'DF__ITEMFATURA__Item__7A2B2FA3' AND type = 'D')
   ALTER TABLE [dbo].[ITEMFATURADO] ADD  CONSTRAINT [DF__ITEMFATURA__Item__7A2B2FA3]  DEFAULT (7) FOR [Item]
GO

IF EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'NUMEROLISTA' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMFATURADO')) AND
   NOT EXISTS (SELECT name FROM sysobjects WHERE name = 'DF__ITEMFATUR__NUMER__7C137815' AND type = 'D')
   ALTER TABLE [dbo].[ITEMFATURADO] ADD  CONSTRAINT [DF__ITEMFATUR__NUMER__7C137815]  DEFAULT (0) FOR [NUMEROLISTA]
GO

IF EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'SITMANUT' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMFATURADO')) AND
   NOT EXISTS (SELECT name FROM sysobjects WHERE name LIKE 'DF__ITEMFATUR__SITMA__%' AND type = 'D')
   ALTER TABLE [dbo].[ITEMFATURADO] ADD  DEFAULT ('') FOR [SITMANUT]
GO

IF EXISTS (SELECT 1 FROM SYSCOLUMNS WHERE name = 'VIAORDEMSEPARACAO' and syscolumns.id IN (SELECT ID FROM SYSOBJECTS WHERE xtype='U' AND name='ITEMFATURADO')) AND
   NOT EXISTS (SELECT name FROM sysobjects WHERE name LIKE 'DF__ITEMFATUR__VIAOR__%' AND type = 'D')
   ALTER TABLE [dbo].[ITEMFATURADO] ADD  DEFAULT (0) FOR [VIAORDEMSEPARACAO]
GO