
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_EST_NF_NFE_INU_VALIDADA_EST_NF_NFE_INU]') AND parent_object_id = OBJECT_ID(N'[dbo].[EST_NF_NFE_INU_VALIDADA]'))
ALTER TABLE [dbo].[EST_NF_NFE_INU_VALIDADA] DROP CONSTRAINT [FK_EST_NF_NFE_INU_VALIDADA_EST_NF_NFE_INU]
GO

IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_EST_NF_NFE_INU_VALIDADA_CD_INU]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[EST_NF_NFE_INU_VALIDADA] DROP CONSTRAINT [DF_EST_NF_NFE_INU_VALIDADA_CD_INU]
END

GO

IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF__EST_NF_NF__DT_CA__48501604]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[EST_NF_NFE_INU_VALIDADA] DROP CONSTRAINT [DF__EST_NF_NF__DT_CA__48501604]
END

GO


/****** Object:  Table [dbo].[EST_NF_NFE_INU_VALIDADA]    Script Date: 03/06/2013 09:41:45 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[EST_NF_NFE_INU_VALIDADA]') AND type in (N'U'))
DROP TABLE [dbo].[EST_NF_NFE_INU_VALIDADA]
GO


/****** Object:  Table [dbo].[EST_NF_NFE_INU_VALIDADA]    Script Date: 03/06/2013 09:41:50 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[EST_NF_NFE_INU_VALIDADA](
	[CD_INU] [int] NOT NULL,
	[NR_PROTOCOLO] [varchar](50) NOT NULL,
	[VS_PROTOCOLO] [varchar](20) NOT NULL,
	[DH_PROTOCOLO] [datetime] NOT NULL,
	[DT_CAD] [datetime] NOT NULL,
 CONSTRAINT [PK_EST_NF_NFE_INU_VALIDADA] PRIMARY KEY CLUSTERED 
(
	[CD_INU] 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

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[EST_NF_NFE_INU_VALIDADA]  WITH CHECK ADD  CONSTRAINT [FK_EST_NF_NFE_INU_VALIDADA_EST_NF_NFE_INU] FOREIGN KEY([CD_INU])
REFERENCES [dbo].[EST_NF_NFE_INU] ([CD_INU])
GO

ALTER TABLE [dbo].[EST_NF_NFE_INU_VALIDADA] CHECK CONSTRAINT [FK_EST_NF_NFE_INU_VALIDADA_EST_NF_NFE_INU]
GO

ALTER TABLE [dbo].[EST_NF_NFE_INU_VALIDADA] ADD  CONSTRAINT [DF_EST_NF_NFE_INU_VALIDADA_CD_INU]  DEFAULT ((0)) FOR [CD_INU]
GO

ALTER TABLE [dbo].[EST_NF_NFE_INU_VALIDADA] ADD  DEFAULT (getdate()) FOR [DT_CAD]
GO


/****** Object:  Trigger [TRG_EST_NF_SAI_NFE_PEND_DEL]    Script Date: 02/19/2013 12:11:17 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRG_EST_NF_NFE_INU_PEND_VALIDADA]'))
DROP TRIGGER [dbo].[TRG_EST_NF_NFE_INU_PEND_VALIDADA]
GO
 
CREATE TRIGGER [dbo].[TRG_EST_NF_NFE_INU_PEND_VALIDADA] ON [dbo].[EST_NF_NFE_INU_VALIDADA] FOR INSERT AS
	
	DELETE FROM EST_NF_NFE_INU_PEND  WHERE CD_INU = (SELECT CD_INU FROM INSERTED)
GO
